sync

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const ErrSignalTrapped errors.Message = "signal trapped"

ErrSignalTrapped is returned by the SignalTrap.Wait when the expected signals caught.

Variables

This section is empty.

Functions

func Times added in v0.2.0

func Times(n uint32) interface{ Do(func()) }

Times add possibility to Do some action the N times.

barrier := sync.Times(100)

func Call(action func() error) {
	barrier.Do(func () {
		if err := action(); err != nil {
			logger.WithError(err).Error("send the error to the Sentry only 100 times")
		}
	})
}

Types

type SignalTrap

type SignalTrap chan os.Signal

SignalTrap wraps os.Signal channel to provide high level API above it.

trap := make(chan os.Signal)
signal.Notify(trap, os.Interrupt)
SignalTrap(trap).Wait(context.Background())

func Termination

func Termination() SignalTrap

Termination returns trap for termination signals.

server := new(http.Server)
go safe.Do(server.ListenAndServe, func(err error) { log.Println(err) })

err := sync.Termination().Wait(context.Background())
if err == sync.ErrSignalTrapped {
	log.Println("shutting down the server", server.Shutdown(context.Background()))
}

func (SignalTrap) Wait

func (trap SignalTrap) Wait(ctx context.Context) error

Wait blocks until one of the expected signals caught or the Context closed. It unregisters from the notification and closes itself.

Jump to

Keyboard shortcuts

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