server

package
v0.0.0-...-68c1abf Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2019 License: MIT Imports: 6 Imported by: 0

README

server

-- import "github.com/rickbassham/example-go/chiapi/server"

Package server contains an HTTP server implementation that supports graceful shutdowns. By gracefully shutting down, we allow existing requests to finish before shutting the server down.

Usage

type GracefulHTTPServer
type GracefulHTTPServer struct {
}

GracefulHTTPServer will run a new http server that supports graceful shutdown.

func NewGracefulHTTPServer
func NewGracefulHTTPServer(log *zap.Logger, svr HTTPServer, l net.Listener, timeout time.Duration) *GracefulHTTPServer

NewGracefulHTTPServer creates a new GracefulHTTPServer.

func (*GracefulHTTPServer) Run
func (s *GracefulHTTPServer) Run() error

Run starts the http server, waiting for a SIGINT signal to the process. Once it has received one of those, or if it fails to start at all, the function will return. If we are shutting down due to a signal, we call Shutdown on the http.Server we are using. This function will wait for existing requests to be completed before returning. We only wait up to 30 seconds to do the graceful shutdown. After that, we just kill the connections.

type HTTPServer
type HTTPServer interface {
	Serve(l net.Listener) error
	Shutdown(ctx context.Context) error
}

HTTPServer is an interface that the go http.Server struct will satisfy.

Documentation

Overview

Package server contains an HTTP server implementation that supports graceful shutdowns. By gracefully shutting down, we allow existing requests to finish before shutting the server down.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GracefulHTTPServer

type GracefulHTTPServer struct {
	// contains filtered or unexported fields
}

GracefulHTTPServer will run a new http server that supports graceful shutdown.

func NewGracefulHTTPServer

func NewGracefulHTTPServer(log *zap.Logger, svr HTTPServer, l net.Listener, timeout time.Duration) *GracefulHTTPServer

NewGracefulHTTPServer creates a new GracefulHTTPServer.

func (*GracefulHTTPServer) Run

func (s *GracefulHTTPServer) Run() error

Run starts the http server, waiting for a SIGINT signal to the process. Once it has received one of those, or if it fails to start at all, the function will return. If we are shutting down due to a signal, we call Shutdown on the http.Server we are using. This function will wait for existing requests to be completed before returning. We only wait up to 30 seconds to do the graceful shutdown. After that, we just kill the connections.

type HTTPServer

type HTTPServer interface {
	Serve(l net.Listener) error
	Shutdown(ctx context.Context) error
}

HTTPServer is an interface that the go http.Server struct will satisfy.

Jump to

Keyboard shortcuts

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