fix http2 failover

This commit is contained in:
ginuerzh 2020-01-09 10:10:22 +08:00
parent 7eea9b3ad7
commit 085b0b30e7

View File

@ -79,6 +79,7 @@ func (c *http2Connector) Connect(conn net.Conn, addr string, options ...ConnectO
} }
resp, err := cc.client.Do(req) resp, err := cc.client.Do(req)
if err != nil { if err != nil {
cc.Close()
return nil, err return nil, err
} }
if Debug { if Debug {
@ -130,11 +131,11 @@ func (tr *http2Transporter) Dial(addr string, options ...DialOption) (net.Conn,
client, ok := tr.clients[addr] client, ok := tr.clients[addr]
if !ok { if !ok {
// NOTE: due to the dummy connection, HTTP2 node in a proxy chain can not be marked as dead. // NOTE: There is no real connection to the HTTP2 server at this moment.
// There is no real connection to the HTTP2 server at this moment. // So we try to connect to the server to check the server health.
// So we should try to connect the server.
conn, err := opts.Chain.Dial(addr) conn, err := opts.Chain.Dial(addr)
if err != nil { if err != nil {
log.Log("http2 dial:", addr, err)
return nil, err return nil, err
} }
conn.Close() conn.Close()
@ -163,6 +164,11 @@ func (tr *http2Transporter) Dial(addr string, options ...DialOption) (net.Conn,
return &http2ClientConn{ return &http2ClientConn{
addr: addr, addr: addr,
client: client, client: client,
onClose: func() {
tr.clientMutex.Lock()
defer tr.clientMutex.Unlock()
delete(tr.clients, addr)
},
}, nil }, nil
} }
@ -889,8 +895,16 @@ func (c *http2ServerConn) SetWriteDeadline(t time.Time) error {
// a dummy HTTP2 client conn used by HTTP2 client connector // a dummy HTTP2 client conn used by HTTP2 client connector
type http2ClientConn struct { type http2ClientConn struct {
nopConn nopConn
addr string addr string
client *http.Client client *http.Client
onClose func()
}
func (c *http2ClientConn) Close() error {
if c.onClose != nil {
c.onClose()
}
return nil
} }
type flushWriter struct { type flushWriter struct {