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
+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