Fix HTTP server leak

This commit is contained in:
世界 2023-10-01 14:36:31 +08:00
parent e727641a98
commit e0ec961fb1
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -21,7 +21,6 @@ import (
type Handler = N.TCPConnectionHandler type Handler = N.TCPConnectionHandler
func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Reader, authenticator auth.Authenticator, handler Handler, metadata M.Metadata) error { func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Reader, authenticator auth.Authenticator, handler Handler, metadata M.Metadata) error {
var httpClient *http.Client
for { for {
request, err := ReadRequest(reader) request, err := ReadRequest(reader)
if err != nil { if err != nil {
@ -95,8 +94,7 @@ func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Read
} }
var innerErr error var innerErr error
if httpClient == nil { httpClient := &http.Client{
httpClient = &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
DisableCompression: true, DisableCompression: true,
DialContext: func(context context.Context, network, address string) (net.Conn, error) { DialContext: func(context context.Context, network, address string) (net.Conn, error) {
@ -117,7 +115,6 @@ func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Read
return http.ErrUseLastResponse return http.ErrUseLastResponse
}, },
} }
}
response, err := httpClient.Do(request) response, err := httpClient.Do(request)
if err != nil { if err != nil {
@ -139,6 +136,8 @@ func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Read
return E.Errors(innerErr, err) return E.Errors(innerErr, err)
} }
httpClient.CloseIdleConnections()
if !keepAlive { if !keepAlive {
return conn.Close() return conn.Close()
} }