Skip to content

Commit

Permalink
add http.Server default timeouts (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyalaribe committed Apr 15, 2019
1 parent 261e476 commit 81479b0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions fdhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"sync"
"sync/atomic"
"time"
)

type (
Expand All @@ -18,7 +19,7 @@ type (
runLock sync.Mutex
stopLock sync.Mutex
router *Router
httpSrv *http.Server
HTTPSrv *http.Server

// Logger will be setted with DefaultLogger when NewServer is called
// but you can overwrite later only in this instance.
Expand Down Expand Up @@ -82,8 +83,13 @@ func (s *Server) Start(r *Router) error {

s.Logger.Printf("Running http server on %s...", s.addr)

s.httpSrv = &http.Server{
Addr: s.addr,
// Default timeouts to prevent unclosed requests leaking memory.
// https://blog.cloudflare.com/exposing-go-on-the-internet/#timeouts
s.HTTPSrv = &http.Server{
Addr: s.addr,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
}

if r != nil {
Expand All @@ -93,13 +99,13 @@ func (s *Server) Start(r *Router) error {

s.router = r
s.router.Init()
s.httpSrv.Handler = s.router
s.HTTPSrv.Handler = s.router
}

errChan := make(chan error)

go func() {
errChan <- s.httpSrv.ListenAndServe()
errChan <- s.HTTPSrv.ListenAndServe()
}()

atomic.StoreUint32(&s.running, 1)
Expand Down Expand Up @@ -131,5 +137,5 @@ func (s *Server) Stop(ctx context.Context) error {

s.Logger.Printf("Stopping http server...")

return s.httpSrv.Shutdown(ctx)
return s.HTTPSrv.Shutdown(ctx)
}

0 comments on commit 81479b0

Please sign in to comment.