ctxsync

package
v0.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

README

Package cloudeng.io/sync/ctxsync

import cloudeng.io/sync/ctxsync

Package ctxsync provides context aware synchronisation primitives.

Types

Type SingleFlight
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.

Functions
func New() *SingleFlight

New creates a new SingleFlight instance.

Methods
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 (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 (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.

Methods
func (wg *WaitGroup) Add(delta int)

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 (wg *WaitGroup) Done()

Done decrements the WaitGroup counter by one.

func (wg *WaitGroup) Go(f func())

Go calls f in a new goroutine and adds that task to the WaitGroup. When f returns, the task is removed from the WaitGroup. If f panics, the task is not removed to ensure the panic remains fatal.

func (wg *WaitGroup) Wait(ctx context.Context)

Wait blocks until the WaitGroup counter reaches zero or the context is canceled, whichever comes first.

Examples

ExampleWaitGroup

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 New added in v0.0.10

func New() *SingleFlight

New creates a new SingleFlight instance.

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

func (wg *WaitGroup) Add(delta int)

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.

func (*WaitGroup) Go added in v0.0.9

func (wg *WaitGroup) Go(f func())

Go calls f in a new goroutine and adds that task to the WaitGroup. When f returns, the task is removed from the WaitGroup. If f panics, the task is not removed to ensure the panic remains fatal.

func (*WaitGroup) Wait

func (wg *WaitGroup) Wait(ctx context.Context)

Wait blocks until the WaitGroup counter reaches zero or the context is canceled, whichever comes first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL