mirror of
https://mau.dev/maunium/synapse.git
synced 2024-12-14 20:13:50 +01:00
fix style inconsistencies
This commit is contained in:
parent
663d9db8e7
commit
ea69a84bbb
4 changed files with 51 additions and 18 deletions
|
@ -1 +1,2 @@
|
||||||
Fix race condition in populating reserved users
|
Fix race condition where config defined reserved users were not being added to
|
||||||
|
the monthly active user list prior to the homeserver reactor firing up
|
||||||
|
|
|
@ -33,21 +33,23 @@ class MonthlyActiveUsersStore(SQLBaseStore):
|
||||||
self._clock = hs.get_clock()
|
self._clock = hs.get_clock()
|
||||||
self.hs = hs
|
self.hs = hs
|
||||||
self.reserved_users = ()
|
self.reserved_users = ()
|
||||||
self.initialise_reserved_users(
|
# Do not add more reserved users than the total allowable number
|
||||||
dbconn.cursor(), hs.config.mau_limits_reserved_threepids
|
self._initialise_reserved_users(
|
||||||
|
dbconn.cursor(),
|
||||||
|
hs.config.mau_limits_reserved_threepids[:self.hs.config.max_mau_value],
|
||||||
)
|
)
|
||||||
|
|
||||||
def initialise_reserved_users(self, txn, threepids):
|
def _initialise_reserved_users(self, txn, threepids):
|
||||||
"""Ensures that reserved threepids are accounted for in the MAU table, should
|
"""Ensures that reserved threepids are accounted for in the MAU table, should
|
||||||
be called on start up.
|
be called on start up.
|
||||||
|
|
||||||
Arguments:
|
Args:
|
||||||
threepids []: List of threepid dicts to reserve
|
txn (cursor):
|
||||||
|
threepids (list[dict]): List of threepid dicts to reserve
|
||||||
"""
|
"""
|
||||||
reserved_user_list = []
|
reserved_user_list = []
|
||||||
|
|
||||||
# Do not add more reserved users than the total allowable number
|
for tp in threepids:
|
||||||
for tp in threepids[:self.hs.config.max_mau_value]:
|
|
||||||
user_id = self.get_user_id_by_threepid_txn(
|
user_id = self.get_user_id_by_threepid_txn(
|
||||||
txn,
|
txn,
|
||||||
tp["medium"], tp["address"]
|
tp["medium"], tp["address"]
|
||||||
|
@ -172,24 +174,34 @@ class MonthlyActiveUsersStore(SQLBaseStore):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def upsert_monthly_active_user(self, user_id):
|
def upsert_monthly_active_user(self, user_id):
|
||||||
"""Updates or inserts monthly active user member
|
"""Updates or inserts the user into the monthly active user table, which
|
||||||
Arguments:
|
is used to track the current MAU usage of the server
|
||||||
|
|
||||||
|
Args:
|
||||||
user_id (str): user to add/update
|
user_id (str): user to add/update
|
||||||
"""
|
"""
|
||||||
is_insert = yield self.runInteraction(
|
is_insert = yield self.runInteraction(
|
||||||
"upsert_monthly_active_user", self.upsert_monthly_active_user_txn,
|
"upsert_monthly_active_user", self.upsert_monthly_active_user_txn,
|
||||||
user_id
|
user_id
|
||||||
)
|
)
|
||||||
|
# Considered pushing cache invalidation down into txn method, but
|
||||||
|
# did not because txn is not a LoggingTransaction. This means I could not
|
||||||
|
# call txn.call_after(). Therefore cache is altered in background thread
|
||||||
|
# and calls from elsewhere to user_last_seen_monthly_active and
|
||||||
|
# get_monthly_active_count fail with ValueError in
|
||||||
|
# synapse/util/caches/descriptors.py#check_thread
|
||||||
if is_insert:
|
if is_insert:
|
||||||
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(())
|
||||||
|
|
||||||
def upsert_monthly_active_user_txn(self, txn, user_id):
|
def upsert_monthly_active_user_txn(self, txn, user_id):
|
||||||
"""
|
"""Updates or inserts monthly active user member
|
||||||
Updates or inserts monthly active user member
|
|
||||||
Arguments:
|
Args:
|
||||||
txn (cursor):
|
txn (cursor):
|
||||||
user_id (str): user to add/update
|
user_id (str): user to add/update
|
||||||
|
|
||||||
|
Returns:
|
||||||
bool: True if a new entry was created, False if an
|
bool: True if a new entry was created, False if an
|
||||||
existing one was updated.
|
existing one was updated.
|
||||||
"""
|
"""
|
||||||
|
@ -207,6 +219,7 @@ class MonthlyActiveUsersStore(SQLBaseStore):
|
||||||
"timestamp": int(self._clock.time_msec()),
|
"timestamp": int(self._clock.time_msec()),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
return is_insert
|
return is_insert
|
||||||
|
|
||||||
@cached(num_args=1)
|
@cached(num_args=1)
|
||||||
|
|
|
@ -474,6 +474,15 @@ class RegistrationStore(RegistrationWorkerStore,
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_user_id_by_threepid(self, medium, address):
|
def get_user_id_by_threepid(self, medium, address):
|
||||||
|
"""Returns user id from threepid
|
||||||
|
|
||||||
|
Args:
|
||||||
|
medium (str): threepid medium e.g. email
|
||||||
|
address (str): threepid address e.g. me@example.com
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Deferred[str|None]: user id or None if no user id/threepid mapping exists
|
||||||
|
"""
|
||||||
user_id = yield self.runInteraction(
|
user_id = yield self.runInteraction(
|
||||||
"get_user_id_by_threepid", self.get_user_id_by_threepid_txn,
|
"get_user_id_by_threepid", self.get_user_id_by_threepid_txn,
|
||||||
medium, address
|
medium, address
|
||||||
|
@ -481,6 +490,16 @@ class RegistrationStore(RegistrationWorkerStore,
|
||||||
defer.returnValue(user_id)
|
defer.returnValue(user_id)
|
||||||
|
|
||||||
def get_user_id_by_threepid_txn(self, txn, medium, address):
|
def get_user_id_by_threepid_txn(self, txn, medium, address):
|
||||||
|
"""Returns user id from threepid
|
||||||
|
|
||||||
|
Args:
|
||||||
|
txn (cursor):
|
||||||
|
medium (str): threepid medium e.g. email
|
||||||
|
address (str): threepid address e.g. me@example.com
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str|None: user id or None if no user id/threepid mapping exists
|
||||||
|
"""
|
||||||
ret = self._simple_select_one_txn(
|
ret = self._simple_select_one_txn(
|
||||||
txn,
|
txn,
|
||||||
"user_threepids",
|
"user_threepids",
|
||||||
|
|
|
@ -54,7 +54,7 @@ class MonthlyActiveUsersTestCase(HomeserverTestCase):
|
||||||
self.store.user_add_threepid(user2, "email", user2_email, now, now)
|
self.store.user_add_threepid(user2, "email", user2_email, now, now)
|
||||||
|
|
||||||
self.store.runInteraction(
|
self.store.runInteraction(
|
||||||
"initialise", self.store.initialise_reserved_users, threepids
|
"initialise", self.store._initialise_reserved_users, threepids
|
||||||
)
|
)
|
||||||
self.pump()
|
self.pump()
|
||||||
|
|
||||||
|
@ -203,7 +203,7 @@ class MonthlyActiveUsersTestCase(HomeserverTestCase):
|
||||||
]
|
]
|
||||||
self.hs.config.mau_limits_reserved_threepids = threepids
|
self.hs.config.mau_limits_reserved_threepids = threepids
|
||||||
self.store.runInteraction(
|
self.store.runInteraction(
|
||||||
"initialise", self.store.initialise_reserved_users, threepids
|
"initialise", self.store._initialise_reserved_users, threepids
|
||||||
)
|
)
|
||||||
|
|
||||||
self.pump()
|
self.pump()
|
||||||
|
|
Loading…
Reference in a new issue