gsserver

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2023 License: MIT Imports: 5 Imported by: 0

README

Implementing Graceful HTTP Server Shutdown

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option added in v1.2.0

type Option func(server *http.Server)

type Server

type Server struct {
	Signals []os.Signal
	Mux     http.Handler
	Addr    string
}

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(ctx context.Context, options ...Option) error

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

Farther it listens context Done channel and then calls Shutdown for the http server

Example
srv := Server{
	Mux:  http.NewServeMux(),
	Addr: ":8111",
}
srv.SetSignals(os.Interrupt, syscall.SIGTERM)

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

// starting server
go func() {
	_ = srv.ListenAndServe(context.Background())
}()

// sending the signal
time.Sleep(time.Microsecond * 200)
_ = syscall.Kill(syscall.Getpid(), syscall.SIGTERM)

<-c
Output:

func (*Server) SetSignals

func (s *Server) SetSignals(sig ...os.Signal)

SetSignals sets list of signals to be notified

Example
server := Server{}
server.SetSignals(os.Interrupt, syscall.SIGTERM)
Output:

Jump to

Keyboard shortcuts

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