mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-30 05:56:54 -05:00
attempt fix of h2 dialer by discarding http client on failure
https://github.com/v2fly/v2ray-core/issues/1056#issuecomment-868321840
This commit is contained in:
parent
e5913a132a
commit
0cbb6d75f3
@ -25,16 +25,24 @@ var (
|
||||
globalDialerAccess sync.Mutex
|
||||
)
|
||||
|
||||
func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.Config) *http.Client {
|
||||
type dialerCanceller func()
|
||||
|
||||
func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.Config) (*http.Client, dialerCanceller) {
|
||||
globalDialerAccess.Lock()
|
||||
defer globalDialerAccess.Unlock()
|
||||
|
||||
canceller := func() {
|
||||
globalDialerAccess.Lock()
|
||||
defer globalDialerAccess.Unlock()
|
||||
delete(globalDialerMap, dest)
|
||||
}
|
||||
|
||||
if globalDialerMap == nil {
|
||||
globalDialerMap = make(map[net.Destination]*http.Client)
|
||||
}
|
||||
|
||||
if client, found := globalDialerMap[dest]; found {
|
||||
return client
|
||||
return client, canceller
|
||||
}
|
||||
|
||||
transport := &http2.Transport{
|
||||
@ -81,7 +89,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.C
|
||||
}
|
||||
|
||||
globalDialerMap[dest] = client
|
||||
return client
|
||||
return client, canceller
|
||||
}
|
||||
|
||||
// Dial dials a new TCP connection to the given destination.
|
||||
@ -91,7 +99,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
|
||||
if tlsConfig == nil {
|
||||
return nil, newError("TLS must be enabled for http transport.").AtWarning()
|
||||
}
|
||||
client := getHTTPClient(ctx, dest, tlsConfig)
|
||||
client, canceller := getHTTPClient(ctx, dest, tlsConfig)
|
||||
|
||||
opts := pipe.OptionsFromContext(ctx)
|
||||
preader, pwriter := pipe.New(opts...)
|
||||
@ -129,6 +137,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
|
||||
|
||||
response, err := client.Do(request) // nolint: bodyclose
|
||||
if err != nil {
|
||||
canceller()
|
||||
return nil, newError("failed to dial to ", dest).Base(err).AtWarning()
|
||||
}
|
||||
if response.StatusCode != 200 {
|
||||
|
Loading…
Reference in New Issue
Block a user