make use of _simple_select_one_onecol, improved comments

This commit is contained in:
Neil Johnson 2018-08-06 17:51:15 +01:00
parent e40a510fbf
commit 16d78be315
2 changed files with 15 additions and 8 deletions

View File

@ -15,7 +15,7 @@
from twisted.internet import defer from twisted.internet import defer
from synapse.util.caches.descriptors import cached, cachedInlineCallbacks from synapse.util.caches.descriptors import cached
from ._base import SQLBaseStore from ._base import SQLBaseStore
@ -104,7 +104,7 @@ class MonthlyActiveUsersStore(SQLBaseStore):
self._user_last_seen_monthly_active.invalidate((user_id,)) self._user_last_seen_monthly_active.invalidate((user_id,))
self.get_monthly_active_count.invalidate(()) self.get_monthly_active_count.invalidate(())
@cachedInlineCallbacks(num_args=1) @cached(num_args=1)
def _user_last_seen_monthly_active(self, user_id): def _user_last_seen_monthly_active(self, user_id):
""" """
Checks if a given user is part of the monthly active user group Checks if a given user is part of the monthly active user group
@ -114,18 +114,16 @@ class MonthlyActiveUsersStore(SQLBaseStore):
int : timestamp since last seen, None if never seen int : timestamp since last seen, None if never seen
""" """
result = yield self._simple_select_onecol(
return(self._simple_select_one_onecol(
table="monthly_active_users", table="monthly_active_users",
keyvalues={ keyvalues={
"user_id": user_id, "user_id": user_id,
}, },
retcol="timestamp", retcol="timestamp",
allow_none=True,
desc="_user_last_seen_monthly_active", desc="_user_last_seen_monthly_active",
) ))
timestamp = None
if len(result) > 0:
timestamp = result[0]
defer.returnValue(timestamp)
@defer.inlineCallbacks @defer.inlineCallbacks
def populate_monthly_active_users(self, user_id): def populate_monthly_active_users(self, user_id):
@ -140,6 +138,11 @@ class MonthlyActiveUsersStore(SQLBaseStore):
last_seen_timestamp = yield self._user_last_seen_monthly_active(user_id) last_seen_timestamp = yield self._user_last_seen_monthly_active(user_id)
now = self.hs.get_clock().time_msec() now = self.hs.get_clock().time_msec()
# We want to reduce to the total number of db writes, and are happy
# to trade accuracy of timestamp in order to lighten load. This means
# We always insert new users (where MAU threshold has not been reached),
# but only update if we have not previously seen the user for
# LAST_SEEN_GRANULARITY ms
if last_seen_timestamp is None: if last_seen_timestamp is None:
count = yield self.get_monthly_active_count() count = yield self.get_monthly_active_count()
if count < self.hs.config.max_mau_value: if count < self.hs.config.max_mau_value:

View File

@ -16,6 +16,10 @@
-- a table of monthly active users, for use where blocking based on mau limits -- a table of monthly active users, for use where blocking based on mau limits
CREATE TABLE monthly_active_users ( CREATE TABLE monthly_active_users (
user_id TEXT NOT NULL, user_id TEXT NOT NULL,
-- Last time we saw the user. Not guaranteed to be accurate due to rate limiting
-- on updates, Granularity of updates governed by
-- syanpse.storage.monthly_active_users.LAST_SEEN_GRANULARITY
-- Measured in ms since epoch.
timestamp BIGINT NOT NULL timestamp BIGINT NOT NULL
); );