Never Shared Once
A team adds request coalescing in front of an 800 ms origin query. Under load the origin still takes one call per concurrent reader, and line 4 logs shared=false every single time.
func GetProfile(ctx context.Context, id string) (*Profile, error) {
if p, ok := cache.Get(id); ok {
return p, nil
}
var g singleflight.Group // 1
v, err, shared := g.Do(id, func() (any, error) { // 2
p, err := origin.LoadProfile(ctx, id) // 3 ~800 ms
if err != nil {
return nil, err
}
cache.Set(id, p, 30*time.Second)
return p, nil
})
log.Printf("profile %s shared=%v", id, shared) // 4
if err != nil {
return nil, err
}
return v.(*Profile), nil
}
Which line is the bug?
Sign in to answer questions and track your progress
Sign In