graceful

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2020 License: ISC Imports: 7 Imported by: 3

README

Go Doc Build Status Go Report Card Test Coverage Maintainability

Graceful

Graceful is a minimal package for building a graceful service. It implements an opinionated workflow for:

  • Gracefully opening long-lived connections
  • Gracefully retrying dropped connections
  • Gracefully starting servers to listen
  • Gracefully terminating all connections and listeners

A client is any kind of long-lived connection that needs to be preserved during a service's operation (gRPC connection, database connection, message queue connection, etc.). A server is any kind of listener that needs to listen to a port and serve requests (HTTP server, gRPC server, etc.).

Quick Start

package main

import (
  "os"

  "github.com/moorara/graceful"
)

func main() {
  logger := &Logger{}
  dbClient := NewClient("db-client")
  queueClient := NewClient("queue-client")
  apiServer := NewServer("api-server", 8080)
  infoServer := NewServer("info-server", 8081)

  graceful.SetLogger(logger)
  graceful.RegisterClient(dbClient, queueClient)
  graceful.RegisterServer(apiServer, infoServer)
  code := graceful.StartAndWait()

  os.Exit(code)
}

You can find the complete example here.

Behaviour

Here is how the StartAndWait behaves:

  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.

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 added in v0.1.1

func RegisterClient(c ...Client)

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

func RegisterServer added in v0.1.1

func RegisterServer(s ...Server)

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

func SetGracePeriod added in v0.1.1

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 added in v0.1.1

func SetLogger(l Logger)

SetLogger enables logging.

func SetMaxRetry added in v0.1.1

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 added in v0.1.1

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(template string, args ...interface{})
	Infof(template string, args ...interface{})
	Warnf(template string, args ...interface{})
	Errorf(template string, args ...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
examples

Jump to

Keyboard shortcuts

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