Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3029 from matrix-org/erikj/linearize_generate_use…
Browse files Browse the repository at this point in the history
…r_id

Linearize calls to _generate_user_id
  • Loading branch information
erikjohnston authored Mar 28, 2018
2 parents e6d87c9 + 3e0c066 commit f879127
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 10 additions & 4 deletions synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from synapse.http.client import CaptchaServerHttpClient
from synapse import types
from synapse.types import UserID
from synapse.util.async import run_on_reactor
from synapse.util.async import run_on_reactor, Linearizer
from synapse.util.threepids import check_3pid_allowed
from ._base import BaseHandler

Expand All @@ -46,6 +46,10 @@ def __init__(self, hs):

self.macaroon_gen = hs.get_macaroon_generator()

self._generate_user_id_linearizer = Linearizer(
name="_generate_user_id_linearizer",
)

@defer.inlineCallbacks
def check_username(self, localpart, guest_access_token=None,
assigned_user_id=None):
Expand Down Expand Up @@ -345,9 +349,11 @@ def check_user_id_not_appservice_exclusive(self, user_id, allowed_appservice=Non
@defer.inlineCallbacks
def _generate_user_id(self, reseed=False):
if reseed or self._next_generated_user_id is None:
self._next_generated_user_id = (
yield self.store.find_next_generated_user_id_localpart()
)
with (yield self._generate_user_id_linearizer.queue(())):
if reseed or self._next_generated_user_id is None:
self._next_generated_user_id = (
yield self.store.find_next_generated_user_id_localpart()
)

id = self._next_generated_user_id
self._next_generated_user_id += 1
Expand Down
4 changes: 1 addition & 3 deletions synapse/storage/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,14 +460,12 @@ def find_next_generated_user_id_localpart(self):
"""
def _find_next_generated_user_id(txn):
txn.execute("SELECT name FROM users")
rows = self.cursor_to_dict(txn)

regex = re.compile("^@(\d+):")

found = set()

for r in rows:
user_id = r["name"]
for user_id, in txn:
match = regex.search(user_id)
if match:
found.add(int(match.group(1)))
Expand Down

0 comments on commit f879127

Please sign in to comment.