Skip to content

Commit 4eee079

Browse files
committed
refactor Group.Do implement via Group.Dochan call
1 parent cd5d95a commit 4eee079

File tree

1 file changed

+2
-17
lines changed

1 file changed

+2
-17
lines changed

singleflight/singleflight.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,8 @@ type Result struct {
4949
// original to complete and receives the same results.
5050
// The return value shared indicates whether v was given to multiple callers.
5151
func (g *Group) Do(key string, fn func() (interface{}, error)) (v interface{}, err error, shared bool) {
52-
g.mu.Lock()
53-
if g.m == nil {
54-
g.m = make(map[string]*call)
55-
}
56-
if c, ok := g.m[key]; ok {
57-
c.dups++
58-
g.mu.Unlock()
59-
c.wg.Wait()
60-
return c.val, c.err, true
61-
}
62-
c := new(call)
63-
c.wg.Add(1)
64-
g.m[key] = c
65-
g.mu.Unlock()
66-
67-
g.doCall(c, key, fn)
68-
return c.val, c.err, c.dups > 0
52+
r := <-g.DoChan(key, fn)
53+
return r.Val, r.Err, r.Shared
6954
}
7055

7156
// DoChan is like Do but returns a channel that will receive the

0 commit comments

Comments
 (0)