Documentation
¶
Overview ¶
Package singleflight provides a duplicate function call suppression mechanism.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Group ¶
type Group[T any] struct { // contains filtered or unexported fields }
Group represents a class of work and forms a namespace in which units of work can be executed with duplicate suppression. Unlike the original singleflight Group, this version uses generics. A Group[T]’s Do method returns a Result[T], so the value has the same type T used to create the Group.
Example ¶
g := NewGroup[int]()
block := make(chan struct{})
started := make(chan struct{}, 2)
var r1, r2 Result[int]
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
started <- struct{}{}
r1 = g.Do("key", func() (int, error) {
<-block
return 1, nil
})
}()
go func() {
defer wg.Done()
started <- struct{}{}
r2 = g.Do("key", func() (int, error) {
<-block
return 2, nil
})
}()
<-started
<-started
close(block)
wg.Wait()
fmt.Println("Equal result:", r1.Val == r2.Val)
Output: Equal result: true
type Result ¶
Result is the return value of a singleflight.Do call.
The shared field indicates whether the value was given to multiple callers. The initial field indicates whether this is the first call to Do for the given key.
Click to show internal directories.
Click to hide internal directories.