fix http connector

This commit is contained in:
ginuerzh
2022-01-28 16:13:43 +08:00
parent 1b22a5db9f
commit 0e9a975a26
5 changed files with 20 additions and 21 deletions

View File

@ -1,7 +1,6 @@
package http2
import (
"context"
"errors"
"net"
"net/http"
@ -70,23 +69,21 @@ type ServerConn struct {
w http.ResponseWriter
localAddr net.Addr
remoteAddr net.Addr
cancel context.CancelFunc
closed chan struct{}
}
func NewServerConn(w http.ResponseWriter, r *http.Request, localAddr, remoteAddr net.Addr) *ServerConn {
ctx, cancel := context.WithCancel(r.Context())
return &ServerConn{
r: r.Clone(ctx),
r: r,
w: w,
localAddr: localAddr,
remoteAddr: remoteAddr,
cancel: cancel,
closed: make(chan struct{}),
}
}
func (c *ServerConn) Done() <-chan struct{} {
return c.r.Context().Done()
return c.closed
}
func (c *ServerConn) Request() *http.Request {
@ -106,11 +103,10 @@ func (c *ServerConn) Write(b []byte) (n int, err error) {
}
func (c *ServerConn) Close() error {
c.cancel()
select {
case <-c.r.Context().Done():
case <-c.closed:
default:
close(c.closed)
}
return nil
}