Skip to content
/ httpterm Public

Go's http.Server with a graceful shutdown and more accurate read timeouts

License

Notifications You must be signed in to change notification settings

tg/httpterm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DEPRECATED

This package is deprecated since Go 1.8 contains relevant timeouts and graceful shutdown. Enjoy the standard library!

httpterm GoDoc Build Status

Extension to Go http.Server allowing for:

  • Graceful shutdown
  • Fine-tuning read timeouts

Examples

Run default server (as would http.ListenAndServe), exit gracefully on signal:

package main

import (
    "fmt"

    "github.com/tg/httpterm"
)

func main() {
    s := httpterm.Server{CloseOnSignal: true}
    pending, err := s.ListenAndServe()

    fmt.Print(err) // will be nil if close has been triggered
    <-pending      // wait for pending requests (channel will be closed)
}

Run server on port 8080, shut down from the handler:

package main

import (
    "fmt"
    "net/http"

    "github.com/tg/httpterm"
)

func main() {
    var s httpterm.Server
    s.Addr = ":8080"

    http.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) {
        s.Close()
    })

    pending, err := s.ListenAndServe()
    if err != nil {
        fmt.Println(err)
    }
    <-pending
}

Run server, read headers within 5 seconds, read body (usually POST content) within a minute, allow for idle connections for up to 15 seconds:

package main

import (
    "fmt"
    "time"

    "github.com/tg/httpterm"
)

func main() {
    var s httpterm.Server
    s.Addr = ":8080"
    s.CloseOnSignal = true

    s.HeadReadTimeout = 5 * time.Second
    s.BodyReadTimeout = time.Minute
    s.IdleTimeout = 15 * time.Second

    pending, err := s.ListenAndServe()
    if err != nil {
        fmt.Print(err)
    }
    <-pending
}

About

Go's http.Server with a graceful shutdown and more accurate read timeouts

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages