-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbase.go
54 lines (45 loc) · 1.59 KB
/
base.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
package transport
import (
"net/http"
)
// BaseTransport is a wrapper around [http.Transport] with some [BaseTransportConfig] configuration.
type BaseTransport struct {
transport *http.Transport
config *BaseTransportConfig
}
// BaseTransportConfig is the configuration of the [BaseTransport].
type BaseTransportConfig struct {
MaxIdleConnections int
MaxConnectionsPerHost int
MaxIdleConnectionsPerHost int
}
// NewBaseTransport returns a [BaseTransport] instance with optimized default [BaseTransportConfig] configuration.
func NewBaseTransport() *BaseTransport {
return NewBaseTransportWithConfig(
&BaseTransportConfig{
MaxIdleConnections: 100,
MaxConnectionsPerHost: 100,
MaxIdleConnectionsPerHost: 100,
},
)
}
// NewBaseTransportWithConfig returns a [BaseTransport] instance for a provided [BaseTransportConfig] configuration.
func NewBaseTransportWithConfig(config *BaseTransportConfig) *BaseTransport {
//nolint:forcetypeassert
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.MaxIdleConns = config.MaxIdleConnections
transport.MaxConnsPerHost = config.MaxConnectionsPerHost
transport.MaxIdleConnsPerHost = config.MaxIdleConnectionsPerHost
return &BaseTransport{
transport: transport,
config: config,
}
}
// Base returns the wrapped [http.Transport].
func (t *BaseTransport) Base() *http.Transport {
return t.transport
}
// RoundTrip performs a request / response round trip, based on the wrapped [http.Transport].
func (t *BaseTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return t.transport.RoundTrip(req)
}