Deduplicate Concurrent Calls With singleflight

Collapse duplicate concurrent requests for the same key into a single upstream call so a burst of callers does not stampede a slow dependency.

GoResilience

Code

snippet
package cache

import (
	"context"

	"golang.org/x/sync/singleflight"
)

type Loader func(ctx context.Context, key string) (string, error)

type Group struct {
	flight singleflight.Group
	load   Loader
}

func NewGroup(load Loader) *Group {
	return &Group{load: load}
}

func (g *Group) Get(ctx context.Context, key string) (string, error) {
	value, err, _ := g.flight.Do(key, func() (interface{}, error) {
		return g.load(ctx, key)
	})
	if err != nil {
		return "", err
	}
	return value.(string), nil
}