graceful

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2023 License: ISC Imports: 7 Imported by: 0

Documentation

Overview

Package graceful provides graceful start, graceful retry, and graceful stop! It can be used for:

  • Gracefully starting servers (http, grpc, etc.) and clients (external services, databases, message queues, etc.).
  • Gracefully retrying lost connections to external services, databases, message queues, etc.
  • Gracefully stopping servers (http, grpc, etc.) and clients (external services, databases, message queues, etc.).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterClient

func RegisterClient(c ...Client)

RegisterClient registers new clients (external services, databases, message queues, etc.).

func RegisterServer

func RegisterServer(s ...Server)

RegisterServer registers new servers (http, grpc, etc.).

func SetGracePeriod

func SetGracePeriod(d time.Duration)

SetGracePeriod sets the timeout for gracefully stopping servers and clients. If the timeout reaches, the process will be terminated ungracefully. The default grace period is 30 seconds.

func SetLogger

func SetLogger(l Logger)

SetLogger enables logging.

func SetMaxRetry

func SetMaxRetry(n int)

SetMaxRetry sets the maximum number of retries. When a client connection is lost, the library retries to establish the connection. The default maximum retry is 5.

func StartAndWait

func StartAndWait() int

StartAndWait behaves as follows:

  1. It tries to connect all clients each in a new goroutine. - If a client fails to connect, it will be automatically retried for a limited number of times with exponential backoff. - If at least one client fails to connect (after retries), a graceful termination will be initiated.
  2. Once all clients are connected successfully, all servers start listening each in a new goroutine. - If any server errors, a graceful termination will be initiated.
  3. Then, this method blocks the current goroutine until one of the following conditions happen: - If any of SIGHUP, SIGINT, SIGQUIT, SIGTERM signals is sent, a graceful termination will be initiated. - If any of the above signals is sent for the second time before the graceful termination is completed, the process will exit immediately with an error code.

Types

type Client

type Client interface {
	fmt.Stringer
	Connect() error
	Disconnect(context.Context) error
}

Client is the generic interface for a client (external service, database, message queue, etc.).

type Logger

type Logger interface {
	Debugf(string, ...interface{})
	Infof(string, ...interface{})
	Warnf(string, ...interface{})
	Errorf(string, ...interface{})
}

Logger is a simple interface for logging.

type Server

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

Server is the generic interface for a server (http, grpc, etc.).

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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