Deduplicate work with singleflight concepts
Deduplicate work with singleflight concepts
Singleflight-style coordination shares one in-progress operation per key. It prevents redundant simultaneous loads, but it is not a cache of completed results. This teaching implementation uses a lock and completion channel. Its loader must return normally; panic, errors and cancellation are not handled.
package main
import ( "fmt" "sync")
type Call struct { done chan struct{} value string}type Group struct { mu sync.Mutex inflight map[string]*Call}
func (g *Group) Start(key string, load func() string) (*Call, bool) { g.mu.Lock() defer g.mu.Unlock() if g.inflight == nil { g.inflight = make(map[string]*Call) } if c, ok := g.inflight[key]; ok { return c, false } c := &Call{done: make(chan struct{})} g.inflight[key] = c go func() { v := load() g.mu.Lock() c.value = v delete(g.inflight, key) close(c.done) g.mu.Unlock() }() return c, true}func (c *Call) Wait() string { <-c.done; return c.value }func main() { var g Group release := make(chan struct{}) load := func() string { <-release; return "report" } a, first := g.Start("day", load) b, second := g.Start("day", load) fmt.Println(first, second, a == b) close(release) fmt.Println(a.Wait(), b.Wait())}The map lock makes check-and-create indivisible. The first caller launches work; the second joins the same Call. A test gate ensures overlap without timing guesses. Closing done publishes the completed value to waiters. Different keys may execute concurrently. Shared mutable results need an ownership policy.
Expected output:
true false truereport reportCompleted work is not cached
package main
import ( "fmt" "sync")
type Call struct { done chan struct{} value string}type Group struct { mu sync.Mutex inflight map[string]*Call}
func (g *Group) Start(key string, load func() string) (*Call, bool) { g.mu.Lock() defer g.mu.Unlock() if g.inflight == nil { g.inflight = make(map[string]*Call) } if c, ok := g.inflight[key]; ok { return c, false } c := &Call{done: make(chan struct{})} g.inflight[key] = c go func() { v := load() g.mu.Lock() c.value = v delete(g.inflight, key) close(c.done) g.mu.Unlock() }() return c, true}func (c *Call) Wait() string { <-c.done; return c.value }func main() { var g Group a, _ := g.Start("k", func() string { return "old" }) fmt.Println(a.Wait()) b, newWork := g.Start("k", func() string { return "new" }) fmt.Println(newWork, b.Wait())}The in-flight entry is deleted at completion. The external golang.org/x/sync/singleflight package provides duplicate-call suppression; no external API version is used or tested here. Production code must define shared error handling, waiter cancellation, bounds and key isolation. Do not merge work across authorization boundaries.
Expected output:
oldtrue newTry it locally
Start overlapping calls with different keys and one shared gate. Verify both start new work; release and print A B.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Concurrency patterns 05: Deduplicate work with singleflight concepts
Check the lesson objective before moving on.
What is shared by overlapping callers?
Why is this not a cache?