Skip to content

Commit

Permalink
async/await get_user_id_by_threepid (matrix-org#7620)
Browse files Browse the repository at this point in the history
Based on matrix-org#7619 

async's `get_user_id_by_threepid` and its call stack.
  • Loading branch information
anoadragon453 authored and phil-flex committed Jun 16, 2020
1 parent f9016db commit dfce9c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
1 change: 1 addition & 0 deletions changelog.d/7620.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert `get_user_id_by_threepid` to async/await.
9 changes: 4 additions & 5 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,18 +617,17 @@ def reap_monthly_active_users():
clock.looping_call(reap_monthly_active_users, 1000 * 60 * 60)
reap_monthly_active_users()

@defer.inlineCallbacks
def generate_monthly_active_users():
async def generate_monthly_active_users():
current_mau_count = 0
current_mau_count_by_service = {}
reserved_users = ()
store = hs.get_datastore()
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 = (
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))

for app_service, count in current_mau_count_by_service.items():
Expand Down
25 changes: 12 additions & 13 deletions synapse/storage/data_stores/main/monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import List

from twisted.internet import defer

Expand Down Expand Up @@ -77,20 +78,19 @@ def _count_users_by_service(txn):

return self.db.runInteraction("count_users_by_service", _count_users_by_service)

@defer.inlineCallbacks
def get_registered_reserved_users(self):
"""Of the reserved threepids defined in config, which are associated
with registered users?
async def get_registered_reserved_users(self) -> List[str]:
"""Of the reserved threepids defined in config, retrieve those that are associated
with registered users
Returns:
Defered[list]: Real reserved users
User IDs of actual users that are reserved
"""
users = []

for tp in self.hs.config.mau_limits_reserved_threepids[
: 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"]
)
if user_id:
Expand Down Expand Up @@ -171,13 +171,9 @@ def _initialise_reserved_users(self, txn, threepids):
else:
logger.warning("mau limit reserved threepid %s not found in db" % tp)

@defer.inlineCallbacks
def reap_monthly_active_users(self):
async def reap_monthly_active_users(self):
"""Cleans out monthly active user table to ensure that no stale
entries exist.
Returns:
Deferred[]
"""

def _reap_users(txn, reserved_users):
Expand Down Expand Up @@ -249,8 +245,8 @@ def _reap_users(txn, reserved_users):
)
self._invalidate_cache_and_stream(txn, self.get_monthly_active_count, ())

reserved_users = yield self.get_registered_reserved_users()
yield self.db.runInteraction(
reserved_users = await self.get_registered_reserved_users()
await self.db.runInteraction(
"reap_monthly_active_users", _reap_users, reserved_users
)

Expand All @@ -261,6 +257,9 @@ def upsert_monthly_active_user(self, user_id):
Args:
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
# from upsert_monthly_active_user_txn because then I need a _txn form of
Expand Down
22 changes: 12 additions & 10 deletions synapse/storage/data_stores/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import logging
import re
from typing import Optional

from six import iterkeys

Expand Down Expand Up @@ -342,7 +343,7 @@ def is_real_user(self, user_id):
)
return res

@cachedInlineCallbacks()
@cached()
def is_support_user(self, user_id):
"""Determines if the user is of type UserTypes.SUPPORT
Expand All @@ -352,10 +353,9 @@ def is_support_user(self, user_id):
Returns:
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
)
return res

def is_real_user_txn(self, txn, user_id):
res = self.db.simple_select_one_onecol_txn(
Expand Down Expand Up @@ -516,18 +516,17 @@ def _find_next_generated_user_id(txn):
)
)

@defer.inlineCallbacks
def get_user_id_by_threepid(self, medium, address):
async def get_user_id_by_threepid(self, medium: str, address: str) -> Optional[str]:
"""Returns user id from threepid
Args:
medium (str): threepid medium e.g. email
address (str): threepid address e.g. me@example.com
medium: threepid medium e.g. email
address: threepid address e.g. me@example.com
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
)
return user_id
Expand Down Expand Up @@ -993,7 +992,7 @@ def register_user(
Args:
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
upgraded to a non-guest account.
make_guest (boolean): True if the the new user should be guest,
Expand All @@ -1007,6 +1006,9 @@ def register_user(
Raises:
StoreError if the user_id could not be registered.
Returns:
Deferred
"""
return self.db.runInteraction(
"register_user",
Expand Down

0 comments on commit dfce9c0

Please sign in to comment.