Documentation ¶
Overview ¶
Copyright © 2021 Optable Technologies Inc. All rights reserved. See LICENSE for details.
Index ¶
- func GracefulShutdownGrpcServer(ctx context.Context, server *grpc.Server) error
- func MaybeGracefulShutdown(ctx context.Context, i interface{}) error
- func ServeGRPCAndHTTP(ctx context.Context, l net.Listener, handler http.Handler, server *grpc.Server, ...) <-chan error
- func ServeGRPCAndMetrics(ctx context.Context, l net.Listener, server *grpc.Server, ...) <-chan error
- func ServeWithGracefulShutdown(ctx context.Context, listen net.Listener, server Servable, ...) <-chan error
- type GracefulShutdown
- type Servable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GracefulShutdownGrpcServer ¶ added in v0.5.0
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
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:
- By passing a context to the builder object, e.g. `NewGizmo(ctx, ...)`. and cancelling the context to stop internal state (goroutines).
- 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`.