package
0.0.0-20230828071713-ffae65984116
Repository: https://github.com/ronak-searce/graph-api.git
Documentation: pkg.go.dev

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

# Functions

BigQuery returns a ShutdownErrorFunc that stops BigQuery client.
Bigtable returns a ShutdownErrorFunc that stops Bigtable client.
Centrifuge returns a ShutdownContextErrorFunc that gracefully stops centrifuge node.
Context returns a ShutdownErrorFunc that cancels the context.
GRPCClient returns a ShutdownErrorFunc that stops GRPC client.
GRPCServer returns a ShutdownErrorFunc that gracefully stops GRPC server.
HTTPServer returns a ShutdownContextErrorFunc that gracefully stops HTTP server.
Logger returns a ShutdownErrorFunc that flushes logger.
New returns a new ShutdownManager.
Parallel returns a Shutdowner that executes underlying Shutdowner's in parallel.
PubSubTopic returns a ShutdownErrorFunc that gracefully stops publishing into PubSub Topic.
Redis returns a ShutdownErrorFunc that gracefully stops redis client.
Sleep returns a ShutdownErrorFunc that just sleeps.
Spanner returns a ShutdownErrorFunc that stops Spanner client.
Tracer returns a ShutdownContextErrorFunc that gracefully shutdowns tracer exporter.
WaitGroup returns a ShutdownErrorFunc that waits WaitGroup.

# Structs

ParallelShutdownOptions is an options structure for parallel Shutdowner.
ShutdownManagerOptions is an options structure for ShutdownManager.

# Interfaces

IBigQueryClient is an interface for shutting down *bigquery.Client.
IBigtableClient is an interface for shutting down *bigtable.Client.
ICentrifugeNode is an interface for shutting down *centrifuge.Node.
IGRPCClientConn is an interface for shutting down *grpc.ClientConn.
IGRPCServer is an interface for shutting down *grpc.Server.
IHTTPServer is an interface for shutting down *http.Server.
IPubsubTopic is an interface for shutting down *pubsub.Topic.
IRedisClient is an interface for shutting down *redis.Client.
ISpannerClient is an interface for shutting down *spanner.Client.
Shutdowner is an interface that gracefully shuts down something.
ShutdownManager is a manager that is a Shutdowner and can handle signals.

# Type aliases

ShutdownContextErrorFunc is a function that implements Shutdowner interface.
ShutdownContextFunc is a function that implements Shutdowner interface.
ShutdownErrorFunc is a function that implements Shutdowner interface.