lifecycle

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2021 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Copyright © 2021 Optable Technologies Inc. All rights reserved. See LICENSE for details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MaybeGracefullShutdown

func MaybeGracefullShutdown(ctx context.Context, i interface{}) error

MaybeGracefulShutdown takes an object and invokes Shutdown if the object implements GracefulShutdown. This function exists to avoid forcing every interface to also implement this.

This is often useful when builder functions, e.g. NewX() -> X returns an interface where not all implementations implements GracefulShutdown. It avoids leaking the GracefulShutdown in all interface.

Types

type GracefulShutdown

type GracefulShutdown interface {
	// Shutdown context should be respected.
	Shutdown(context.Context) error
}

GracefulShutdown is implemented by components that need to be gracefully terminated, e.g. flushing buffers to permanent storage. Shutdown is traditionally done in multiple fashions:

  1. By passing a context to the builder object, e.g. `NewGizmo(ctx, ...)`. and cancelling the context to stop internal state (goroutines).
  2. By providing a `Close() error` method.

The first method suffers from multiple issues:

  • No signal *when* the component is done shutting down.
  • No success status on the shutdown.

The second method (and also the first) suffers from indeterminate timeout; how long do we wait for Close to return?

By implementing the GracefulShutdown interface, a component or a service, can implement graceful shutdown and have parent (main process) know about the lifecycle of the components. It also ties nicely with Kubernetes' via `terminationGracePeriodSeconds`.

Jump to

Keyboard shortcuts

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