without cancel the context

This commit is contained in:
ginuerzh
2025-08-05 20:24:00 +08:00
parent fd9dc6408a
commit 404aaae5f1
8 changed files with 11 additions and 46 deletions
+1 -1
View File
@@ -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()
+1 -2
View File
@@ -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
}
-26
View File
@@ -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
}
+3 -9
View File
@@ -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
}
+3 -2
View File
@@ -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()
}
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -4
View File
@@ -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 {