aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar losingle <losingle@gmail.com> 2019-06-06 23:17:40 +0800
committerGravatar Erik Dubbelboer <erik@dubbelboer.com> 2019-06-06 17:17:40 +0200
commit9494955f8e5a64a6abd126049e11f916d3121ac9 (patch)
tree23e736f825bf098e19c43fffa6241c088f5661b9
parent:zap: Used Headers constants instead raw strings (diff)
downloadfasthttp-9494955f8e5a64a6abd126049e11f916d3121ac9.tar.gz
fasthttp-9494955f8e5a64a6abd126049e11f916d3121ac9.tar.bz2
fasthttp-9494955f8e5a64a6abd126049e11f916d3121ac9.zip
ADD TimeoutWithCodeHandler support (#589)
* ADD TimeoutWithCodeHandler support * FIX description
-rw-r--r--server.go13
1 files changed, 12 insertions, 1 deletions
diff --git a/server.go b/server.go
index 98a5be8..9d14599 100644
--- a/server.go
+++ b/server.go
@@ -371,6 +371,17 @@ type Server struct {
// msg to the client if there are more than Server.Concurrency concurrent
// handlers h are running at the moment.
func TimeoutHandler(h RequestHandler, timeout time.Duration, msg string) RequestHandler {
+ return TimeoutWithCodeHandler(h,timeout,msg, StatusRequestTimeout)
+}
+
+// TimeoutWithCodeHandler creates RequestHandler, which returns an error with
+// the given msg and status code to the client if h didn't return during
+// the given duration.
+//
+// The returned handler may return StatusTooManyRequests error with the given
+// msg to the client if there are more than Server.Concurrency concurrent
+// handlers h are running at the moment.
+func TimeoutWithCodeHandler(h RequestHandler, timeout time.Duration, msg string, statusCode int) RequestHandler {
if timeout <= 0 {
return h
}
@@ -398,7 +409,7 @@ func TimeoutHandler(h RequestHandler, timeout time.Duration, msg string) Request
select {
case <-ch:
case <-ctx.timeoutTimer.C:
- ctx.TimeoutError(msg)
+ ctx.TimeoutErrorWithCode(msg, statusCode)
}
stopTimer(ctx.timeoutTimer)
}