4242 metricsCollector MetricsCollector
4343 cfg config.CacheConfig
4444 httpClient * http.Client
45- proxy * httputil.ReverseProxy
4645 }
4746)
4847
@@ -139,16 +138,12 @@ func newCacheHandler(c Cacher, m MetricsCollector, w Worker, cfg config.CacheCon
139138 Transport : netTransport ,
140139 }
141140
142- proxy := httputil .NewSingleHostReverseProxy (cfg .DownstreamHost )
143- proxy .Transport = netTransport
144-
145141 return handler {
146142 cacher : c ,
147143 worker : w ,
148144 metricsCollector : m ,
149145 cfg : cfg ,
150146 httpClient : httpClient ,
151- proxy : proxy ,
152147 }
153148}
154149
@@ -175,22 +170,23 @@ func (h handler) asyncCacheRevalidate(hashKey string, req *http.Request) func()
175170}
176171
177172func (h handler ) ServeHTTP (res http.ResponseWriter , req * http.Request ) {
178- h .proxy .ErrorHandler = errHandler
179173 logger := log .With ().Str ("path" , req .URL .Path ).Str ("method" , req .Method ).Logger ()
180174 logCtx := logger .WithContext (req .Context ())
181175
176+ proxy := httputil .NewSingleHostReverseProxy (h .cfg .DownstreamHost )
177+ proxy .Transport = h .httpClient .Transport
178+ proxy .ErrorHandler = errHandler
179+
182180 // websockets
183181 if strings .ToLower (req .Header .Get ("connection" )) == "upgrade" {
184182 logger .Info ().Msg ("will not cache websocket request" )
185- h .proxy .ModifyResponse = nil
186- h .proxy .ServeHTTP (res , req )
183+ proxy .ServeHTTP (res , req )
187184 return
188185 }
189186
190187 if strings .ToLower (req .Method ) != "get" {
191188 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 )
194190 return
195191 }
196192
@@ -202,9 +198,9 @@ func (h handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
202198 }
203199
204200 if result == nil {
205- h . proxy .ModifyResponse = h .cacheResponse (logCtx , hashKey )
201+ proxy .ModifyResponse = h .cacheResponse (logCtx , hashKey )
206202 logger .Debug ().Msg ("will cache response from downstream" )
207- h . proxy .ServeHTTP (res , req )
203+ proxy .ServeHTTP (res , req )
208204 return
209205 }
210206
0 commit comments