-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransport.go
80 lines (70 loc) · 2.73 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package requests
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/url"
"strings"
"time"
)
// WarpRoundTripper wraps an http.RoundTripper instance.
// This function returns a new decorator function that adds additional functionality to an existing RoundTripper.
func WarpRoundTripper(next http.RoundTripper) func(http.RoundTripper) http.RoundTripper {
return func(http.RoundTripper) http.RoundTripper {
return RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
return next.RoundTrip(r)
})
}
}
// RoundTripperFunc is a functional implementation of the http.RoundTripper interface.
// It allows converting regular functions to the RoundTripper interface, facilitating functional extensions.
type RoundTripperFunc func(*http.Request) (*http.Response, error)
// RoundTrip implements the http.RoundTripper interface.
// It directly calls the underlying function to complete the request sending and response receiving.
func (fn RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return fn(r)
}
// newTransport creates a new Transport instance.
// It configures connection pool, timeout settings, TLS, and other parameters.
func newTransport(opts ...Option) *http.Transport {
options := newOptions(opts)
return &http.Transport{
// Proxy sets the proxy function
Proxy: options.Proxy,
// DialContext customizes connection creation logic
// Supports Unix domain sockets and TCP connections
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
// Handle Unix domain socket connections
if strings.HasPrefix(options.URL, "unix://") {
u, err := url.Parse(options.URL)
if err != nil {
return nil, err
}
network, addr = u.Scheme, u.Path
}
// Configure dialer parameters
dialer := net.Dialer{
Timeout: 10 * time.Second, // TCP connection timeout
KeepAlive: 60 * time.Second, // TCP keepalive interval
LocalAddr: options.LocalAddr, // Local address binding
Resolver: &net.Resolver{ // DNS resolver configuration
PreferGo: true, // Prefer Go's DNS resolver
StrictErrors: false, // Tolerate DNS resolution errors
},
}
return dialer.DialContext(ctx, network, addr)
},
// Connection pool configuration
MaxIdleConns: options.MaxConns, // Maximum number of idle connections
MaxIdleConnsPerHost: options.MaxConns, // Maximum number of idle connections per host
IdleConnTimeout: 120 * time.Second, // Idle connection timeout
// Connection behavior configuration
DisableCompression: true, // Disable compression
DisableKeepAlives: false, // Enable Keep-Alive
// TLS configuration
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !options.Verify, // Whether to verify server certificates
},
}
}