mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2024-09-09 13:42:02 +00:00
Allow Configurable Rate Limiting Per AS
This adds a flag loaded from the registration file of an AS that will determine whether or not its users are rate limited (by ratelimit in _base.py). Needed for IRC bridge reasons - see https://github.com/matrix-org/matrix-appservice-irc/issues/240.
This commit is contained in:
parent
50ac1d843d
commit
5b54d51d1e
@ -81,7 +81,7 @@ class ApplicationService(object):
|
|||||||
NS_LIST = [NS_USERS, NS_ALIASES, NS_ROOMS]
|
NS_LIST = [NS_USERS, NS_ALIASES, NS_ROOMS]
|
||||||
|
|
||||||
def __init__(self, token, url=None, namespaces=None, hs_token=None,
|
def __init__(self, token, url=None, namespaces=None, hs_token=None,
|
||||||
sender=None, id=None, protocols=None):
|
sender=None, id=None, protocols=None, rate_limited=True):
|
||||||
self.token = token
|
self.token = token
|
||||||
self.url = url
|
self.url = url
|
||||||
self.hs_token = hs_token
|
self.hs_token = hs_token
|
||||||
@ -95,6 +95,8 @@ class ApplicationService(object):
|
|||||||
else:
|
else:
|
||||||
self.protocols = set()
|
self.protocols = set()
|
||||||
|
|
||||||
|
self.rate_limited = rate_limited
|
||||||
|
|
||||||
def _check_namespaces(self, namespaces):
|
def _check_namespaces(self, namespaces):
|
||||||
# Sanity check that it is of the form:
|
# Sanity check that it is of the form:
|
||||||
# {
|
# {
|
||||||
@ -234,5 +236,8 @@ class ApplicationService(object):
|
|||||||
def is_exclusive_room(self, room_id):
|
def is_exclusive_room(self, room_id):
|
||||||
return self._is_exclusive(ApplicationService.NS_ROOMS, room_id)
|
return self._is_exclusive(ApplicationService.NS_ROOMS, room_id)
|
||||||
|
|
||||||
|
def is_rate_limited(self):
|
||||||
|
return self.rate_limited
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "ApplicationService: %s" % (self.__dict__,)
|
return "ApplicationService: %s" % (self.__dict__,)
|
||||||
|
@ -110,6 +110,11 @@ def _load_appservice(hostname, as_info, config_filename):
|
|||||||
user = UserID(localpart, hostname)
|
user = UserID(localpart, hostname)
|
||||||
user_id = user.to_string()
|
user_id = user.to_string()
|
||||||
|
|
||||||
|
# Rate limiting for users of this AS is on by default (excludes sender)
|
||||||
|
rate_limited = True
|
||||||
|
if isinstance(as_info.get("rate_limited"), bool):
|
||||||
|
rate_limited = as_info.get("rate_limited")
|
||||||
|
|
||||||
# namespace checks
|
# namespace checks
|
||||||
if not isinstance(as_info.get("namespaces"), dict):
|
if not isinstance(as_info.get("namespaces"), dict):
|
||||||
raise KeyError("Requires 'namespaces' object.")
|
raise KeyError("Requires 'namespaces' object.")
|
||||||
@ -155,4 +160,5 @@ def _load_appservice(hostname, as_info, config_filename):
|
|||||||
sender=user_id,
|
sender=user_id,
|
||||||
id=as_info["id"],
|
id=as_info["id"],
|
||||||
protocols=protocols,
|
protocols=protocols,
|
||||||
|
rate_limited=rate_limited
|
||||||
)
|
)
|
||||||
|
@ -57,10 +57,24 @@ class BaseHandler(object):
|
|||||||
time_now = self.clock.time()
|
time_now = self.clock.time()
|
||||||
user_id = requester.user.to_string()
|
user_id = requester.user.to_string()
|
||||||
|
|
||||||
|
# Disable rate limiting of users belonging to any AS that is configured
|
||||||
|
# not to be rate limited in its registration file (rate_limited: true|false).
|
||||||
|
# The AS user itself is never rate limited.
|
||||||
|
|
||||||
app_service = self.store.get_app_service_by_user_id(user_id)
|
app_service = self.store.get_app_service_by_user_id(user_id)
|
||||||
if app_service is not None:
|
if app_service is not None:
|
||||||
return # do not ratelimit app service senders
|
return # do not ratelimit app service senders
|
||||||
|
|
||||||
|
should_rate_limit = True
|
||||||
|
|
||||||
|
for service in self.store.get_app_services():
|
||||||
|
if service.is_interested_in_user(user_id):
|
||||||
|
should_rate_limit = service.is_rate_limited()
|
||||||
|
break
|
||||||
|
|
||||||
|
if not should_rate_limit:
|
||||||
|
return
|
||||||
|
|
||||||
allowed, time_allowed = self.ratelimiter.send_message(
|
allowed, time_allowed = self.ratelimiter.send_message(
|
||||||
user_id, time_now,
|
user_id, time_now,
|
||||||
msg_rate_hz=self.hs.config.rc_messages_per_second,
|
msg_rate_hz=self.hs.config.rc_messages_per_second,
|
||||||
|
Loading…
Reference in New Issue
Block a user