Skip to content

tabalt/gracehttp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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'