lifecycle

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2022 License: Apache-2.0 Imports: 14 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 GracefulShutdownGrpcServer added in v0.5.0

func GracefulShutdownGrpcServer(ctx context.Context, server *grpc.Server) error

GracefulShutdownGrpcServer gracefully stops a grpc.Server by invoking first the GracefulStop method, and then waiting for completion or until the cancel timeouts; in such case the server is immediately shutdown in a non graceful fashion by calling the Stop method.

func MaybeGracefulShutdown added in v0.5.0

func MaybeGracefulShutdown(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.

The function also knows how to handle graceful shutdown of grpc.Server objects which exposes the GracefulStop/Stop methods.

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.

func ServeGRPCAndHTTP added in v0.7.0

func ServeGRPCAndHTTP(ctx context.Context, l net.Listener, handler http.Handler, server *grpc.Server, shutdownTimeout time.Duration) <-chan error

ServeGRPCAndHTTP behaves like ServeWithGracefulShutdown excepts that it also starts an HTTP1 service on the same Listener to expose metrics.

func ServeGRPCAndMetrics added in v0.7.0

func ServeGRPCAndMetrics(ctx context.Context, l net.Listener, server *grpc.Server, shutdownTimeout time.Duration) <-chan error

ServeGRPCAndMetrics behaves like ServeWithGracefulShutdown excepts that it also starts a prometheus HTTP1 service on the same Listener to expose metrics.

func ServeWithGracefulShutdown added in v0.5.0

func ServeWithGracefulShutdown(ctx context.Context, listen net.Listener, server Servable, shutdownTimeout time.Duration) <-chan error

ServeWithGracefulShutdown glue a Servable with a proper shutdown routine. register signals to trigger a proper shutdown sequence. This function does not block and returns immediately a channel where an error will be emitted if it failed to serve, or the returned shutdown error (or nil if none).

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`.

type Servable added in v0.5.0

type Servable interface {
	Serve(net.Listener) error
}

Servable is implemented by components that listen and serve requests. The most notable type implementing this are http.Server and grpc.Server.

Jump to

Keyboard shortcuts

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