42
42
metricsCollector MetricsCollector
43
43
cfg config.CacheConfig
44
44
httpClient * http.Client
45
- proxy * httputil.ReverseProxy
46
45
}
47
46
)
48
47
@@ -139,16 +138,12 @@ func newCacheHandler(c Cacher, m MetricsCollector, w Worker, cfg config.CacheCon
139
138
Transport : netTransport ,
140
139
}
141
140
142
- proxy := httputil .NewSingleHostReverseProxy (cfg .DownstreamHost )
143
- proxy .Transport = netTransport
144
-
145
141
return handler {
146
142
cacher : c ,
147
143
worker : w ,
148
144
metricsCollector : m ,
149
145
cfg : cfg ,
150
146
httpClient : httpClient ,
151
- proxy : proxy ,
152
147
}
153
148
}
154
149
@@ -175,22 +170,23 @@ func (h handler) asyncCacheRevalidate(hashKey string, req *http.Request) func()
175
170
}
176
171
177
172
func (h handler ) ServeHTTP (res http.ResponseWriter , req * http.Request ) {
178
- h .proxy .ErrorHandler = errHandler
179
173
logger := log .With ().Str ("path" , req .URL .Path ).Str ("method" , req .Method ).Logger ()
180
174
logCtx := logger .WithContext (req .Context ())
181
175
176
+ proxy := httputil .NewSingleHostReverseProxy (h .cfg .DownstreamHost )
177
+ proxy .Transport = h .httpClient .Transport
178
+ proxy .ErrorHandler = errHandler
179
+
182
180
// websockets
183
181
if strings .ToLower (req .Header .Get ("connection" )) == "upgrade" {
184
182
logger .Info ().Msg ("will not cache websocket request" )
185
- h .proxy .ModifyResponse = nil
186
- h .proxy .ServeHTTP (res , req )
183
+ proxy .ServeHTTP (res , req )
187
184
return
188
185
}
189
186
190
187
if strings .ToLower (req .Method ) != "get" {
191
188
logger .Debug ().Msg ("will not cache non-GET request" )
192
- h .proxy .ModifyResponse = nil
193
- h .proxy .ServeHTTP (res , req )
189
+ proxy .ServeHTTP (res , req )
194
190
return
195
191
}
196
192
@@ -202,9 +198,9 @@ func (h handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
202
198
}
203
199
204
200
if result == nil {
205
- h . proxy .ModifyResponse = h .cacheResponse (logCtx , hashKey )
201
+ proxy .ModifyResponse = h .cacheResponse (logCtx , hashKey )
206
202
logger .Debug ().Msg ("will cache response from downstream" )
207
- h . proxy .ServeHTTP (res , req )
203
+ proxy .ServeHTTP (res , req )
208
204
return
209
205
}
210
206
0 commit comments