go-zero/rest/handler/timeouthandler.go

20 lines
364 B
Go
Raw Normal View History

2020-07-29 18:00:04 +08:00
package handler
2020-07-26 17:09:05 +08:00
import (
"net/http"
"time"
)
const reason = "Request Timeout"
2021-02-08 21:31:56 +08:00
// TimeoutHandler returns the handler with given timeout.
2020-07-26 17:09:05 +08:00
func TimeoutHandler(duration time.Duration) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
if duration > 0 {
return http.TimeoutHandler(next, duration, reason)
}
2021-02-08 21:31:56 +08:00
return next
2020-07-26 17:09:05 +08:00
}
}