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 #2233 from matrix-org/erikj/faster_as_check
Browse files Browse the repository at this point in the history
Make get_if_app_services_interested_in_user faster
  • Loading branch information
erikjohnston authored May 18, 2017
2 parents c57789d + 760625a commit 234772d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
10 changes: 10 additions & 0 deletions synapse/appservice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ def is_exclusive_alias(self, alias):
def is_exclusive_room(self, room_id):
return self._is_exclusive(ApplicationService.NS_ROOMS, room_id)

def get_exlusive_user_regexes(self):
"""Get the list of regexes used to determine if a user is exclusively
registered by the AS
"""
return [
regex_obj["regex"]
for regex_obj in self.namespaces[ApplicationService.NS_USERS]
if regex_obj["exclusive"]
]

def is_rate_limited(self):
return self.rate_limited

Expand Down
4 changes: 1 addition & 3 deletions synapse/push/bulk_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,7 @@ def get_rules(self, context):
if not self.is_mine_id(user_id):
continue

if self.store.get_if_app_services_interested_in_user(
user_id, exclusive=True
):
if self.store.get_if_app_services_interested_in_user(user_id):
continue

# If a user has left a room we remove their push rule. If they
Expand Down
31 changes: 22 additions & 9 deletions synapse/storage/appservice.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
import re
import simplejson as json
from twisted.internet import defer

Expand All @@ -36,19 +37,31 @@ def __init__(self, hs):
hs.config.app_service_config_files
)

# We precompie a regex constructed from all the regexes that the AS's
# have registered for exclusive users.
exclusive_user_regexes = [
regex.pattern
for service in self.services_cache
for regex in service.get_exlusive_user_regexes()
]
if exclusive_user_regexes:
exclusive_user_regex = "|".join("(" + r + ")" for r in exclusive_user_regexes)
self.exclusive_user_regex = re.compile(exclusive_user_regex)
else:
# We handle this case specially otherwise the constructed regex
# will always match
self.exclusive_user_regex = None

def get_app_services(self):
return self.services_cache

def get_if_app_services_interested_in_user(self, user_id, exclusive=False):
"""Check if the user is one associated with an app service
def get_if_app_services_interested_in_user(self, user_id):
"""Check if the user is one associated with an app service (exclusively)
"""
for service in self.services_cache:
if service.is_interested_in_user(user_id):
if exclusive:
return service.is_exclusive_user(user_id)
else:
return True
return False
if self.exclusive_user_regex:
return bool(self.exclusive_user_regex.match(user_id))
else:
return False

def get_app_service_by_user_id(self, user_id):
"""Retrieve an application service from their user ID.
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/push_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def _bulk_get_push_rules_for_room(self, room_id, state_group, current_state_ids,
local_users_in_room = set(
u for u in users_in_room
if self.hs.is_mine_id(u)
and not self.get_if_app_services_interested_in_user(u, exclusive=True)
and not self.get_if_app_services_interested_in_user(u)
)

# users in the room who have pushers need to get push rules run because
Expand Down

0 comments on commit 234772d

Please sign in to comment.