gracehttp

package module
v1.4.9 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2023 License: Apache-2.0 Imports: 18 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 (
	DefaultReadTimeout  = 60 * time.Second
	DefaultWriteTimeout = DefaultReadTimeout

	EnvDebug = "debug"
	EnvTest  = "test"
	EnvStage = "stage"
	EnvProd  = "prod"
)
View Source
const (
	GracefulEnvironKey    = "IS_GRACEFUL"
	GracefulEnvironString = GracefulEnvironKey
	GracefulListenerFd    = 3
	OcspDefaultExpire     = time.Minute * 10
)

Variables

This section is empty.

Functions

func ListenAndServe

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

ListenAndServe http

func ListenAndServeTLS

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

ListenAndServeTLS https

func SetEnv added in v1.4.4

func SetEnv(mode string)

Types

type Server

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

Server 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