...
1
2
3
4
5 package singleflight
6
7 import (
8 "bytes"
9 "errors"
10 "fmt"
11 "runtime"
12 "runtime/debug"
13 "sync"
14 )
15
16
17
18 var errGoexit = errors.New("runtime.Goexit was called")
19
20
21
22 type panicError struct {
23 value interface{}
24 stack []byte
25 }
26
27
28 func (p *panicError) Error() string {
29 return fmt.Sprintf("%v\n\n%s", p.value, p.stack)
30 }
31
32 func newPanicError(v interface{}) error {
33 stack := debug.Stack()
34
35
36
37
38 if line := bytes.IndexByte(stack[:], '\n'); line >= 0 {
39 stack = stack[line+1:]
40 }
41 return &panicError{value: v, stack: stack}
42 }
43
44
45 type call struct {
46 wg sync.WaitGroup
47
48
49
50 val interface{}
51 err error
52
53
54
55 forgotten bool
56
57
58
59
60 dups int
61 chans []chan<- Result
62 }
63
64
65
66 type Group struct {
67 mu sync.Mutex
68 m map[string]*call
69 }
70
71
72
73 type Result struct {
74 Val interface{}
75 Err error
76 Shared bool
77 }
78
79
80
81
82
83
84 func (g *Group) Do(key string, fn func() (interface{}, error)) (v interface{}, err error, shared bool) {
85 g.mu.Lock()
86 if g.m == nil {
87 g.m = make(map[string]*call)
88 }
89 if c, ok := g.m[key]; ok {
90 c.dups++
91 g.mu.Unlock()
92 c.wg.Wait()
93
94 if e, ok := c.err.(*panicError); ok {
95 panic(e)
96 } else if c.err == errGoexit {
97 runtime.Goexit()
98 }
99 return c.val, c.err, true
100 }
101 c := new(call)
102 c.wg.Add(1)
103 g.m[key] = c
104 g.mu.Unlock()
105
106 g.doCall(c, key, fn)
107 return c.val, c.err, c.dups > 0
108 }
109
110
111
112
113
114 func (g *Group) DoChan(key string, fn func() (interface{}, error)) <-chan Result {
115 ch := make(chan Result, 1)
116 g.mu.Lock()
117 if g.m == nil {
118 g.m = make(map[string]*call)
119 }
120 if c, ok := g.m[key]; ok {
121 c.dups++
122 c.chans = append(c.chans, ch)
123 g.mu.Unlock()
124 return ch
125 }
126 c := &call{chans: []chan<- Result{ch}}
127 c.wg.Add(1)
128 g.m[key] = c
129 g.mu.Unlock()
130
131 go g.doCall(c, key, fn)
132
133 return ch
134 }
135
136
137 func (g *Group) doCall(c *call, key string, fn func() (interface{}, error)) {
138 normalReturn := false
139 recovered := false
140
141
142
143 defer func() {
144
145 if !normalReturn && !recovered {
146 c.err = errGoexit
147 }
148
149 c.wg.Done()
150 g.mu.Lock()
151 defer g.mu.Unlock()
152 if !c.forgotten {
153 delete(g.m, key)
154 }
155
156 if e, ok := c.err.(*panicError); ok {
157
158
159 if len(c.chans) > 0 {
160 go panic(e)
161 select {}
162 } else {
163 panic(e)
164 }
165 } else if c.err == errGoexit {
166
167 } else {
168
169 for _, ch := range c.chans {
170 ch <- Result{c.val, c.err, c.dups > 0}
171 }
172 }
173 }()
174
175 func() {
176 defer func() {
177 if !normalReturn {
178
179
180
181
182
183
184
185 if r := recover(); r != nil {
186 c.err = newPanicError(r)
187 }
188 }
189 }()
190
191 c.val, c.err = fn()
192 normalReturn = true
193 }()
194
195 if !normalReturn {
196 recovered = true
197 }
198 }
199
200
201
202
203 func (g *Group) Forget(key string) {
204 g.mu.Lock()
205 if c, ok := g.m[key]; ok {
206 c.forgotten = true
207 }
208 delete(g.m, key)
209 g.mu.Unlock()
210 }
211
View as plain text