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 #2076 from matrix-org/erikj/as_perf
Browse files Browse the repository at this point in the history
Make AS's faster
  • Loading branch information
erikjohnston authored Mar 31, 2017
2 parents 639d9ae + 69efd77 commit 350333a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
38 changes: 20 additions & 18 deletions synapse/appservice/__init__.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.
from synapse.api.constants import EventTypes
from synapse.util.caches.descriptors import cachedInlineCallbacks

from twisted.internet import defer

Expand Down Expand Up @@ -124,29 +125,23 @@ def _check_namespaces(self, namespaces):
raise ValueError(
"Expected bool for 'exclusive' in ns '%s'" % ns
)
if not isinstance(regex_obj.get("regex"), basestring):
regex = regex_obj.get("regex")
if isinstance(regex, basestring):
regex_obj["regex"] = re.compile(regex) # Pre-compile regex
else:
raise ValueError(
"Expected string for 'regex' in ns '%s'" % ns
)
return namespaces

def _matches_regex(self, test_string, namespace_key, return_obj=False):
if not isinstance(test_string, basestring):
logger.error(
"Expected a string to test regex against, but got %s",
test_string
)
return False

def _matches_regex(self, test_string, namespace_key):
for regex_obj in self.namespaces[namespace_key]:
if re.match(regex_obj["regex"], test_string):
if return_obj:
return regex_obj
return True
return False
if regex_obj["regex"].match(test_string):
return regex_obj
return None

def _is_exclusive(self, ns_key, test_string):
regex_obj = self._matches_regex(test_string, ns_key, return_obj=True)
regex_obj = self._matches_regex(test_string, ns_key)
if regex_obj:
return regex_obj["exclusive"]
return False
Expand All @@ -166,7 +161,14 @@ def _matches_user(self, event, store):
if not store:
defer.returnValue(False)

member_list = yield store.get_users_in_room(event.room_id)
does_match = yield self._matches_user_in_member_list(event.room_id, store)
defer.returnValue(does_match)

@cachedInlineCallbacks(num_args=1, cache_context=True)
def _matches_user_in_member_list(self, room_id, store, cache_context):
member_list = yield store.get_users_in_room(
room_id, on_invalidate=cache_context.invalidate
)

# check joined member events
for user_id in member_list:
Expand Down Expand Up @@ -219,10 +221,10 @@ def is_interested_in_user(self, user_id):
)

def is_interested_in_alias(self, alias):
return self._matches_regex(alias, ApplicationService.NS_ALIASES)
return bool(self._matches_regex(alias, ApplicationService.NS_ALIASES))

def is_interested_in_room(self, room_id):
return self._matches_regex(room_id, ApplicationService.NS_ROOMS)
return bool(self._matches_regex(room_id, ApplicationService.NS_ROOMS))

def is_exclusive_user(self, user_id):
return (
Expand Down
4 changes: 3 additions & 1 deletion tests/appservice/test_appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
from mock import Mock
from tests import unittest

import re


def _regex(regex, exclusive=True):
return {
"regex": regex,
"regex": re.compile(regex),
"exclusive": exclusive
}

Expand Down

0 comments on commit 350333a

Please sign in to comment.