async/await get_user_id_by_threepid (#7620)

Based on #7619 

async's `get_user_id_by_threepid` and its call stack.
This commit is contained in:
Andrew Morgan 2020-06-03 17:15:57 +01:00 committed by GitHub
parent 86d814cdde
commit e91abfd291
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 28 deletions

1
changelog.d/7620.misc Normal file
View File

@ -0,0 +1 @@
Convert `get_user_id_by_threepid` to async/await.

View File

@ -617,18 +617,17 @@ def run(hs):
clock.looping_call(reap_monthly_active_users, 1000 * 60 * 60) clock.looping_call(reap_monthly_active_users, 1000 * 60 * 60)
reap_monthly_active_users() reap_monthly_active_users()
@defer.inlineCallbacks async def generate_monthly_active_users():
def generate_monthly_active_users():
current_mau_count = 0 current_mau_count = 0
current_mau_count_by_service = {} current_mau_count_by_service = {}
reserved_users = () reserved_users = ()
store = hs.get_datastore() store = hs.get_datastore()
if hs.config.limit_usage_by_mau or hs.config.mau_stats_only: if hs.config.limit_usage_by_mau or hs.config.mau_stats_only:
current_mau_count = yield store.get_monthly_active_count() current_mau_count = await store.get_monthly_active_count()
current_mau_count_by_service = ( current_mau_count_by_service = (
yield store.get_monthly_active_count_by_service() await store.get_monthly_active_count_by_service()
) )
reserved_users = yield store.get_registered_reserved_users() reserved_users = await store.get_registered_reserved_users()
current_mau_gauge.set(float(current_mau_count)) current_mau_gauge.set(float(current_mau_count))
for app_service, count in current_mau_count_by_service.items(): for app_service, count in current_mau_count_by_service.items():

View File

@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging import logging
from typing import List
from twisted.internet import defer from twisted.internet import defer
@ -77,20 +78,19 @@ class MonthlyActiveUsersWorkerStore(SQLBaseStore):
return self.db.runInteraction("count_users_by_service", _count_users_by_service) return self.db.runInteraction("count_users_by_service", _count_users_by_service)
@defer.inlineCallbacks async def get_registered_reserved_users(self) -> List[str]:
def get_registered_reserved_users(self): """Of the reserved threepids defined in config, retrieve those that are associated
"""Of the reserved threepids defined in config, which are associated with registered users
with registered users?
Returns: Returns:
Defered[list]: Real reserved users User IDs of actual users that are reserved
""" """
users = [] users = []
for tp in self.hs.config.mau_limits_reserved_threepids[ for tp in self.hs.config.mau_limits_reserved_threepids[
: self.hs.config.max_mau_value : self.hs.config.max_mau_value
]: ]:
user_id = yield self.hs.get_datastore().get_user_id_by_threepid( user_id = await self.hs.get_datastore().get_user_id_by_threepid(
tp["medium"], tp["address"] tp["medium"], tp["address"]
) )
if user_id: if user_id:
@ -171,13 +171,9 @@ class MonthlyActiveUsersStore(MonthlyActiveUsersWorkerStore):
else: else:
logger.warning("mau limit reserved threepid %s not found in db" % tp) logger.warning("mau limit reserved threepid %s not found in db" % tp)
@defer.inlineCallbacks async def reap_monthly_active_users(self):
def reap_monthly_active_users(self):
"""Cleans out monthly active user table to ensure that no stale """Cleans out monthly active user table to ensure that no stale
entries exist. entries exist.
Returns:
Deferred[]
""" """
def _reap_users(txn, reserved_users): def _reap_users(txn, reserved_users):
@ -249,8 +245,8 @@ class MonthlyActiveUsersStore(MonthlyActiveUsersWorkerStore):
) )
self._invalidate_cache_and_stream(txn, self.get_monthly_active_count, ()) self._invalidate_cache_and_stream(txn, self.get_monthly_active_count, ())
reserved_users = yield self.get_registered_reserved_users() reserved_users = await self.get_registered_reserved_users()
yield self.db.runInteraction( await self.db.runInteraction(
"reap_monthly_active_users", _reap_users, reserved_users "reap_monthly_active_users", _reap_users, reserved_users
) )
@ -261,6 +257,9 @@ class MonthlyActiveUsersStore(MonthlyActiveUsersWorkerStore):
Args: Args:
user_id (str): user to add/update user_id (str): user to add/update
Returns:
Deferred
""" """
# Support user never to be included in MAU stats. Note I can't easily call this # Support user never to be included in MAU stats. Note I can't easily call this
# from upsert_monthly_active_user_txn because then I need a _txn form of # from upsert_monthly_active_user_txn because then I need a _txn form of

View File

@ -17,6 +17,7 @@
import logging import logging
import re import re
from typing import Optional
from six import iterkeys from six import iterkeys
@ -342,7 +343,7 @@ class RegistrationWorkerStore(SQLBaseStore):
) )
return res return res
@cachedInlineCallbacks() @cached()
def is_support_user(self, user_id): def is_support_user(self, user_id):
"""Determines if the user is of type UserTypes.SUPPORT """Determines if the user is of type UserTypes.SUPPORT
@ -352,10 +353,9 @@ class RegistrationWorkerStore(SQLBaseStore):
Returns: Returns:
Deferred[bool]: True if user is of type UserTypes.SUPPORT Deferred[bool]: True if user is of type UserTypes.SUPPORT
""" """
res = yield self.db.runInteraction( return self.db.runInteraction(
"is_support_user", self.is_support_user_txn, user_id "is_support_user", self.is_support_user_txn, user_id
) )
return res
def is_real_user_txn(self, txn, user_id): def is_real_user_txn(self, txn, user_id):
res = self.db.simple_select_one_onecol_txn( res = self.db.simple_select_one_onecol_txn(
@ -516,18 +516,17 @@ class RegistrationWorkerStore(SQLBaseStore):
) )
) )
@defer.inlineCallbacks async def get_user_id_by_threepid(self, medium: str, address: str) -> Optional[str]:
def get_user_id_by_threepid(self, medium, address):
"""Returns user id from threepid """Returns user id from threepid
Args: Args:
medium (str): threepid medium e.g. email medium: threepid medium e.g. email
address (str): threepid address e.g. me@example.com address: threepid address e.g. me@example.com
Returns: Returns:
Deferred[str|None]: user id or None if no user id/threepid mapping exists The user ID or None if no user id/threepid mapping exists
""" """
user_id = yield self.db.runInteraction( user_id = await self.db.runInteraction(
"get_user_id_by_threepid", self.get_user_id_by_threepid_txn, medium, address "get_user_id_by_threepid", self.get_user_id_by_threepid_txn, medium, address
) )
return user_id return user_id
@ -993,7 +992,7 @@ class RegistrationStore(RegistrationBackgroundUpdateStore):
Args: Args:
user_id (str): The desired user ID to register. user_id (str): The desired user ID to register.
password_hash (str): Optional. The password hash for this user. password_hash (str|None): Optional. The password hash for this user.
was_guest (bool): Optional. Whether this is a guest account being was_guest (bool): Optional. Whether this is a guest account being
upgraded to a non-guest account. upgraded to a non-guest account.
make_guest (boolean): True if the the new user should be guest, make_guest (boolean): True if the the new user should be guest,
@ -1007,6 +1006,9 @@ class RegistrationStore(RegistrationBackgroundUpdateStore):
Raises: Raises:
StoreError if the user_id could not be registered. StoreError if the user_id could not be registered.
Returns:
Deferred
""" """
return self.db.runInteraction( return self.db.runInteraction(
"register_user", "register_user",