graceful

package module
v0.0.0-...-2158936 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: AGPL-3.0 Imports: 11 Imported by: 0

README

Graceful shutdown

[[TOC]]

Goal

This library aims to make graceful shutdown easy and safe. It has some helpers to make setup easier.

Helpers

Graceful
  • GRPCServer - pass *grpc.Server and it will stop it gracefully. New connections are not accepted, blocks until existing connections finish.
  • HTTPServer - pass *http.Server and it will stop it gracefully. New connections are not accepted, blocks until existing connections finish.
  • PubSubTopic - pass *pubsub.Topic and it will stop it gracefully. Publishes all remaining messages to publish.
  • Centrifuge - pass *centrifuge.Node and it will stop it gracefully. New connections are not accepted, existing ones are disconnected with shutdown reason.
  • Redis - pass *redis.Client and it will stop it gracefully.
  • Logger - pass *zap.SugaredLogger and it will flush all buffered messages. Useful to ensure no logs are missing in logs explorer.
  • WaitGroup - pass *sync.WaitGroup and it will wait for waitgroup to finish.
  • Tracer - exports all remaining tracing spans.
Graceless
  • Context - pass context.CancelFunc and it will be called. Useful to cancel root context after all servers were stopped to ensure nothing is still invoking.
  • GRPCClient - pass *grpc.ClientConn and it will stop it.
  • Spanner - pass *spanner.Client and it will close all connections.

Example

package main

import (
	"context"
	"os"
	"runtime"
	"syscall"
	"time"

	"github.com/go-redis/redis/v9"
	"gitlab.com/picnic-app/backend/libs/golang/graceful"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	rc := redis.NewClient(&redis.Options{})
	rc.Get(ctx, "blabla")

	shutdown := graceful.New(
		&graceful.ShutdownManagerOptions{
			Timeout: time.Second * 15,
		},
		graceful.Parallel(
			&graceful.ParallelShutdownOptions{
				Name:    "servers",
				Timeout: time.Second * 30,
			},
		),
		graceful.Parallel(
			&graceful.ParallelShutdownOptions{
				Name:    "long tasks",
				Timeout: time.Second * 5,
			},
		),
		graceful.Context(cancel),
		graceful.Logger(nil),
		graceful.Parallel(
			&graceful.ParallelShutdownOptions{
				Name:    "clients",
				Timeout: time.Second * 5,
			},
			graceful.Redis(rc),
		),
	)
	shutdown.RegisterSignals(os.Interrupt, syscall.SIGTERM)

	time.Sleep(time.Second * 5)
	_ = shutdown.Shutdown(context.Background())

	for {
		runtime.Gosched()
	}
}

Configure access to private modules on gitlab

This notion page explains how to configure local environment to work with private modules stored in Picnic gitlab repository.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IBigQueryClient

type IBigQueryClient interface {
	Close() error
}

IBigQueryClient is an interface for shutting down *bigquery.Client.

type IBigtableClient

type IBigtableClient interface {
	Close() error
}

IBigtableClient is an interface for shutting down *bigtable.Client.

type ICentrifugeNode

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

ICentrifugeNode is an interface for shutting down *centrifuge.Node.

type IGRPCClientConn

type IGRPCClientConn interface {
	Close() error
}

IGRPCClientConn is an interface for shutting down *grpc.ClientConn.

type IGRPCServer

type IGRPCServer interface {
	GracefulStop()
}

IGRPCServer is an interface for shutting down *grpc.Server.

type IHTTPServer

type IHTTPServer interface {
	Shutdown(ctx context.Context) error
}

IHTTPServer is an interface for shutting down *http.Server.

type IPubsubTopic

type IPubsubTopic interface {
	Stop()
}

IPubsubTopic is an interface for shutting down *pubsub.Topic.

type IRedisClient

type IRedisClient interface {
	Close() error
}

IRedisClient is an interface for shutting down *redis.Client.

type ISpannerClient

type ISpannerClient interface {
	Close()
}

ISpannerClient is an interface for shutting down *spanner.Client.

type ParallelShutdownOptions

type ParallelShutdownOptions struct {
	Name    string
	Timeout time.Duration
}

ParallelShutdownOptions is an options structure for parallel Shutdowner.

type ShutdownContextErrorFunc

type ShutdownContextErrorFunc func(context.Context) error

ShutdownContextErrorFunc is a function that implements Shutdowner interface.

func Centrifuge

Centrifuge returns a ShutdownContextErrorFunc that gracefully stops centrifuge node.

func HTTPServer

func HTTPServer(server IHTTPServer) ShutdownContextErrorFunc

HTTPServer returns a ShutdownContextErrorFunc that gracefully stops HTTP server.

func Tracer

func Tracer() ShutdownContextErrorFunc

Tracer returns a ShutdownContextErrorFunc that gracefully shutdowns tracer exporter.

func (ShutdownContextErrorFunc) Shutdown

Shutdown runs the function itself.

func (ShutdownContextErrorFunc) String

func (s ShutdownContextErrorFunc) String() string

String returns just dummy value.

type ShutdownContextFunc

type ShutdownContextFunc func(ctx context.Context)

ShutdownContextFunc is a function that implements Shutdowner interface.

func (ShutdownContextFunc) Shutdown

func (s ShutdownContextFunc) Shutdown(ctx context.Context) error

Shutdown runs the function itself. It always returns nil as the original function doesn't return any error.

func (ShutdownContextFunc) String

func (s ShutdownContextFunc) String() string

String returns just dummy value.

type ShutdownErrorFunc

type ShutdownErrorFunc func() error

ShutdownErrorFunc is a function that implements Shutdowner interface.

func BigQuery

func BigQuery(client IBigQueryClient) ShutdownErrorFunc

BigQuery returns a ShutdownErrorFunc that stops BigQuery client.

func Bigtable

func Bigtable(client IBigtableClient) ShutdownErrorFunc

Bigtable returns a ShutdownErrorFunc that stops Bigtable client.

func Context

func Context(cancel context.CancelFunc) ShutdownErrorFunc

Context returns a ShutdownErrorFunc that cancels the context.

func GRPCClient

func GRPCClient(client IGRPCClientConn) ShutdownErrorFunc

GRPCClient returns a ShutdownErrorFunc that stops GRPC client.

func GRPCServer

func GRPCServer(server IGRPCServer) ShutdownErrorFunc

GRPCServer returns a ShutdownErrorFunc that gracefully stops GRPC server.

func Logger

func Logger(sugaredLogger *zap.SugaredLogger) ShutdownErrorFunc

Logger returns a ShutdownErrorFunc that flushes logger. nil uses default logger.

func PubSubTopic

func PubSubTopic(topic IPubsubTopic) ShutdownErrorFunc

PubSubTopic returns a ShutdownErrorFunc that gracefully stops publishing into PubSub Topic.

func Redis

func Redis(client IRedisClient) ShutdownErrorFunc

Redis returns a ShutdownErrorFunc that gracefully stops redis client.

func Sleep

func Sleep(duration time.Duration) ShutdownErrorFunc

Sleep returns a ShutdownErrorFunc that just sleeps. Useful for delays and debug purposes.

func Spanner

func Spanner(client ISpannerClient) ShutdownErrorFunc

Spanner returns a ShutdownErrorFunc that stops Spanner client.

func WaitGroup

func WaitGroup(wg *sync.WaitGroup) ShutdownErrorFunc

WaitGroup returns a ShutdownErrorFunc that waits WaitGroup.

func (ShutdownErrorFunc) Shutdown

func (s ShutdownErrorFunc) Shutdown(ctx context.Context) error

Shutdown runs the function itself. It returns early in case of context timeout but original function is not canceled and will run even after canceling context.

func (ShutdownErrorFunc) String

func (s ShutdownErrorFunc) String() string

String returns just dummy value.

type ShutdownManager

type ShutdownManager interface {
	Shutdowner
	RegisterSignals(...os.Signal)
	AddStep(Shutdowner)
}

ShutdownManager is a manager that is a Shutdowner and can handle signals.

func New

New returns a new ShutdownManager.

type ShutdownManagerOptions

type ShutdownManagerOptions struct {
	Timeout time.Duration
}

ShutdownManagerOptions is an options structure for ShutdownManager.

type Shutdowner

type Shutdowner interface {
	Shutdown(context.Context) error
	fmt.Stringer
}

Shutdowner is an interface that gracefully shuts down something.

func Parallel

func Parallel(opts *ParallelShutdownOptions, funcs ...Shutdowner) Shutdowner

Parallel returns a Shutdowner that executes underlying Shutdowner's in parallel.

Jump to

Keyboard shortcuts

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