42
42
metricsCollector MetricsCollector
43
43
cfg config.CacheConfig
44
44
httpClient * http.Client
45
+ proxy * httputil.ReverseProxy
45
46
}
46
47
)
47
48
@@ -119,26 +120,35 @@ func errHandler(res http.ResponseWriter, req *http.Request, err error) {
119
120
120
121
func newCacheHandler (c Cacher , m MetricsCollector , w Worker , cfg config.CacheConfig ) handler {
121
122
netTransport := & http.Transport {
122
- MaxIdleConnsPerHost : 1000 ,
123
- DisableKeepAlives : false ,
124
- IdleConnTimeout : time .Hour * 1 ,
125
- Dial : (& net.Dialer {
123
+ Proxy : http .ProxyFromEnvironment ,
124
+ DialContext : (& net.Dialer {
126
125
Timeout : 10 * time .Second ,
127
- KeepAlive : 30 * time .Second ,
128
- }).Dial ,
126
+ KeepAlive : 60 * time .Second ,
127
+ }).DialContext ,
128
+ ForceAttemptHTTP2 : true ,
129
+ MaxIdleConns : 1000 ,
130
+ MaxIdleConnsPerHost : 100 ,
131
+ IdleConnTimeout : 90 * time .Second ,
129
132
TLSHandshakeTimeout : 10 * time .Second ,
130
- ResponseHeaderTimeout : 10 * time .Second ,
133
+ ExpectContinueTimeout : 1 * time .Second ,
134
+ ResponseHeaderTimeout : 30 * time .Second ,
131
135
}
132
136
137
+ httpClient := & http.Client {
138
+ Timeout : time .Second * 30 ,
139
+ Transport : netTransport ,
140
+ }
141
+
142
+ proxy := httputil .NewSingleHostReverseProxy (cfg .DownstreamHost )
143
+ proxy .Transport = netTransport
144
+
133
145
return handler {
134
146
cacher : c ,
135
147
worker : w ,
136
148
metricsCollector : m ,
137
149
cfg : cfg ,
138
- httpClient : & http.Client {
139
- Timeout : time .Second * 10 ,
140
- Transport : netTransport ,
141
- },
150
+ httpClient : httpClient ,
151
+ proxy : proxy ,
142
152
}
143
153
}
144
154
@@ -165,23 +175,22 @@ func (h handler) asyncCacheRevalidate(hashKey string, req *http.Request) func()
165
175
}
166
176
167
177
func (h handler ) ServeHTTP (res http.ResponseWriter , req * http.Request ) {
168
- proxy := httputil .NewSingleHostReverseProxy (h .cfg .DownstreamHost )
169
- proxy .ErrorHandler = errHandler
178
+ h .proxy .ErrorHandler = errHandler
170
179
logger := log .With ().Str ("path" , req .URL .Path ).Str ("method" , req .Method ).Logger ()
171
180
logCtx := logger .WithContext (req .Context ())
172
181
173
182
// websockets
174
183
if strings .ToLower (req .Header .Get ("connection" )) == "upgrade" {
175
184
logger .Info ().Msg ("will not cache websocket request" )
176
- proxy .ServeHTTP ( res , req )
177
-
185
+ h . proxy .ModifyResponse = nil
186
+ h . proxy . ServeHTTP ( res , req )
178
187
return
179
188
}
180
189
181
190
if strings .ToLower (req .Method ) != "get" {
182
191
logger .Debug ().Msg ("will not cache non-GET request" )
183
- proxy .ServeHTTP ( res , req )
184
-
192
+ h . proxy .ModifyResponse = nil
193
+ h . proxy . ServeHTTP ( res , req )
185
194
return
186
195
}
187
196
@@ -193,10 +202,9 @@ func (h handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
193
202
}
194
203
195
204
if result == nil {
196
- proxy .ModifyResponse = h .cacheResponse (logCtx , hashKey )
205
+ h . proxy .ModifyResponse = h .cacheResponse (logCtx , hashKey )
197
206
logger .Debug ().Msg ("will cache response from downstream" )
198
- proxy .ServeHTTP (res , req )
199
-
207
+ h .proxy .ServeHTTP (res , req )
200
208
return
201
209
}
202
210
0 commit comments