gracehttp

package
v5.9.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2018 License: GPL-3.0 Imports: 10 Imported by: 0

README

gracehttp

This is a simple and graceful http server for golang.

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 localhost: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 (
	GRACEFUL_ENVIRON_KEY    = "IS_GRACEFUL"
	GRACEFUL_ENVIRON_STRING = GRACEFUL_ENVIRON_KEY + "=1"

	DEFAULT_READ_TIMEOUT  = 60 * time.Second
	DEFAULT_WRITE_TIMEOUT = DEFAULT_READ_TIMEOUT
)

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 Connection

type Connection struct {
	net.Conn
	// contains filtered or unexported fields
}

func (*Connection) Close

func (this *Connection) Close() error

type Listener

type Listener struct {
	*net.TCPListener
	// contains filtered or unexported fields
}

func (*Listener) Accept

func (this *Listener) Accept() (net.Conn, error)

func (*Listener) GetFd

func (this *Listener) GetFd() (uintptr, error)

func (*Listener) Wait

func (this *Listener) Wait()

type Server

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

支持优雅重启的http服务

func NewServer

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

new server

func (*Server) ListenAndServe

func (this *Server) ListenAndServe() error

func (*Server) ListenAndServeTLS

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

func (*Server) Serve

func (this *Server) Serve() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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