interrupts

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package interrupts exposes helpers for graceful handling of interrupt signals

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Context

func Context() context.Context

Context returns a context that is cancelled when an interrupt hits. Using this context is a weak guarantee that your work will finish before process exit as callers cannot signal that they are finished. Prefer to use Run().

func ListenAndServe

func ListenAndServe(server ListenAndServer, gracePeriod time.Duration)

ListenAndServe runs the HTTP server and handles shutting it down gracefully on interrupts. This function is not blocking. Callers are expected to exit only after WaitForGracefulShutdown returns to ensure all servers have had time to shut down.

func ListenAndServeTLS

func ListenAndServeTLS(server *http.Server, certFile, keyFile string, gracePeriod time.Duration)

ListenAndServeTLS runs the HTTP server and handles shutting it down gracefully on interrupts. This function is not blocking. Callers are expected to exit only after WaitForGracefulShutdown returns to ensure all servers have had time to shut down.

func OnInterrupt

func OnInterrupt(work func())

OnInterrupt ensures that work is done when an interrupt is fired and that we wait for the work to be finished before we consider the process cleaned up. This function is not blocking.

func Run

func Run(work func(ctx context.Context))

Run will do work until an interrupt is received, then signal the worker. This function is not blocking. Callers are expected to exit only after WaitForGracefulShutdown returns to ensure all workers have had time to shut down. This is preferable to getting the raw Context as we can ensure that the work is finished before releasing our share of the wait group on shutdown.

func Terminate

func Terminate()

Terminate can be called to trigger a termination to the current process.

func Tick

func Tick(work func(), interval func() time.Duration)

Tick will do work on a dynamically determined interval until an interrupt is received. This function is not blocking. Callers are expected to exit only after WaitForGracefulShutdown returns to ensure all workers have had time to shut down.

func TickLiteral

func TickLiteral(work func(), interval time.Duration)

TickLiteral runs Tick with an unchanging interval.

func WaitForGracefulShutdown

func WaitForGracefulShutdown()

WaitForGracefulShutdown waits until all registered servers and workers have had time to gracefully shut down, or times out. This function is blocking.

Types

type ListenAndServer

type ListenAndServer interface {
	Shutdownable
	ListenAndServe() error
}

ListenAndServer is typically an http.Server

type Shutdownable

type Shutdownable interface {
	Shutdown(context.Context) error
}

Shutdownable is typically an http.Server

Jump to

Keyboard shortcuts

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