Skip to content

Commit

Permalink
Timeout HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
cboylan committed Aug 19, 2019
1 parent 14230ca commit d11d4da
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package routes

import (
"bytes"
stdcontext "context"
"encoding/gob"
"net/http"
"os"
Expand Down Expand Up @@ -90,6 +91,13 @@ func setupAccessLogger(m *macaron.Macaron) {
})
}

func requestTimeout(ctx * macaron.Context) {
// TODO make timeout configurable
httpContext := ctx.Req.Context()
newContext, _ := stdcontext.WithTimeout(httpContext, time.Second*5)
ctx.Req.WithContext(newContext)
}

// RouterHandler is a macaron handler that will log the routing to the default gitea log
func RouterHandler(level log.Level) func(ctx *macaron.Context) {
return func(ctx *macaron.Context) {
Expand Down Expand Up @@ -127,6 +135,7 @@ func NewMacaron() *macaron.Macaron {
if setting.EnableAccessLog {
setupAccessLogger(m)
}
m.Use(requestTimeout)
m.Use(macaron.Recovery())
if setting.EnableGzip {
m.Use(gzip.Middleware())
Expand Down

0 comments on commit d11d4da

Please sign in to comment.