http: use go1.8 graceful shutdown

This commit is contained in:
Leo Balduf 2017-02-18 13:06:52 +01:00
parent bb93e478b8
commit 300b9c12ab
5 changed files with 15 additions and 25 deletions

View file

@ -1,6 +1,6 @@
language: go
go:
- 1.7
- 1.8
sudo: false
install:
- go get -t ./...

View file

@ -39,7 +39,6 @@ chihaya:
# The timeout durations for HTTP requests.
read_timeout: 5s
write_timeout: 5s
request_timeout: 5s
# This block defines configuration for the tracker's UDP interface.
# If you do not wish to run this, delete this section.

View file

@ -12,7 +12,6 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus"
"github.com/tylerb/graceful"
"github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/frontend"
@ -66,7 +65,6 @@ type Config struct {
Addr string `yaml:"addr"`
ReadTimeout time.Duration `yaml:"read_timeout"`
WriteTimeout time.Duration `yaml:"write_timeout"`
RequestTimeout time.Duration `yaml:"request_timeout"`
AllowIPSpoofing bool `yaml:"allow_ip_spoofing"`
RealIPHeader string `yaml:"real_ip_header"`
TLSCertPath string `yaml:"tls_cert_path"`
@ -75,7 +73,7 @@ type Config struct {
// Frontend holds the state of an HTTP BitTorrent Frontend.
type Frontend struct {
grace *graceful.Server
s *http.Server
logic frontend.TrackerLogic
Config
@ -91,8 +89,9 @@ func NewFrontend(logic frontend.TrackerLogic, cfg Config) *Frontend {
// Stop provides a thread-safe way to shutdown a currently running Frontend.
func (t *Frontend) Stop() {
t.grace.Stop(t.grace.Timeout)
<-t.grace.StopChan()
if err := t.s.Shutdown(context.Background()); err != nil {
log.Warn("Error shutting down HTTP frontend:", err)
}
}
func (t *Frontend) handler() http.Handler {
@ -105,15 +104,11 @@ func (t *Frontend) handler() http.Handler {
// ListenAndServe listens on the TCP network address t.Addr and blocks serving
// BitTorrent requests until t.Stop() is called or an error is returned.
func (t *Frontend) ListenAndServe() error {
t.grace = &graceful.Server{
Server: &http.Server{
Addr: t.Addr,
Handler: t.handler(),
ReadTimeout: t.ReadTimeout,
WriteTimeout: t.WriteTimeout,
},
Timeout: t.RequestTimeout,
NoSignalHandling: true,
t.s = &http.Server{
Addr: t.Addr,
Handler: t.handler(),
ReadTimeout: t.ReadTimeout,
WriteTimeout: t.WriteTimeout,
ConnState: func(conn net.Conn, state http.ConnState) {
switch state {
case http.StateNew:
@ -133,7 +128,7 @@ func (t *Frontend) ListenAndServe() error {
}
},
}
t.grace.SetKeepAlivesEnabled(false)
t.s.SetKeepAlivesEnabled(false)
// If TLS is enabled, create a key pair and add it to the HTTP server.
if t.Config.TLSCertPath != "" && t.Config.TLSKeyPath != "" {
@ -145,11 +140,11 @@ func (t *Frontend) ListenAndServe() error {
if err != nil {
return err
}
t.grace.Server.TLSConfig = tlsCfg
t.s.TLSConfig = tlsCfg
}
// Start the HTTP server and gracefully handle any network errors.
if err := t.grace.ListenAndServe(); err != nil {
if err := t.s.ListenAndServe(); err != nil {
if opErr, ok := err.(*net.OpError); !ok || (ok && opErr.Op != "accept") {
panic("http: failed to gracefully run HTTP server: " + err.Error())
}

6
glide.lock generated
View file

@ -1,5 +1,5 @@
hash: 5d8b4db88e29a7095440707b96e55cafed00fd98a32293b4b54950797a54f9bc
updated: 2017-02-15T00:55:54.619709972-05:00
hash: ba0c138fa4c3c1f55ce0bce72afb19e67be67edc48be86297440dae521cfee57
updated: 2017-02-18T13:02:38.493823158+01:00
imports:
- name: github.com/beorn7/perks
version: 4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9
@ -62,8 +62,6 @@ imports:
subpackages:
- assert
- require
- name: github.com/tylerb/graceful
version: 4654dfbb6ad53cb5e27f37d99b02e16c1872fbbb
- name: golang.org/x/sys
version: d75a52659825e75fff6158388dddc6a5b04f9ba5
subpackages:

View file

@ -21,6 +21,4 @@ import:
version: ^1.1.4
subpackages:
- require
- package: github.com/tylerb/graceful
version: ^1.2.15
- package: gopkg.in/yaml.v2