diff --git a/db/db.go b/db/db.go index 619889e3..805b6e8a 100644 --- a/db/db.go +++ b/db/db.go @@ -183,8 +183,16 @@ 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 { - out = append(out, res) + 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 diff --git a/web/err.go b/web/err.go index 051997e1..b5ed1d7c 100644 --- a/web/err.go +++ b/web/err.go @@ -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.", + }, } diff --git a/web/routes.go b/web/routes.go index c59b44e5..d025c739 100644 --- a/web/routes.go +++ b/web/routes.go @@ -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,