Debian Codegraph @

singleflight/singleflight.go

raw symbols SCIP
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package singleflight provides a duplicate function call suppression
6 // mechanism.
7 package singleflightscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/ // import "golang.org/x/sync/singleflight"
8
9 import (
10 "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/"
11 "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/"
12 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
13 "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/"
14 "runtime/debugscip-go gomod github.com/golang/go/src go1.25.0 `runtime/debug`/"
15 "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/"
16 )
17
18 // errGoexit indicates the runtime.Goexit was called in
19 // the user given function.
20 var errGoexitscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/errGoexit. = errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("runtime.Goexit was called")
21
22 // A panicError is an arbitrary value recovered from a panic
23 // with the stack trace during the execution of given function.
24 type panicErrorscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError# struct {
25 valuescip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#value. any
26 stackscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#stack. []byte
27 }
28
29 // Error implements error interface.
30 func (p *panicErrorscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#) Errorscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#Error().() string {
31 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%v\n\n%s", p.valuescip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#value., p.stackscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#stack.)
32 }
33
34 func (p *panicErrorscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#) Unwrapscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#Unwrap().() error {
35 err, ok := p.valuescip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#value..(error)
36 if !ok {
37 return nil
38 }
39
40 return err
41 }
42
43 func newPanicErrorscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/newPanicError().(v any) error {
44 stack := debugscip-go gomod github.com/golang/go/src go1.25.0 `runtime/debug`/.Stackscip-go gomod github.com/golang/go/src go1.25.0 `runtime/debug`/Stack().()
45
46 // The first line of the stack trace is of the form "goroutine N [status]:"
47 // but by the time the panic reaches Do the goroutine may no longer exist
48 // and its status will have changed. Trim out the misleading line.
49 if line := bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.IndexBytescip-go gomod github.com/golang/go/src go1.25.0 bytes/IndexByte().(stack[:], '\n'); line >= 0 {
50 stack = stack[line+1:]
51 }
52 return &panicErrorscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#{valuescip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#value.: v, stackscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#stack.: stack}
53 }
54
55 // call is an in-flight or completed singleflight.Do call
56 type callscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call# struct {
57 wgscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#wg. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#
58
59 // These fields are written once before the WaitGroup is done
60 // and are only read after the WaitGroup is done.
61 valscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#val. any
62 errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#err. error
63
64 // These fields are read and written with the singleflight
65 // mutex held before the WaitGroup is done, and are read but
66 // not written after the WaitGroup is done.
67 dupsscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#dups. int
68 chansscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#chans. []chan<- Resultscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Result#
69 }
70
71 // Group represents a class of work and forms a namespace in
72 // which units of work can be executed with duplicate suppression.
73 type Groupscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group# struct {
74 muscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#mu. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex# // protects m
75 mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m. map[string]*callscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call# // lazily initialized
76 }
77
78 // Result holds the results of Do, so they can be passed
79 // on a channel.
80 type Resultscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Result# struct {
81 Valscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Result#Val. any
82 Errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Result#Err. error
83 Sharedscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Result#Shared. bool
84 }
85
86 // Do executes and returns the results of the given function, making
87 // sure that only one execution is in-flight for a given key at a
88 // time. If a duplicate comes in, the duplicate caller waits for the
89 // original to complete and receives the same results.
90 // The return value shared indicates whether v was given to multiple callers.
91 func (g *Groupscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#) Doscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#Do().(key string, fn func() (any, error)) (v any, err error, shared bool) {
92 g.muscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().()
93 if g.mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m. == nil {
94 g.mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m. = make(map[string]*callscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#)
95 }
96 if c, ok := g.mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m.[key]; ok {
97 c.dupsscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#dups.++
98 g.muscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().()
99 c.wgscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#wg..Waitscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Wait().()
100
101 if e, ok := c.errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#err..(*panicErrorscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#); ok {
102 panic(e)
103 } else if c.errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#err. == errGoexitscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/errGoexit. {
104 runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.Goexitscip-go gomod github.com/golang/go/src go1.25.0 runtime/Goexit().()
105 }
106 return c.valscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#val., c.errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#err., true
107 }
108 c := new(callscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#)
109 c.wgscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#wg..Addscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Add().(1)
110 g.mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m.[key] = c
111 g.muscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().()
112
113 g.doCallscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#doCall().(c, key, fn)
114 return c.valscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#val., c.errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#err., c.dupsscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#dups. > 0
115 }
116
117 // DoChan is like Do but returns a channel that will receive the
118 // results when they are ready.
119 //
120 // The returned channel will not be closed.
121 func (g *Groupscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#) DoChanscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#DoChan().(key string, fn func() (any, error)) <-chan Resultscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Result# {
122 ch := make(chan Resultscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Result#, 1)
123 g.muscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().()
124 if g.mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m. == nil {
125 g.mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m. = make(map[string]*callscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#)
126 }
127 if c, ok := g.mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m.[key]; ok {
128 c.dupsscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#dups.++
129 c.chansscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#chans. = append(c.chansscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#chans., ch)
130 g.muscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().()
131 return ch
132 }
133 c := &callscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#{chansscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#chans.: []chan<- Resultscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Result#{ch}}
134 c.wgscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#wg..Addscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Add().(1)
135 g.mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m.[key] = c
136 g.muscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().()
137
138 go g.doCallscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#doCall().(c, key, fn)
139
140 return ch
141 }
142
143 // doCall handles the single call for a key.
144 func (g *Groupscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#) doCallscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#doCall().(c *callscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#, key string, fn func() (any, error)) {
145 normalReturn := false
146 recovered := false
147
148 // use double-defer to distinguish panic from runtime.Goexit,
149 // more details see https://golang.org/cl/134395
150 defer func() {
151 // the given function invoked runtime.Goexit
152 if !normalReturn && !recovered {
153 c.errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#err. = errGoexitscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/errGoexit.
154 }
155
156 g.muscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().()
157 defer g.muscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().()
158 c.wgscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#wg..Donescip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Done().()
159 if g.mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m.[key] == c {
160 delete(g.mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m., key)
161 }
162
163 if e, ok := c.errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#err..(*panicErrorscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/panicError#); ok {
164 // In order to prevent the waiting channels from being blocked forever,
165 // needs to ensure that this panic cannot be recovered.
166 if len(c.chansscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#chans.) > 0 {
167 go panic(e)
168 select {} // Keep this goroutine around so that it will appear in the crash dump.
169 } else {
170 panic(e)
171 }
172 } else if c.errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#err. == errGoexitscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/errGoexit. {
173 // Already in the process of goexit, no need to call again
174 } else {
175 // Normal return
176 for _, ch := range c.chansscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#chans. {
177 ch <- Resultscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Result#{c.valscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#val., c.errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#err., c.dupsscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#dups. > 0}
178 }
179 }
180 }()
181
182 func() {
183 defer func() {
184 if !normalReturn {
185 // Ideally, we would wait to take a stack trace until we've determined
186 // whether this is a panic or a runtime.Goexit.
187 //
188 // Unfortunately, the only way we can distinguish the two is to see
189 // whether the recover stopped the goroutine from terminating, and by
190 // the time we know that, the part of the stack trace relevant to the
191 // panic has been discarded.
192 if r := recover(); r != nil {
193 c.errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#err. = newPanicErrorscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/newPanicError().(r)
194 }
195 }
196 }()
197
198 c.valscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#val., c.errscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/call#err. = fn()
199 normalReturn = true
200 }()
201
202 if !normalReturn {
203 recovered = true
204 }
205 }
206
207 // Forget tells the singleflight to forget about a key. Future calls
208 // to Do for this key will call the function rather than waiting for
209 // an earlier call to complete.
210 func (g *Groupscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#) Forgetscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#Forget().(key string) {
211 g.muscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().()
212 delete(g.mscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#m., key)
213 g.muscip-go gomod golang.org/x/sync . `golang.org/x/sync/singleflight`/Group#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().()
214 }