Skip to content

Commit

Permalink
doc: optimize the comment of the Request.Done method (#1454)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Dec 18, 2022
1 parent f6aac90 commit 2a572e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -320,6 +320,23 @@ with fasthttp support:
fasthttp.ListenAndServe(":80", m)
```

* Because creating a new channel for every request is just too expensive, so 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:
Expand Down
6 changes: 6 additions & 0 deletions server.go
Expand Up @@ -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
}
Expand All @@ -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:
Expand Down

0 comments on commit 2a572e0

Please sign in to comment.