gracedown

package module
v0.0.0-...-b19e746 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2023 License: MIT Imports: 6 Imported by: 6

README

go-gracedown

Package go-gracedown provides a library that makes it easy to build a HTTP server that can be shutdown gracefully (that is, without dropping any connections).

ARCHIVED!

This project is no longer maintained. Use https://pkg.go.dev/net/http#Server.Shutdown instead it.

SYNOPSIS

import (
  "os"
  "os/signal"

  "github.com/shogo82148/go-gracedown"
)

func main() {
  go func() {
    for {
      s := <-signal_chan
        if s == syscall.SIGTERM {
          gracedown.Close() // trigger graceful shutdown
        }
    }
  }()

  handler := MyHTTPHandler()
  gracedown.ListenAndServe(":7000", handler)
}

built-in graceful shutdown support (from Go version 1.8 onward)

From Go version 1.8 onward, the HTTP Server has support for graceful shutdown. (HTTP Server Graceful Shutdown) The go-gracedown package is just a wrapper of the net/http package to maintain interface compatibility. So you should use the "net/http".Server.Shutdown method and "net/http".Server.Close method directly.

Changes
  • Go version 1.7 or less: The grace.Server.Close method keeps currently-open connections.
  • From Go version 1.8 onward: The grace.Server.Close method drops currently-open connections.

GODOC

See godoc for more information.

SEE ALSO

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() bool

Close shuts down the default server used by ListenAndServe, ListenAndServeTLS and Serve. It returns true if it's the first time Close is called.

func ListenAndServe

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

ListenAndServe provides a graceful version of the function provided by the net/http package. Call Close() to stop the server.

func ListenAndServeTLS

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

ListenAndServeTLS provides a graceful version of the function provided by the net/http package. Call Close() to stop the server.

func Serve

func Serve(l net.Listener, handler http.Handler) error

Serve provides a graceful version of the function provided by the net/http package. Call Close() to stop the server.

Types

type Server

type Server struct {
	*http.Server

	KillTimeOut time.Duration
	// contains filtered or unexported fields
}

Server provides a graceful equivalent of net/http.Server.

func NewWithServer

func NewWithServer(s *http.Server) *Server

NewWithServer wraps an existing http.Server.

func (*Server) Close

func (srv *Server) Close() bool

Close shuts down the default server used by ListenAndServe, ListenAndServeTLS and Serve. It returns true if it's the first time Close is called.

func (*Server) Serve

func (srv *Server) Serve(l net.Listener) error

Jump to

Keyboard shortcuts

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