graceful

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2023 License: MIT Imports: 7 Imported by: 0

README

graceful

Package graceful is a Go 1.8+ package enabling graceful shutdown of http.Handler servers.

Usage

Simply use ListenAndServe to create a http server:

package main

import (
	"log"

	"github.com/ETZhangSX/graceful"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	if err := graceful.ListenAndServe(":8080", r.Handler()); err != nil {
		log.Fatal(err)
	}
}

The default timeout of shutdown is 5 seconds. You can configure it by using WithShutdownTimeout

func main() {
	...
	...
	
	if err := graceful.ListenAndServe(
		":8080",
		handler,
		graceful.WithShutdownTimeout(10*time.Second),
		// add customer function before shutting down
		graceful.WithShutdownFunc(func() {
			log.Info("Http Server is shutting down...")
        }
	); err != nil {
		log.Fatal(err)
	}
}

Documentation

Overview

Package graceful is a Go 1.8+ package enabling graceful shutdown of http.Handler servers.

Usage:

package main

import (
    "log"

    "github.com/ETZhangSX/graceful"
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()
    if err := graceful.ListenAndServe(":8080", r.Handler()); err != nil {
        log.Fatal(err)
    }
}

Index

Constants

View Source
const DefaultShutdownTimeout = 5 * time.Second

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(addr string, handler http.Handler, opts ...Option) error

ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

The handler is typically nil, in which case the DefaultServeMux is used. ShutdownTimeout defaults 5 seconds.

func ListenAndServeTLS

func ListenAndServeTLS(addr, certFile, keyFile string, handler http.Handler, opts ...Option) error

ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate. ShutdownTimeout defaults 5 seconds.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option interface

func WithShutdownFunc

func WithShutdownFunc(fs ...func()) Option

WithShutdownFunc registers function(s) running while shutting down by calling s.RegisterOnShutdown.

func WithShutdownTimeout

func WithShutdownTimeout(timeout time.Duration) Option

WithShutdownTimeout set timeout for shutting down server

type Server

type Server struct {
	*http.Server

	// ShutdownTimeout is the maximum duration for shutting down the server.
	// A zero or negative value means there will be no timeout.
	ShutdownTimeout time.Duration
	// contains filtered or unexported fields
}

A Server defines parameters for gracefully running an HTTP server. The zero value for Server is a valid configuration.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(opts ...Option) error

ListenAndServe listens on the TCP network address s.Addr and then calls s.Server.ListenAndServe to handle requests on incoming connections.

func (*Server) ListenAndServeTLS

func (s *Server) ListenAndServeTLS(certFile, keyFile string, opts ...Option) error

ListenAndServeTLS listens on the TCP network address srv.Addr and then calls ServeTLS to handle requests on incoming TLS connections. Accepted connections are configured to enable TCP keep-alives.

Jump to

Keyboard shortcuts

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