tdsync

package
v0.0.0-...-87e9d67 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package tdsync contains some useful synchronization utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SyncBackoff

func SyncBackoff(from backoff.BackOff) backoff.BackOff

SyncBackoff decorates backoff.BackOff to be thread-safe.

Types

type CancellableGroup

type CancellableGroup struct {
	// contains filtered or unexported fields
}

CancellableGroup is simple wrapper around errgroup.Group to make group cancellation easier. Unlike WaitGroup and errgroup.Group this is not allowed to use zero value.

func NewCancellableGroup

func NewCancellableGroup(parent context.Context) *CancellableGroup

NewCancellableGroup creates new CancellableGroup.

Example:

g := NewCancellableGroup(ctx)
g.Go(func(ctx context.Context) error {
	<-ctx.Done()
	return ctx.Err()
})
g.Cancel()
g.Wait()

func (*CancellableGroup) Cancel

func (g *CancellableGroup) Cancel()

Cancel cancels all goroutines in group.

Note: context cancellation error will be returned by Wait().

func (*CancellableGroup) Go

func (g *CancellableGroup) Go(f func(ctx context.Context) error)

Go calls the given function in a new goroutine.

The first call to return a non-nil error cancels the group; its error will be returned by Wait.

func (*CancellableGroup) Wait

func (g *CancellableGroup) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

type LogGroup

type LogGroup struct {
	// contains filtered or unexported fields
}

LogGroup is simple wrapper around CancellableGroup to log task state. Unlike WaitGroup and errgroup.Group this is not allowed to use zero value.

func NewLogGroup

func NewLogGroup(parent context.Context, log *zap.Logger) *LogGroup

NewLogGroup creates new LogGroup.

func (*LogGroup) Cancel

func (g *LogGroup) Cancel()

Cancel cancels all goroutines in group.

Note: context cancellation error will be returned by Wait().

func (*LogGroup) Go

func (g *LogGroup) Go(taskName string, f func(groupCtx context.Context) error)

Go calls the given function in a new goroutine.

The first call to return a non-nil error cancels the group; its error will be returned by Wait.

func (*LogGroup) SetClock

func (g *LogGroup) SetClock(c clock.Clock)

SetClock sets Clock to use.

func (*LogGroup) Wait

func (g *LogGroup) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

type Ready

type Ready struct {
	// contains filtered or unexported fields
}

Ready is simple signal primitive which sends signal once. This is not allowed to use zero value.

func NewReady

func NewReady() *Ready

NewReady creates new Ready.

func (*Ready) Ready

func (r *Ready) Ready() <-chan struct{}

Ready returns waiting channel.

func (*Ready) Signal

func (r *Ready) Signal()

Signal sends ready signal. Can be called multiple times.

type ResetReady

type ResetReady struct {
	// contains filtered or unexported fields
}

ResetReady is like Ready, but can be Reset.

func NewResetReady

func NewResetReady() *ResetReady

NewResetReady creates new ResetReady.

func (*ResetReady) Ready

func (r *ResetReady) Ready() <-chan struct{}

Ready returns waiting channel.

func (*ResetReady) Reset

func (r *ResetReady) Reset()

Reset resets underlying Ready.

func (*ResetReady) Signal

func (r *ResetReady) Signal()

Signal sends ready signal. Can be called multiple times.

type Supervisor

type Supervisor struct {
	// contains filtered or unexported fields
}

Supervisor is simple task group primitive to control multiple long-live tasks. Unlike Groups, Supervisor does not cancel when one task is failed. Unlike WaitGroup and errgroup.Group this is not allowed to use zero value.

func NewSupervisor

func NewSupervisor(parent context.Context) *Supervisor

NewSupervisor creates new Supervisor.

func (*Supervisor) Cancel

func (s *Supervisor) Cancel()

Cancel cancels all goroutines in group.

Note: context cancellation error can be returned by Wait().

func (*Supervisor) Go

func (s *Supervisor) Go(task func(ctx context.Context) error)

Go calls the given function in a new goroutine.

func (*Supervisor) Wait

func (s *Supervisor) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

func (*Supervisor) WithErrorHandler

func (s *Supervisor) WithErrorHandler(h func(err error)) *Supervisor

WithErrorHandler sets tasks error handler Must be called before any Go calls.

Jump to

Keyboard shortcuts

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