4242 metricsCollector MetricsCollector
4343 cfg config.CacheConfig
4444 httpClient * http.Client
45+ proxy * httputil.ReverseProxy
4546 }
4647)
4748
@@ -119,26 +120,35 @@ func errHandler(res http.ResponseWriter, req *http.Request, err error) {
119120
120121func newCacheHandler (c Cacher , m MetricsCollector , w Worker , cfg config.CacheConfig ) handler {
121122 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 {
126125 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 ,
129132 TLSHandshakeTimeout : 10 * time .Second ,
130- ResponseHeaderTimeout : 10 * time .Second ,
133+ ExpectContinueTimeout : 1 * time .Second ,
134+ ResponseHeaderTimeout : 30 * time .Second ,
131135 }
132136
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+
133145 return handler {
134146 cacher : c ,
135147 worker : w ,
136148 metricsCollector : m ,
137149 cfg : cfg ,
138- httpClient : & http.Client {
139- Timeout : time .Second * 10 ,
140- Transport : netTransport ,
141- },
150+ httpClient : httpClient ,
151+ proxy : proxy ,
142152 }
143153}
144154
@@ -165,23 +175,22 @@ func (h handler) asyncCacheRevalidate(hashKey string, req *http.Request) func()
165175}
166176
167177func (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
170179 logger := log .With ().Str ("path" , req .URL .Path ).Str ("method" , req .Method ).Logger ()
171180 logCtx := logger .WithContext (req .Context ())
172181
173182 // websockets
174183 if strings .ToLower (req .Header .Get ("connection" )) == "upgrade" {
175184 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 )
178187 return
179188 }
180189
181190 if strings .ToLower (req .Method ) != "get" {
182191 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 )
185194 return
186195 }
187196
@@ -193,10 +202,9 @@ func (h handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
193202 }
194203
195204 if result == nil {
196- proxy .ModifyResponse = h .cacheResponse (logCtx , hashKey )
205+ h . proxy .ModifyResponse = h .cacheResponse (logCtx , hashKey )
197206 logger .Debug ().Msg ("will cache response from downstream" )
198- proxy .ServeHTTP (res , req )
199-
207+ h .proxy .ServeHTTP (res , req )
200208 return
201209 }
202210
0 commit comments