Skip to content

Commit d11d4da

Browse files
committed
Timeout HTTP requests
1 parent 14230ca commit d11d4da

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

routers/routes/routes.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package routes
66

77
import (
88
"bytes"
9+
stdcontext "context"
910
"encoding/gob"
1011
"net/http"
1112
"os"
@@ -90,6 +91,13 @@ func setupAccessLogger(m *macaron.Macaron) {
9091
})
9192
}
9293

94+
func requestTimeout(ctx * macaron.Context) {
95+
// TODO make timeout configurable
96+
httpContext := ctx.Req.Context()
97+
newContext, _ := stdcontext.WithTimeout(httpContext, time.Second*5)
98+
ctx.Req.WithContext(newContext)
99+
}
100+
93101
// RouterHandler is a macaron handler that will log the routing to the default gitea log
94102
func RouterHandler(level log.Level) func(ctx *macaron.Context) {
95103
return func(ctx *macaron.Context) {
@@ -127,6 +135,7 @@ func NewMacaron() *macaron.Macaron {
127135
if setting.EnableAccessLog {
128136
setupAccessLogger(m)
129137
}
138+
m.Use(requestTimeout)
130139
m.Use(macaron.Recovery())
131140
if setting.EnableGzip {
132141
m.Use(gzip.Middleware())

0 commit comments

Comments
 (0)