@@ -64,22 +64,7 @@ func (v RefCounter) Decrement() bool {
64
64
// DoRefCount should NOT be mixed for same key with Do/DoChan to avoid race condition in calulating "shared" retiun value
65
65
// The return value refCount maintains "reference counter" for multiple callers.
66
66
func (g * Group ) DoRefCount (key string , fn func () (interface {}, error )) (v interface {}, err error , refCount RefCounter ) {
67
- g .mu .Lock ()
68
- if g .m == nil {
69
- g .m = make (map [string ]* call )
70
- }
71
- if c , ok := g .m [key ]; ok {
72
- c .dups ++
73
- g .mu .Unlock ()
74
- c .wg .Wait ()
75
- return c .val , c .err , RefCounter {ptr : & c .dups , zero : - 1 }
76
- }
77
- c := new (call )
78
- c .wg .Add (1 )
79
- g .m [key ] = c
80
- g .mu .Unlock ()
81
-
82
- g .doCall (c , key , fn )
67
+ c := g .doNoChan (key , fn )
83
68
84
69
// use -1 instead of 0 because call.dups is initialized with 0,
85
70
// in other words: for single caller RefCounter will hold value of 0
@@ -92,6 +77,11 @@ func (g *Group) DoRefCount(key string, fn func() (interface{}, error)) (v interf
92
77
// original to complete and receives the same results.
93
78
// The return value shared indicates whether v was given to multiple callers.
94
79
func (g * Group ) Do (key string , fn func () (interface {}, error )) (v interface {}, err error , shared bool ) {
80
+ c := g .doNoChan (key , fn )
81
+ return c .val , c .err , c .dups > 0
82
+ }
83
+
84
+ func (g * Group ) doNoChan (key string , fn func () (interface {}, error )) * call {
95
85
g .mu .Lock ()
96
86
if g .m == nil {
97
87
g .m = make (map [string ]* call )
@@ -100,15 +90,15 @@ func (g *Group) Do(key string, fn func() (interface{}, error)) (v interface{}, e
100
90
c .dups ++
101
91
g .mu .Unlock ()
102
92
c .wg .Wait ()
103
- return c . val , c . err , true
93
+ return c
104
94
}
105
95
c := new (call )
106
96
c .wg .Add (1 )
107
97
g .m [key ] = c
108
98
g .mu .Unlock ()
109
99
110
100
g .doCall (c , key , fn )
111
- return c . val , c . err , c . dups > 0
101
+ return c
112
102
}
113
103
114
104
// DoChan is like Do but returns a channel that will receive the
0 commit comments