syncs

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package syncs provides synchronization primitives util functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextValue added in v0.7.2

func ContextValue(key, value any) context.Context

ContextValue create a new context with given value

func Go

func Go(f func() error) error

Go is a basic promise implementation: it wraps calls a function in a goroutine and returns a channel which will later return the function's return value.

func SignalHandler

func SignalHandler(ctx context.Context, signals ...os.Signal) (execute func() error, interrupt func(error))

SignalHandler returns an actor, i.e. an execute and interrupt func, that terminates with SignalError when the process receives one of the provided signals, or the parent context is canceled.

from https://github.com/oklog/run/blob/master/actors.go

func WaitCloseSignals

func WaitCloseSignals(onClose func(sig os.Signal), sigCh ...chan os.Signal)

WaitCloseSignals for some huang program.

Usage:

// do something. eg: start a http server

syncs.WaitCloseSignals(func(sig os.Signal) {
	// do something on shutdown. eg: close db, flush logs
})

Types

type ErrGroup added in v0.6.13

type ErrGroup struct {
	*errgroup.Group
}

ErrGroup is a collection of goroutines working on subtasks that are part of the same overall task.

Refers:

https://github.com/neilotoole/errgroup
https://github.com/fatih/semgroup

func NewCtxErrGroup added in v0.6.13

func NewCtxErrGroup(ctx context.Context, limit ...int) (*ErrGroup, context.Context)

NewCtxErrGroup instance

func NewErrGroup added in v0.6.13

func NewErrGroup(limit ...int) *ErrGroup

NewErrGroup instance

func (*ErrGroup) Add added in v0.6.13

func (g *ErrGroup) Add(handlers ...func() error)

Add one or more handler at once

type SignalError

type SignalError struct {
	Signal os.Signal
}

SignalError is returned by the signal handler's execute function when it terminates due to a received signal.

func (SignalError) Error

func (e SignalError) Error() string

Error implements the error interface.

type WaitGroup added in v0.7.0

type WaitGroup struct {
	sync.WaitGroup
}

WaitGroup is a wrapper of sync.WaitGroup.

func (*WaitGroup) Go added in v0.7.0

func (wg *WaitGroup) Go(fn func())

Go runs the given function in a new goroutine. will auto call Add and Done.

Jump to

Keyboard shortcuts

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