|
@@ -3,13 +3,17 @@ package syncx
|
|
|
import "sync"
|
|
|
|
|
|
type (
|
|
|
- // SharedCalls lets the concurrent calls with the same key to share the call result.
|
|
|
+ // SharedCalls is an alias of SingleFlight.
|
|
|
+ // Deprecated: use SingleFlight.
|
|
|
+ SharedCalls = SingleFlight
|
|
|
+
|
|
|
+ // SingleFlight lets the concurrent calls with the same key to share the call result.
|
|
|
// For example, A called F, before it's done, B called F. Then B would not execute F,
|
|
|
// and shared the result returned by F which called by A.
|
|
|
// The calls with the same key are dependent, concurrent calls share the returned values.
|
|
|
// A ------->calls F with key<------------------->returns val
|
|
|
// B --------------------->calls F with key------>returns val
|
|
|
- SharedCalls interface {
|
|
|
+ SingleFlight interface {
|
|
|
Do(key string, fn func() (interface{}, error)) (interface{}, error)
|
|
|
DoEx(key string, fn func() (interface{}, error)) (interface{}, bool, error)
|
|
|
}
|
|
@@ -20,20 +24,26 @@ type (
|
|
|
err error
|
|
|
}
|
|
|
|
|
|
- sharedGroup struct {
|
|
|
+ flightGroup struct {
|
|
|
calls map[string]*call
|
|
|
lock sync.Mutex
|
|
|
}
|
|
|
)
|
|
|
|
|
|
-// NewSharedCalls returns a SharedCalls.
|
|
|
-func NewSharedCalls() SharedCalls {
|
|
|
- return &sharedGroup{
|
|
|
+// NewSingleFlight returns a SingleFlight.
|
|
|
+func NewSingleFlight() SingleFlight {
|
|
|
+ return &flightGroup{
|
|
|
calls: make(map[string]*call),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (g *sharedGroup) Do(key string, fn func() (interface{}, error)) (interface{}, error) {
|
|
|
+// NewSharedCalls returns a SingleFlight.
|
|
|
+// Deprecated: use NewSingleFlight.
|
|
|
+func NewSharedCalls() SingleFlight {
|
|
|
+ return NewSingleFlight()
|
|
|
+}
|
|
|
+
|
|
|
+func (g *flightGroup) Do(key string, fn func() (interface{}, error)) (interface{}, error) {
|
|
|
c, done := g.createCall(key)
|
|
|
if done {
|
|
|
return c.val, c.err
|
|
@@ -43,7 +53,7 @@ func (g *sharedGroup) Do(key string, fn func() (interface{}, error)) (interface{
|
|
|
return c.val, c.err
|
|
|
}
|
|
|
|
|
|
-func (g *sharedGroup) DoEx(key string, fn func() (interface{}, error)) (val interface{}, fresh bool, err error) {
|
|
|
+func (g *flightGroup) DoEx(key string, fn func() (interface{}, error)) (val interface{}, fresh bool, err error) {
|
|
|
c, done := g.createCall(key)
|
|
|
if done {
|
|
|
return c.val, false, c.err
|
|
@@ -53,7 +63,7 @@ func (g *sharedGroup) DoEx(key string, fn func() (interface{}, error)) (val inte
|
|
|
return c.val, true, c.err
|
|
|
}
|
|
|
|
|
|
-func (g *sharedGroup) createCall(key string) (c *call, done bool) {
|
|
|
+func (g *flightGroup) createCall(key string) (c *call, done bool) {
|
|
|
g.lock.Lock()
|
|
|
if c, ok := g.calls[key]; ok {
|
|
|
g.lock.Unlock()
|
|
@@ -69,7 +79,7 @@ func (g *sharedGroup) createCall(key string) (c *call, done bool) {
|
|
|
return c, false
|
|
|
}
|
|
|
|
|
|
-func (g *sharedGroup) makeCall(c *call, key string, fn func() (interface{}, error)) {
|
|
|
+func (g *flightGroup) makeCall(c *call, key string, fn func() (interface{}, error)) {
|
|
|
defer func() {
|
|
|
g.lock.Lock()
|
|
|
delete(g.calls, key)
|