gracehttp

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

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

Go to latest
Published: Feb 7, 2021 License: ISC Imports: 11 Imported by: 1

README

gracefield

A small library to provide cleaner graceful HTTP shutdown.

Example

Refer to the documentation's example section.

Documentation

Overview

Example
package main

import (
	"io"
	"net/http"
	"time"

	"github.com/diamondburned/gracehttp"
)

const addr = "unix:///tmp/gracehttp-example.sock"

func main() {
	// ListenAndServeAsync will only block until the listener is being served.
	server := gracehttp.ListenAndServeAsync(addr, http.HandlerFunc(handle))
	defer server.ShutdownTimeout(5 * time.Second)

	time.Sleep(time.Second)
}

func handle(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "Hello, 世界")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAddr

func ListenAddr(addr string) (net.Listener, error)

ListenAddr parses the given address and returns either a TCP or Unix listener. Below lists acceptable formats.

unixpacket://relative/path/to.socket
unix:///path/to.socket
/path/to.socket

http://address
tcp4://address
tcp6://address
tcp://address
address

func ListenAddrCfg

func ListenAddrCfg(ctx context.Context, addr string, cfg net.ListenConfig) (net.Listener, error)

ListenAddrCfg is ListenAddr with additional parameters.

func WaitForInterrupt

func WaitForInterrupt() os.Signal

WaitForInterrupt blocks until an interrupt signal is received.

Types

type Server

type Server struct {
	*http.Server
}

Server is the wrapper around a regular net/http server. It is not thread-safe, and must never be shared across goroutines.

func ListenAndServeAsync

func ListenAndServeAsync(addr string, h http.Handler) *Server

ListenAndServeAsync listens and serves the given address. The returned server can be used to gracefully shut down the server.

func MustServer

func MustServer(addr string, h http.Handler) *Server

MustServer creates a new server and panics if it fails.

func NewCustomServer

func NewCustomServer(h1 *http.Server, h2 *http2.Server) (*Server, error)

NewCustomServer creates a new graceful server instance from the given configs. If h2 is nil, then HTTP2 is not forced.

func NewServer

func NewServer(addr string, h http.Handler) (*Server, error)

NewServer creates a new graceful server instance with defaults.

func (*Server) ListenAndServe

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

ListenAndServe listens to and serves the server's address. The context is used for timing out the initial listen.

func (*Server) ListenAndServeAsync

func (s *Server) ListenAndServeAsync(ctx context.Context) error

ListenAndServeAsync listens to the server's address and serves in a background goroutine. The context is used for timing out the initial listen.

func (*Server) ShutdownTimeout

func (s *Server) ShutdownTimeout(timeout time.Duration) error

ShutdownTimeout is a convenient function to allow graceful shutdown for the given duration.

Jump to

Keyboard shortcuts

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