shutdown

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2023 License: MIT Imports: 9 Imported by: 0

README

Shutdown

Provides basic support for graceful shutdown.

The main goroutine will likely use WaitForSignal():

func main() {
  c, cf := ctx.Background()
  // your code here
  shutdown.WaitForSignal(c, cf)
}

While the other goroutines will likely keep the shutdown from completing until they are properly closed:

func work(c ctx.C) {
  defer shutdown.Hold().Release() // obtain a hold immediately, and defer the release

  for {
    // main loop
    select {
    case <-c.Done():
      // abort

    case <-shutdown.Started():
      // graceful close and then

    case data, ok := <-io:
      if !ok {
        return // EOF
      }
      // do work
    }
  }
}

WaitForSignal()

It blocks until there are no more active Hold()s, or 3 signals (INT, TERM or QUIT) has been detected.

When the first signal is detected calls Begin()

Begin()

It begins the shutdown procedure. Anyone waiting for <-shutdown.Started() will be notified.

Started()

Returns a channel which will be closed when the shutdown Begin()

Hold().Release()

return a hold which prevents the shutdown to complete until it is Released()

Started5Sec()

Similar to Started() but returns the channel returned will be closed 5 seconds later

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Err = errors.New("shutdown")

Use to signal that an operation can't be executed because the system is shutting down

Functions

func Begin

func Begin()

start a global shutdown unless already started

func Done

func Done() <-chan struct{}

returns a channel that will close when the shutdown has completed

func Started

func Started() <-chan struct{}

return a closed channel when the shutdown has started

func Started5Sec

func Started5Sec() <-chan struct{}

return a closed channel 5 seconds after the shutdown has started

func WaitForSignal

func WaitForSignal(c ctx.C, cf ctx.CancelFunc)

setup signals and wait for the shutdown to complete

Types

type ReleaseFn

type ReleaseFn func()

func Hold

func Hold() ReleaseFn

prevent the shutdown to complete until released

func HoldAndWait

func HoldAndWait() ReleaseFn

prevents the shutdown to complete until released, and also wait for the shutdown to start

func (ReleaseFn) Release

func (fn ReleaseFn) Release()

Jump to

Keyboard shortcuts

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