From ef7d46723d8a1d00e6dd6dafa5d55e70032c9182 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Mon, 8 Jun 2020 07:26:38 +0200 Subject: [PATCH] dont cache cyclic requests (#460) --- cmd/processRequest.go | 19 ++++++++++++++++++- lib/cache.py | 13 +++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cmd/processRequest.go b/cmd/processRequest.go index 25209b6..7c44a9f 100644 --- a/cmd/processRequest.go +++ b/cmd/processRequest.go @@ -7,15 +7,21 @@ import ( "math/rand" "net" "net/http" + "strings" "time" ) func processRequest(r *http.Request) responseWithHeader { var response responseWithHeader - foundInCache := false + if dontCache(r) { + return get(r) + } + cacheDigest := getCacheDigest(r) + foundInCache := false + savePeakRequest(cacheDigest, r) cacheBody, ok := lruCache.Get(cacheDigest) @@ -109,6 +115,17 @@ func getCacheDigest(req *http.Request) string { return fmt.Sprintf("%s:%s%s:%s:%s", userAgent, queryHost, queryString, clientIPAddress, lang) } +// return true if request should not be cached +func dontCache(req *http.Request) bool { + + // dont cache cyclic requests + loc := strings.Split(req.RequestURI, "?")[0] + if strings.Contains(loc, ":") { + return true + } + return false +} + func readUserIP(r *http.Request) string { IPAddress := r.Header.Get("X-Real-Ip") if IPAddress == "" { diff --git a/lib/cache.py b/lib/cache.py index 2dd1151..3317cb7 100644 --- a/lib/cache.py +++ b/lib/cache.py @@ -34,8 +34,16 @@ def get_signature(user_agent, query_string, client_ip_address, lang): """ Get cache signature based on `user_agent`, `url_string`, `lang`, and `client_ip_address` + Return `None` if query should not be cached. """ + if "?" in query_string: + location = query_string.split("?", 1)[0] + else: + location = query_string + if ":" in location: + return None + signature = "%s:%s:%s:%s" % \ (user_agent, query_string, client_ip_address, lang) print(signature) @@ -48,6 +56,9 @@ def get(signature): the `_update_answer` function. """ + if not signature: + return None + value_record = CACHE.get(signature) if not value_record: return None @@ -69,6 +80,8 @@ def store(signature, value): """ Store in cache `value` for `signature` """ + if not signature: + return value if len(value) >= MIN_SIZE_FOR_FILECACHE: value_to_store = _store_in_file(signature, value)