3
3
package transformerclient
4
4
5
5
import (
6
- "context"
7
6
"net"
8
7
"net/http"
9
8
"time"
@@ -12,8 +11,6 @@ import (
12
11
"github.com/bufbuild/httplb/conn"
13
12
"github.com/bufbuild/httplb/picker"
14
13
"github.com/bufbuild/httplb/resolver"
15
-
16
- "github.com/rudderlabs/rudder-server/utils/sysUtils"
17
14
)
18
15
19
16
type ClientConfig struct {
@@ -26,10 +23,9 @@ type ClientConfig struct {
26
23
27
24
ClientTimeout time.Duration // 600*time.Second
28
25
ClientTTL time.Duration // 10*time.Second
29
-
30
- ClientType string // stdlib(default), recycled, httplb
31
-
32
- PickerType string // power_of_two(default), round_robin, least_loaded_random, least_loaded_round_robin, random
26
+ ClientType string // stdlib(default), httplb
27
+ PickerType string // power_of_two(default), round_robin, least_loaded_random, least_loaded_round_robin, random
28
+ Recycle bool // false
33
29
}
34
30
35
31
type Client interface {
@@ -72,22 +68,24 @@ func NewClient(config *ClientConfig) Client {
72
68
}
73
69
74
70
switch config .ClientType {
75
- case "stdlib" :
76
- return client
77
- case "recycled" :
78
- return sysUtils .NewRecycledHTTPClient (func () * http.Client {
79
- return client
80
- }, clientTTL )
81
71
case "httplb" :
82
- return httplb .NewClient (
83
- httplb .WithRootContext (context .TODO ()),
72
+ tr := & httplbtransport {
73
+ MaxConnsPerHost : transport .MaxConnsPerHost ,
74
+ MaxIdleConnsPerHost : transport .MaxIdleConnsPerHost ,
75
+ }
76
+ options := []httplb.ClientOption {
84
77
httplb .WithPicker (getPicker (config .PickerType )),
85
78
httplb .WithIdleConnectionTimeout (transport .IdleConnTimeout ),
86
79
httplb .WithRequestTimeout (client .Timeout ),
87
- httplb .WithRoundTripperMaxLifetime (transport .IdleConnTimeout ),
88
- httplb .WithIdleTransportTimeout (2 * transport .IdleConnTimeout ),
89
80
httplb .WithResolver (resolver .NewDNSResolver (net .DefaultResolver , resolver .PreferIPv4 , clientTTL )),
90
- )
81
+ httplb .WithTransport ("http" , tr ),
82
+ httplb .WithTransport ("https" , tr ),
83
+ }
84
+ if config .Recycle {
85
+ options = append (options , httplb .WithRoundTripperMaxLifetime (transport .IdleConnTimeout ))
86
+ }
87
+
88
+ return httplb .NewClient (options ... )
91
89
default :
92
90
return client
93
91
}
@@ -110,10 +108,27 @@ func getPicker(pickerType string) func(prev picker.Picker, allConns conn.Conns)
110
108
}
111
109
}
112
110
113
- type HTTPLBTransport struct {
111
+ type httplbtransport struct {
112
+ MaxConnsPerHost int
113
+ MaxIdleConnsPerHost int
114
114
* http.Transport
115
115
}
116
116
117
- func (t * HTTPLBTransport ) NewRoundTripper (scheme , target string , config httplb.TransportConfig ) httplb.RoundTripperResult {
118
- return httplb.RoundTripperResult {RoundTripper : t .Transport , Close : t .CloseIdleConnections }
117
+ func (s httplbtransport ) NewRoundTripper (_ , _ string , opts httplb.TransportConfig ) httplb.RoundTripperResult {
118
+ transport := & http.Transport {
119
+ Proxy : opts .ProxyFunc ,
120
+ GetProxyConnectHeader : opts .ProxyConnectHeadersFunc ,
121
+ DialContext : opts .DialFunc ,
122
+ ForceAttemptHTTP2 : true ,
123
+ MaxConnsPerHost : s .MaxConnsPerHost ,
124
+ MaxIdleConns : s .MaxIdleConnsPerHost ,
125
+ MaxIdleConnsPerHost : s .MaxIdleConnsPerHost ,
126
+ IdleConnTimeout : opts .IdleConnTimeout ,
127
+ TLSHandshakeTimeout : opts .TLSHandshakeTimeout ,
128
+ TLSClientConfig : opts .TLSClientConfig ,
129
+ MaxResponseHeaderBytes : opts .MaxResponseHeaderBytes ,
130
+ ExpectContinueTimeout : 1 * time .Second ,
131
+ DisableCompression : opts .DisableCompression ,
132
+ }
133
+ return httplb.RoundTripperResult {RoundTripper : transport , Close : transport .CloseIdleConnections }
119
134
}
0 commit comments