Collapse Duplicate Loads with Singleflight

Share one in-flight load across concurrent callers, report who led the work, and prevent a cache stampede after expiry.

GoData Fetching

Code

gosnippet
76 lines
package main

import (
	"fmt"
	"sync"
	"sync/atomic"
	"time"
)

type call[T any] struct {
	wg  sync.WaitGroup
	val T
	err error
}

type Group[T any] struct {
	mu    sync.Mutex
	calls map[string]*call[T]
	loads atomic.Int64
}

func NewGroup[T any]() *Group[T] {
	return &Group[T]{calls: make(map[string]*call[T])}
}

func (g *Group[T]) Do(key string, load func() (T, error)) (T, bool, error) {
	g.mu.Lock()
	if existing, ok := g.calls[key]; ok {
		g.mu.Unlock()
		existing.wg.Wait()
		return existing.val, false, existing.err
	}

	current := &call[T]{}
	current.wg.Add(1)
	g.calls[key] = current
	g.loads.Add(1)
	g.mu.Unlock()

	current.val, current.err = load()
	current.wg.Done()

	g.mu.Lock()
	delete(g.calls, key)
	g.mu.Unlock()

	return current.val, true, current.err
}

func (g *Group[T]) Loads() int64 {
	return g.loads.Load()
}

func main() {
	group := NewGroup[string]()
	var wg sync.WaitGroup
	start := make(chan struct{})

	for id := 0; id < 8; id++ {
		wg.Add(1)
		go func(id int) {
			defer wg.Done()
			<-start
			value, leader, err := group.Do("user:42", func() (string, error) {
				time.Sleep(30 * time.Millisecond)
				return "profile-42", nil
			})
			fmt.Printf("caller %d leader=%t value=%s err=%v\n", id, leader, value, err)
		}(id)
	}

	close(start)
	wg.Wait()
	fmt.Println("loads:", group.Loads())
}

How it works

What another module actually calls is the functions Do, Loads and main — everything else is wiring.

Repetition is confined to for id := 0; id < 8; id++ — the rest of the file is straight-line. Work runs on a goroutine and results come back through a channel, giving a safe handoff between the two execution contexts. defer schedules the cleanup, and the scheduled call fires no matter how the function exits.

Using it

The signature doing the main work is func (g *Group[T]) Do(key string, load func() (T, error)) (T, bool, error)

The run prints labeled output — caller, loads: — when the file runs.

main is the driver: it invokes Do and Loads before returning.

Key statements

return &Group[T]{calls: make(map[string]*call[T])}
func (g *Group[T]) Loads() int64 {
	return g.loads.Load()
}
start := make(chan struct{})

Interactive Tools for This Topic

FAQ

What does Collapse Duplicate Loads with Singleflight print when run?
The script runs standalone; its 2 labeled outputs include `caller`, `loads:`.