Add HTTP 504 request timeouts to certain routes

This commit is contained in:
Tobie Morgan Hitchcock 2017-03-03 21:41:06 +00:00
parent a96a8466cb
commit a133eb2482
3 changed files with 31 additions and 2 deletions

View file

@ -183,9 +183,17 @@ func Process(ctx *fibre.Context, ast *sql.Query, vars map[string]interface{}) (o
// return results, buffer the output, and
// return the output when finished.
for res := range recv {
for {
select {
case <-ctx.Context().Done():
return nil, fibre.NewHTTPError(504)
case res, open := <-recv:
if !open {
return
}
out = append(out, res)
}
}
return
@ -236,6 +244,9 @@ func (e *executor) execute(ctx context.Context, quit <-chan bool, send chan<- *R
select {
case <-ctx.Done():
return
case <-quit:
return

View file

@ -142,4 +142,10 @@ var errs = map[int]*err{
Details: "Not Implemented",
Description: "The server either does not recognize the request method, or it lacks the ability to fulfill the request.",
},
504: {
Code: 504,
Details: "Gateway Timeout",
Description: "The request to the server was restricted by a timeout duration, and did not manage to complete in time.",
},
}

View file

@ -84,6 +84,10 @@ func routes(s *fibre.Fibre) {
return signup(c)
})
s.Use(mw.Quit(&mw.QuitOpts{
Timeout: 10 * time.Second,
}).PathIs("/signup"))
s.Use(mw.Type(&mw.TypeOpts{
AllowedContent: map[string]bool{
"application/json": true,
@ -105,6 +109,10 @@ func routes(s *fibre.Fibre) {
return signin(c)
})
s.Use(mw.Quit(&mw.QuitOpts{
Timeout: 10 * time.Second,
}).PathIs("/signin"))
s.Use(mw.Type(&mw.TypeOpts{
AllowedContent: map[string]bool{
"application/json": true,
@ -336,6 +344,10 @@ func routes(s *fibre.Fibre) {
})
s.Use(mw.Quit(&mw.QuitOpts{
Timeout: 15 * time.Second,
}).PathBegsWith("/key/"))
s.Use(mw.Type(&mw.TypeOpts{
AllowedContent: map[string]bool{
"application/json": true,