@@ -39,7 +39,7 @@ func plainTextAgents() []string {
39
39
}
40
40
}
41
41
42
- type responseWithHeader struct {
42
+ type ResponseWithHeader struct {
43
43
InProgress bool // true if the request is being processed
44
44
Expires time.Time // expiration time of the cache entry
45
45
@@ -105,14 +105,12 @@ func (rp *RequestProcessor) Start() error {
105
105
return rp .startPeakHandling ()
106
106
}
107
107
108
- func (rp * RequestProcessor ) ProcessRequest (r * http.Request ) (* responseWithHeader , error ) {
108
+ func (rp * RequestProcessor ) ProcessRequest (r * http.Request ) (* ResponseWithHeader , error ) {
109
109
var (
110
- response * responseWithHeader
111
- cacheEntry responseWithHeader
112
- err error
110
+ response * ResponseWithHeader
111
+ ip = util .ReadUserIP (r )
113
112
)
114
113
115
- ip := util .ReadUserIP (r )
116
114
if ip != "127.0.0.1" {
117
115
rp .stats .Inc ("total" )
118
116
}
@@ -137,46 +135,68 @@ func (rp *RequestProcessor) ProcessRequest(r *http.Request) (*responseWithHeader
137
135
return get (r , rp .upstreamTransport )
138
136
}
139
137
138
+ // processing cached request
140
139
cacheDigest := getCacheDigest (r )
141
140
142
- foundInCache := false
143
-
144
141
rp .savePeakRequest (cacheDigest , r )
145
142
146
- cacheBody , ok : = rp .lruCache . Get ( cacheDigest )
147
- if ok {
148
- cacheEntry , ok = cacheBody .( responseWithHeader )
143
+ response = rp .processRequestFromCache ( r )
144
+ if response != nil {
145
+ return response , nil
149
146
}
150
- if ok {
151
- rp .stats .Inc ("cache1" )
152
147
153
- // if after all attempts we still have no answer,
154
- // we try to make the query on our own
155
- for attempts := 0 ; attempts < 300 ; attempts ++ {
156
- if ! ok || ! cacheEntry .InProgress {
157
- break
158
- }
159
- time .Sleep (30 * time .Millisecond )
160
- cacheBody , ok = rp .lruCache .Get (cacheDigest )
161
- if ok && cacheBody != nil {
162
- if v , ok := cacheBody .(responseWithHeader ); ok {
163
- cacheEntry = v
164
- }
165
- }
166
- }
167
- if cacheEntry .InProgress {
168
- log .Printf ("TIMEOUT: %s\n " , cacheDigest )
148
+ return rp .processUncachedRequest (r )
149
+ }
150
+
151
+ // processRequestFromCache processes requests using the cache.
152
+ // If no entry in cache found, nil is returned.
153
+ func (rp * RequestProcessor ) processRequestFromCache (r * http.Request ) * ResponseWithHeader {
154
+ var (
155
+ cacheEntry ResponseWithHeader
156
+ cacheDigest = getCacheDigest (r )
157
+ ok bool
158
+ )
159
+
160
+ cacheBody , _ := rp .lruCache .Get (cacheDigest )
161
+ cacheEntry , ok = cacheBody .(ResponseWithHeader )
162
+ if ! ok {
163
+ return nil
164
+ }
165
+
166
+ // if after all attempts we still have no answer,
167
+ // we try to make the query on our own
168
+ for attempts := 0 ; attempts < 300 ; attempts ++ {
169
+ if ! ok || ! cacheEntry .InProgress {
170
+ break
169
171
}
170
- if ok && ! cacheEntry .InProgress && cacheEntry .Expires .After (time .Now ()) {
171
- response = & cacheEntry
172
- foundInCache = true
172
+ time .Sleep (30 * time .Millisecond )
173
+ cacheBody , _ = rp .lruCache .Get (cacheDigest )
174
+ v , ok := cacheBody .(ResponseWithHeader )
175
+ if ok {
176
+ cacheEntry = v
173
177
}
174
178
}
179
+ if cacheEntry .InProgress {
180
+ log .Printf ("TIMEOUT: %s\n " , cacheDigest )
181
+ }
182
+ if ok && ! cacheEntry .InProgress && cacheEntry .Expires .After (time .Now ()) {
183
+ rp .stats .Inc ("cache1" )
175
184
176
- if foundInCache {
177
- return response , nil
185
+ return & cacheEntry
178
186
}
179
187
188
+ return nil
189
+ }
190
+
191
+ // processUncachedRequest processes requests that were not found in the cache.
192
+ func (rp * RequestProcessor ) processUncachedRequest (r * http.Request ) (* ResponseWithHeader , error ) {
193
+ var (
194
+ cacheDigest = getCacheDigest (r )
195
+ ip = util .ReadUserIP (r )
196
+ response * ResponseWithHeader
197
+ err error
198
+ )
199
+
180
200
// Response was not found in cache.
181
201
// Starting real handling.
182
202
format := r .URL .Query ().Get ("format" )
@@ -187,13 +207,14 @@ func (rp *RequestProcessor) ProcessRequest(r *http.Request) (*responseWithHeader
187
207
}
188
208
}
189
209
190
- // How many IP addresses are known.
210
+ // Count, how many IP addresses are known.
191
211
_ , err = rp .geoIPCache .Read (ip )
192
212
if err == nil {
193
213
rp .stats .Inc ("geoip" )
194
214
}
195
215
196
- rp .lruCache .Add (cacheDigest , responseWithHeader {InProgress : true })
216
+ // Indicate, that the request is being handled.
217
+ rp .lruCache .Add (cacheDigest , ResponseWithHeader {InProgress : true })
197
218
198
219
response , err = get (r , rp .upstreamTransport )
199
220
if err != nil {
@@ -209,7 +230,7 @@ func (rp *RequestProcessor) ProcessRequest(r *http.Request) (*responseWithHeader
209
230
return response , nil
210
231
}
211
232
212
- func get (req * http.Request , transport * http.Transport ) (* responseWithHeader , error ) {
233
+ func get (req * http.Request , transport * http.Transport ) (* ResponseWithHeader , error ) {
213
234
client := & http.Client {
214
235
Transport : transport ,
215
236
}
@@ -245,7 +266,7 @@ func get(req *http.Request, transport *http.Transport) (*responseWithHeader, err
245
266
return nil , err
246
267
}
247
268
248
- return & responseWithHeader {
269
+ return & ResponseWithHeader {
249
270
InProgress : false ,
250
271
Expires : time .Now ().Add (time .Duration (randInt (1000 , 1500 )) * time .Second ),
251
272
Body : body ,
@@ -282,7 +303,7 @@ func dontCache(req *http.Request) bool {
282
303
// Insecure queries are marked by the frontend web server
283
304
// with X-Forwarded-Proto header:
284
305
// `proxy_set_header X-Forwarded-Proto $scheme;`.
285
- func redirectInsecure (req * http.Request ) (* responseWithHeader , bool ) {
306
+ func redirectInsecure (req * http.Request ) (* ResponseWithHeader , bool ) {
286
307
if isPlainTextAgent (req .Header .Get ("User-Agent" )) {
287
308
return nil , false
288
309
}
@@ -304,7 +325,7 @@ The document has moved
304
325
</BODY></HTML>
305
326
` , target ))
306
327
307
- return & responseWithHeader {
328
+ return & ResponseWithHeader {
308
329
InProgress : false ,
309
330
Expires : time .Now ().Add (time .Duration (randInt (1000 , 1500 )) * time .Second ),
310
331
Body : body ,
@@ -340,8 +361,8 @@ func ipFromAddr(s string) string {
340
361
}
341
362
342
363
// fromCadre converts Cadre into a responseWithHeader.
343
- func fromCadre (cadre * routing.Cadre ) * responseWithHeader {
344
- return & responseWithHeader {
364
+ func fromCadre (cadre * routing.Cadre ) * ResponseWithHeader {
365
+ return & ResponseWithHeader {
345
366
Body : cadre .Body ,
346
367
Expires : cadre .Expires ,
347
368
StatusCode : 200 ,
0 commit comments