gracehttp

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: May 10, 2020 License: Apache-2.0 Imports: 17 Imported by: 2

README

gracehttp

This is a simple and graceful HTTP server for Golang.

Version

V1.2.0 (Require Go 1.8+)

Usage

package main

import (
    "fmt"
    "net/http"

    "github.com/tabalt/gracehttp"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "hello world")
    })

    err := gracehttp.ListenAndServe(":8080", nil)
    if err != nil {
        fmt.Println(err)
    }
}

Demo

  1. Install the demo application

     go get github.com/tabalt/gracehttp/gracehttpdemo
    
  2. Start it in the first terminal

     gracehttpdemo
    

    This will output something like:

     2015/09/14 20:01:08 Serving :8080 with pid 4388.
    
  3. In a second terminal start a slow HTTP request

     curl 'http://localhost:8080/sleep/?duration=20s'
    
  4. In a third terminal trigger a graceful server restart (using the pid from your output):

     kill -SIGUSR2 $pid
    
  5. Trigger another shorter request that finishes before the earlier request:

     curl 'http://localhost:8080/sleep/?duration=0s'
    

Documentation

Index

Constants

View Source
const (
	DEFAULT_READ_TIMEOUT  = 60 * time.Second
	DEFAULT_WRITE_TIMEOUT = DEFAULT_READ_TIMEOUT
)
View Source
const (
	GRACEFUL_ENVIRON_KEY    = "IS_GRACEFUL"
	GRACEFUL_ENVIRON_STRING = GRACEFUL_ENVIRON_KEY + "=1"
	GRACEFUL_LISTENER_FD    = 3
	OCSP_DEFAULT_EXPIRE     = time.Minute * 10
)

Variables

This section is empty.

Functions

func ListenAndServe

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

refer http.ListenAndServe

func ListenAndServeTLS

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

refer http.ListenAndServeTLS

Types

type Server

type Server struct {
	*http.Server
	// contains filtered or unexported fields
}

HTTP server that supported graceful shutdown or restart

func NewServer

func NewServer(addr string, handler http.Handler, readTimeout, writeTimeout time.Duration) *Server

func (*Server) GetCertificateWithOcsp added in v1.4.0

func (srv *Server) GetCertificateWithOcsp(*tls.ClientHelloInfo) (*tls.Certificate, error)

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

func (*Server) ListenAndServeTLS

func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error

func (*Server) ListenAndServeTLSOcsp added in v1.4.0

func (srv *Server) ListenAndServeTLSOcsp(expire time.Duration, certFile, keyFile string) error

func (*Server) Serve

func (srv *Server) Serve() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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