From 404aaae5f15e28a8c9f3bbcfa0c22f21f7e506f6 Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Tue, 5 Aug 2025 20:24:00 +0800 Subject: [PATCH] without cancel the context --- connector/http2/connector.go | 2 +- dialer/http2/h2/dialer.go | 3 +-- internal/ctx/value.go | 26 -------------------------- listener/http2/conn.go | 12 +++--------- listener/http2/listener.go | 5 +++-- listener/mws/listener.go | 2 +- listener/ws/listener.go | 2 +- service/service.go | 5 +---- 8 files changed, 11 insertions(+), 46 deletions(-) diff --git a/connector/http2/connector.go b/connector/http2/connector.go index efbd2345..81e29ca2 100644 --- a/connector/http2/connector.go +++ b/connector/http2/connector.go @@ -97,7 +97,7 @@ func (c *http2Connector) Connect(ctx context.Context, conn net.Conn, network, ad defer conn.SetDeadline(time.Time{}) } - resp, err := client.Do(req.WithContext(ictx.Copy(ctx))) + resp, err := client.Do(req.WithContext(context.WithoutCancel(ctx))) if err != nil { log.Error(err) conn.Close() diff --git a/dialer/http2/h2/dialer.go b/dialer/http2/h2/dialer.go index ebe50d63..0f3c1854 100644 --- a/dialer/http2/h2/dialer.go +++ b/dialer/http2/h2/dialer.go @@ -16,7 +16,6 @@ import ( "github.com/go-gost/core/logger" md "github.com/go-gost/core/metadata" xctx "github.com/go-gost/x/ctx" - ictx "github.com/go-gost/x/internal/ctx" "github.com/go-gost/x/internal/net/proxyproto" "github.com/go-gost/x/registry" "golang.org/x/net/http2" @@ -170,7 +169,7 @@ func (d *h2Dialer) Dial(ctx context.Context, address string, opts ...dialer.Dial d.logger.Trace(string(dump)) } - resp, err := client.Do(req.WithContext(ictx.Copy(ctx))) + resp, err := client.Do(req.WithContext(context.WithoutCancel(ctx))) if err != nil { return nil, err } diff --git a/internal/ctx/value.go b/internal/ctx/value.go index 271645b3..cdab85da 100644 --- a/internal/ctx/value.go +++ b/internal/ctx/value.go @@ -6,7 +6,6 @@ import ( "github.com/go-gost/core/logger" "github.com/go-gost/core/metadata" - xctx "github.com/go-gost/x/ctx" xrecorder "github.com/go-gost/x/recorder" ) @@ -53,28 +52,3 @@ func RecorderObjectFromContext(ctx context.Context) *xrecorder.HandlerRecorderOb v, _ := ctx.Value(recorderObjectCtxKey{}).(*xrecorder.HandlerRecorderObject) return v } - -func Copy(ctx context.Context) context.Context { - if ctx == nil { - return nil - } - - ctx2 := context.Background() - if v := xctx.SrcAddrFromContext(ctx); v != nil { - ctx2 = xctx.ContextWithSrcAddr(ctx2, v) - } - if v := xctx.DstAddrFromContext(ctx); v != nil { - ctx2 = xctx.ContextWithDstAddr(ctx2, v) - } - if v := xctx.SidFromContext(ctx); v != "" { - ctx2 = xctx.ContextWithSid(ctx2, v) - } - if v := xctx.ClientIDFromContext(ctx); v != "" { - ctx2 = xctx.ContextWithClientID(ctx2, v) - } - if v := MetadataFromContext(ctx); v != nil { - ctx2 = ContextWithMetadata(ctx2, v) - } - - return ctx2 -} diff --git a/listener/http2/conn.go b/listener/http2/conn.go index 45199624..b546f45f 100644 --- a/listener/http2/conn.go +++ b/listener/http2/conn.go @@ -11,8 +11,8 @@ import ( type conn struct { laddr net.Addr raddr net.Addr - closed chan struct{} ctx context.Context + cancel context.CancelFunc } func (c *conn) Read(b []byte) (n int, err error) { @@ -24,10 +24,8 @@ func (c *conn) Write(b []byte) (n int, err error) { } func (c *conn) Close() error { - select { - case <-c.closed: - default: - close(c.closed) + if c.cancel != nil { + c.cancel() } return nil } @@ -52,10 +50,6 @@ func (c *conn) SetWriteDeadline(t time.Time) error { return &net.OpError{Op: "set", Net: "http2", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} } -func (c *conn) Done() <-chan struct{} { - return c.closed -} - func (c *conn) Context() context.Context { return c.ctx } diff --git a/listener/http2/listener.go b/listener/http2/listener.go index db13430e..041d6d1d 100644 --- a/listener/http2/listener.go +++ b/listener/http2/listener.go @@ -172,11 +172,12 @@ func (l *http2Listener) handleFunc(w http.ResponseWriter, r *http.Request) { "w": w, })) + ctx, cancel := context.WithCancel(ctx) conn := &conn{ laddr: l.addr, raddr: remoteAddr, - closed: make(chan struct{}), ctx: ctx, + cancel: cancel, } select { @@ -186,5 +187,5 @@ func (l *http2Listener) handleFunc(w http.ResponseWriter, r *http.Request) { return } - <-conn.Done() + <-ctx.Done() } diff --git a/listener/mws/listener.go b/listener/mws/listener.go index 4913404a..7155c642 100644 --- a/listener/mws/listener.go +++ b/listener/mws/listener.go @@ -185,7 +185,7 @@ func (l *mwsListener) upgrade(w http.ResponseWriter, r *http.Request) { return } - ctx := context.Background() + ctx := r.Context() if cc, ok := conn.NetConn().(xctx.Context); ok { if cv := cc.Context(); cv != nil { ctx = cv diff --git a/listener/ws/listener.go b/listener/ws/listener.go index 4fa2a6f0..97e1bfc7 100644 --- a/listener/ws/listener.go +++ b/listener/ws/listener.go @@ -180,7 +180,7 @@ func (l *wsListener) upgrade(w http.ResponseWriter, r *http.Request) { return } - ctx := context.Background() + ctx := context.WithoutCancel(r.Context()) if cc, ok := conn.NetConn().(xctx.Context); ok { if cv := cc.Context(); cv != nil { ctx = cv diff --git a/service/service.go b/service/service.go index 8f3e4206..68b61d17 100644 --- a/service/service.go +++ b/service/service.go @@ -230,14 +230,11 @@ func (s *defaultService) Serve() error { ctx = xctx.ContextWithDstAddr(ctx, dstAddr) } - // clientAddr := srcAddr.String() - // ctx = xctx.ContextWithClientAddr(ctx, xctx.ClientAddr(clientAddr)) - clientIP := srcAddr.String() if h, _, _ := net.SplitHostPort(clientIP); h != "" { clientIP = h } - // ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: clientIP}) + ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: clientIP}) for _, rec := range s.options.recorders { if rec.Record == recorder.RecorderServiceClientAddress {