Fix pruning of {SITE}_loggedin/out dicts.

For the past week, we noticed a gradual increase in CPU usage and
request times. Use of a sampling profiler revealed the time waas spent
in serializing/deserializing data stored in Redis. In particular, the
user counter dicts were filtered for calculation of the loggedin/out
counters, but the filtered versions were never stored.

To make concurrency safe, we still filter on every request, but at
least the resting data will eventually be appropriately filtered,
and this data is non-critical regardless.
remotes/1693176582716663532/tmp_refs/heads/watchparty
Snakes 2022-10-14 17:41:26 -04:00
parent b6703595da
commit 683d562058
Signed by: Snakes
GPG Key ID: E745A82778055C7E
1 changed files with 5 additions and 6 deletions

View File

@ -52,9 +52,6 @@ def get_logged_in_user():
loggedin = cache.get(f'{SITE}_loggedin') or {}
loggedout = cache.get(f'{SITE}_loggedout') or {}
g.loggedin_counter = 0
g.loggedout_counter = 0
timestamp = int(time.time())
if v:
if session["session_id"] in loggedout: del loggedout[session["session_id"]]
@ -69,12 +66,14 @@ def get_logged_in_user():
if 'spider' not in ua.lower() and 'bot' not in ua.lower():
loggedout[session["session_id"]] = (timestamp, ua)
g.loggedin_counter = len([x for x in loggedin.values() if timestamp-x < LOGGEDIN_ACTIVE_TIME])
loggedin = {k: v for k, v in loggedin.items() if (timestamp - v) < LOGGEDIN_ACTIVE_TIME}
loggedout = {k: v for k, v in loggedout.items() if (timestamp - v[0]) < LOGGEDIN_ACTIVE_TIME}
cache.set(f'{SITE}_loggedin', loggedin)
g.loggedout_counter = len([x for x in loggedout.values() if timestamp-x[0] < LOGGEDIN_ACTIVE_TIME])
cache.set(f'{SITE}_loggedout', loggedout)
g.loggedin_counter = len(loggedin)
g.loggedout_counter = len(loggedout)
g.v = v
if v: v.poor = session.get('poor')