gracehttp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: MIT Imports: 9 Imported by: 0

README

gracehttp

A wrapper around the net/http.Server that can be shutdown gracefully.

To read more about graceful shutdowns, see this blog post.

Installation

go get github.com/josestg/gracehttp

Usage

package main

import (
    "log/slog"
    "net/http"
    "os"
    "syscall"
    "time"

    "github.com/josestg/gracehttp"
)

func main() {
    log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{}))

    serv := http.Server{Addr: ":8081"}
    opts := []gracehttp.Option{
        gracehttp.WithLogger(log),
        gracehttp.WithSignals(syscall.SIGINT, syscall.SIGTERM), // default is syscall.SIGINT, syscall.SIGTERM
        gracehttp.WithWaitTimeout(10 * time.Second),            // default is 5 seconds
    }

    gs := gracehttp.NewGracefulShutdownServer(&serv, opts...)
    log.Info("server started")
    defer log.Info("server stopped")

    if err := gs.ListenAndServe(); err != nil {
        log.Error("server failed to start", "err", err)
        os.Exit(1)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GracefulShutdown

type GracefulShutdown struct {
	Server
	// contains filtered or unexported fields
}

GracefulShutdown is a wrapper of http.Server that can be shutdown gracefully.

func NewGracefulShutdownServer

func NewGracefulShutdownServer(srv Server, opts ...Option) *GracefulShutdown

NewGracefulShutdownServer wraps the given server with graceful shutdown capability mechanism. By default, the server will listen to SIGINT and SIGTERM signals to initiate shutdown and wait for all active connections to be closed. If still active connections after wait timeout exceeded, it will force close the server. The default wait timeout is 5 seconds.

References: - https://blog.stackademic.com/graceful-shutdown-in-go-820d28e1d1c4

func (*GracefulShutdown) ListenAndServe

func (gs *GracefulShutdown) ListenAndServe() error

ListenAndServe starts listening and serving the server gracefully.

type Option

type Option func(*GracefulShutdown)

Option is the type for GracefulShutdown options.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger for the GracefulShutdown.

func WithSignals

func WithSignals(signals ...os.Signal) Option

WithSignals sets the signals to listen for initiating graceful shutdown.

func WithWaitTimeout

func WithWaitTimeout(d time.Duration) Option

WithWaitTimeout sets the wait timeout for graceful shutdown.

type Server

type Server interface {
	// ListenAndServe starts listening and serving the server.
	// This method should block until shutdown signal received or failed to start.
	ListenAndServe() error

	// Shutdown gracefully shuts down the server, it will wait for all active connections to be closed.
	Shutdown(ctx context.Context) error

	// Close force closes the server.
	// Close is called when Shutdown timeout exceeded.
	Close() error
}

Server defines a minimum methods that required for graceful shutdown procedure.

Jump to

Keyboard shortcuts

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