|
@@ -25,16 +25,24 @@ var (
|
|
|
globalDialerAccess sync.Mutex
|
|
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()
|
|
globalDialerAccess.Lock()
|
|
|
defer globalDialerAccess.Unlock()
|
|
defer globalDialerAccess.Unlock()
|
|
|
|
|
|
|
|
|
|
+ canceller := func() {
|
|
|
|
|
+ globalDialerAccess.Lock()
|
|
|
|
|
+ defer globalDialerAccess.Unlock()
|
|
|
|
|
+ delete(globalDialerMap, dest)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if globalDialerMap == nil {
|
|
if globalDialerMap == nil {
|
|
|
globalDialerMap = make(map[net.Destination]*http.Client)
|
|
globalDialerMap = make(map[net.Destination]*http.Client)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if client, found := globalDialerMap[dest]; found {
|
|
if client, found := globalDialerMap[dest]; found {
|
|
|
- return client
|
|
|
|
|
|
|
+ return client, canceller
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
transport := &http2.Transport{
|
|
transport := &http2.Transport{
|
|
@@ -81,7 +89,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.C
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
globalDialerMap[dest] = client
|
|
globalDialerMap[dest] = client
|
|
|
- return client
|
|
|
|
|
|
|
+ return client, canceller
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Dial dials a new TCP connection to the given destination.
|
|
// 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 {
|
|
if tlsConfig == nil {
|
|
|
return nil, newError("TLS must be enabled for http transport.").AtWarning()
|
|
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)
|
|
opts := pipe.OptionsFromContext(ctx)
|
|
|
preader, pwriter := pipe.New(opts...)
|
|
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
|
|
response, err := client.Do(request) // nolint: bodyclose
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
|
|
+ canceller()
|
|
|
return nil, newError("failed to dial to ", dest).Base(err).AtWarning()
|
|
return nil, newError("failed to dial to ", dest).Base(err).AtWarning()
|
|
|
}
|
|
}
|
|
|
if response.StatusCode != 200 {
|
|
if response.StatusCode != 200 {
|