Documentation
¶
Overview ¶
Package ctxsync provides context aware synchronisation primitives.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SingleFlight ¶ added in v0.0.10
type SingleFlight struct {
// contains filtered or unexported fields
}
SingleFlight mirrors golang.org/x/sync/singleflight.Group but with different handling of context cancellation. In particular, if a shared invocation returns with a canceled or timed out context, but the caller's context is not canceled, SingleFlight will reissue the invocation. This handles the case where one invocation has its context canceled, but others have not and hence could potentially succeed if reissued.
func (*SingleFlight) Do ¶ added in v0.0.10
func (g *SingleFlight) Do(ctx context.Context, key string, fn func() (any, error)) (v any, err error, shared bool)
Do is like singleflight.Group.Do but with different handling of context cancellation. In particular, if a shared invocation returns with a canceled or timed out context, but the caller's context is not canceled, SingleFlight will reissue the invocation.
func (*SingleFlight) DoChan ¶ added in v0.0.10
func (g *SingleFlight) DoChan(ctx context.Context, key string, fn func() (any, error)) <-chan singleflight.Result
DoChan is like singleflight.Group.DoChan but with different handling of context cancellation. In particular, if a shared invocation returns with a canceled or timed out context, but the caller's context is not canceled, SingleFlight will reissue the invocation.
func (*SingleFlight) Forget ¶ added in v0.0.10
func (g *SingleFlight) Forget(key string)
type WaitGroup ¶
type WaitGroup struct {
// contains filtered or unexported fields
}
WaitGroup is a context-aware sync.WaitGroup. The zero value is ready to use. Unlike sync.WaitGroup, it is safe to call Add immediately after a Wait that returned due to context cancellation.
Example ¶
package main
import (
"context"
"fmt"
"time"
"cloudeng.io/sync/ctxsync"
)
func main() {
var wg ctxsync.WaitGroup
wg.Add(1)
ctx, cancel := context.WithCancel(context.Background()) //nolint:gosec // G118 false positive
go func() {
time.Sleep(time.Second)
cancel()
}()
wg.Wait(ctx)
fmt.Println(ctx.Err())
}
Output: context canceled
func (*WaitGroup) Add ¶ added in v0.0.9
Add adds delta to the WaitGroup counter. If the counter transitions from zero to positive a new completion channel is allocated. If the counter transitions from positive to zero all blocked Wait calls are unblocked. It panics if the counter goes negative.
func (*WaitGroup) Done ¶ added in v0.0.9
func (wg *WaitGroup) Done()
Done decrements the WaitGroup counter by one.