diff --git a/README.md b/README.md index c11d9cef14..9aa65a5dec 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,23 @@ with fasthttp support: fasthttp.ListenAndServe(":80", m) ``` +* Because creating a new channel for every request is just too expensive, the channel returned by RequestCtx.Done() is only closed when the server is shutting down. + + ```go + func main() { + fasthttp.ListenAndServe(":8080", fasthttp.TimeoutHandler(func(ctx *fasthttp.RequestCtx) { + select { + case <-ctx.Done(): + // ctx.Done() is only closed when the server is shutting down. + log.Println("context cancelled") + return + case <-time.After(10 * time.Second): + log.Println("process finished ok") + } + }, time.Second*2, "timeout")) + } + ``` + * net/http -> fasthttp conversion table: * All the pseudocode below assumes w, r and ctx have these types: diff --git a/server.go b/server.go index 0be703c77d..6148384b17 100644 --- a/server.go +++ b/server.go @@ -2697,6 +2697,9 @@ func (ctx *RequestCtx) Deadline() (deadline time.Time, ok bool) { // Done returns a channel that's closed when work done on behalf of this // context should be canceled. Done may return nil if this context can // never be canceled. Successive calls to Done return the same value. +// +// Note: Because creating a new channel for every request is just too expensive so +// RequestCtx.s.done is only closed when the server is shutting down func (ctx *RequestCtx) Done() <-chan struct{} { return ctx.s.done } @@ -2707,6 +2710,9 @@ func (ctx *RequestCtx) Done() <-chan struct{} { // If Done is closed, Err returns a non-nil error explaining why: // Canceled if the context was canceled (via server Shutdown) // or DeadlineExceeded if the context's deadline passed. +// +// Note: Because creating a new channel for every request is just too expensive so +// RequestCtx.s.done is only closed when the server is shutting down func (ctx *RequestCtx) Err() error { select { case <-ctx.s.done: