gracefulshutdown

package
v1.1.10 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2022 License: MIT Imports: 6 Imported by: 3

README

Gracefulshutdown

fiber-modules/gracefulshutdown is a module for providing a grace period between signaling server to shutdown and actual server closing. In between you can handle pending requests and call closing functions ShutdownFns.

Usage

import  "github.com/gkampitakis/fiber-modules/gracefulshutdown"

app := fiber.New()

...

gracefulshutdown.Listen(app, "localhost:8080", gracefulshutdown.WithShutdownFns([]func() error{
  func() error {
      return db.Close()
  }
}))
Options

fiber-modules/gracefulshutdown can be configured with three ways:

// Registers gracefulshutdown with default config
gracefulshutdown.Listen(app, "localhost:8080", gracefulshutdown.Default())

// Registers gracefulshutdown with default config and passes ShutdownFns
gracefulshutdown.Listen(app, "localhost:8080", gracefulshutdown.WithShutdownFns())

// or the last method where you can pass the config as you please
gracefulshutdown.Listen(app, "localhost:8080", gracefulshutdown.Config{...})

The default values that get applied are:

  • Default() sets Period to 15 seconds and Enabled to true
  • WithShutdownFns() sets Period to 15 seconds and Enabled to true and allows you to pass

With gracefulshutdown.Config{} you can pass:

  • Period Time to wait before shutting down the server

  • Enabled Intercept os Signals and provide graceful shutdown when true

  • ShutdownFns Functions that will execute before shutting down server

    Make sure Period time is enough for functions to run. Each functions is executed in a separate goroutine

  • ListenErrorHandler Function that handles the error returned from app.Listen. By default calls log.Fatal

  • Signals Signals that get intercepted. Default syscall.SIGKILL, syscall.SIGINT, syscall.SIGTERM,

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Listen

func Listen(app *fiber.App, addr string, cfg Config)
Starts fiber server

Calls app.Listen if "Enabled" intercepts os.Signals and provides some period for handling requests and calling closing functions and closing connections

Types

type Config

type Config struct {
	// Time to wait before shutting down the server
	Period time.Duration
	// Intercept os Signals and provide graceful shutdown
	Enabled bool
	// Functions that will execute before shutting down server
	// E.g. Close db connections
	ShutdownFns []func() error
	// Function that handles the error returned from app.Listen
	// By default calls log.Fatal
	ListenErrorHandler func(error)
	// Signals that get intercepted
	// Default syscall.SIGKILL, syscall.SIGINT, syscall.SIGTERM,
	Signals []os.Signal
}

func Default

func Default() Config
Default values

Period: 15

Enabled: true

func WithShutdownFns

func WithShutdownFns(fns []func() error) Config

Returns a default Config and sets ShutdownFns

Period: 15

Enabled: true

Jump to

Keyboard shortcuts

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