Skip to content

Commit

Permalink
http context never done (gogf#2602)
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn authored and dukepan2005 committed Apr 27, 2023
1 parent 83be434 commit 7c4a562
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion net/ghttp/ghttp_request_param_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,32 @@ package ghttp

import (
"context"
"time"

"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/os/gctx"
)

// neverDoneCtx never done.
type neverDoneCtx struct {
context.Context
}

// Done forbids the context done from parent context.
func (*neverDoneCtx) Done() <-chan struct{} {
return nil
}

// Deadline forbids the context deadline from parent context.
func (*neverDoneCtx) Deadline() (deadline time.Time, ok bool) {
return time.Time{}, false
}

// Err forbids the context done from parent context.
func (c *neverDoneCtx) Err() error {
return nil
}

// RequestFromCtx retrieves and returns the Request object from context.
func RequestFromCtx(ctx context.Context) *Request {
if v := ctx.Value(ctxKeyForRequest); v != nil {
Expand All @@ -26,7 +47,12 @@ func RequestFromCtx(ctx context.Context) *Request {
// See GetCtx.
func (r *Request) Context() context.Context {
if r.context == nil {
r.context = gctx.WithCtx(r.Request.Context())
// It forbids the context manually done,
// to make the context can be propagated to asynchronous goroutines.
r.context = &neverDoneCtx{
r.Request.Context(),
}
r.context = gctx.WithCtx(r.context)
}
// Inject Request object into context.
if RequestFromCtx(r.context) == nil {
Expand Down

0 comments on commit 7c4a562

Please sign in to comment.