capypad
0 day streak
go / expert
Snippet

Request Collapsing with singleflight

The singleflight package provides a duplicate function call suppression mechanism. It ensures that for a given key, only one execution of a function is in flight at a time. If multiple goroutines call Do with the same key simultaneously, the first one triggers the function execution, and the others wait to receive the exact same result once it's ready, effectively preventing 'thundering herd' problems on downstream services.

snippet.go
go
1
2
3
4
5
6
7
8
9
10
11
var g singleflight.Group
 
func fetchData(key string) (interface{}, error) {
v, err, shared := g.Do(key, func() (interface{}, error) {
return callExpensiveAPI(key)
})
if shared {
log.Printf("Result for %s was shared", key)
}
return v, err
}
Breakdown
1
g.Do(key, func()
Executes and suppresses duplicate calls for the given key.
2
shared
A boolean indicating whether the result was returned to multiple callers.