summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndrew Morgan <andrew@amorgan.xyz>2020-05-28 22:37:28 +0100
committerAndrew Morgan <andrew@amorgan.xyz>2020-05-28 22:53:23 +0100
commit0e6ee7ca1728e6912f9bc219849bcfc1188b5cff (patch)
treefdab4b015ff1a468d1c06102997cc52d2a4035e6
parentRefactor and comment ratelimiting. Set limits in constructor (diff)
downloadsynapse-0e6ee7ca1728e6912f9bc219849bcfc1188b5cff.tar.xz
Ratelimiters are instantiated by the HomeServer class
This makes it simple for tests to modify/nullify them.
-rw-r--r--synapse/server.py43
1 files changed, 36 insertions, 7 deletions
diff --git a/synapse/server.py b/synapse/server.py
index ca2deb49bb..440c6807d0 100644
--- a/synapse/server.py
+++ b/synapse/server.py
@@ -24,6 +24,7 @@
 import abc
 import logging
 import os
+from typing import Optional
 
 from twisted.mail.smtp import sendmail
 
@@ -242,9 +243,31 @@ class HomeServer(object):
 
         self.clock = Clock(reactor)
         self.distributor = Distributor()
-        self.ratelimiter = Ratelimiter()
-        self.admin_redaction_ratelimiter = Ratelimiter()
-        self.registration_ratelimiter = Ratelimiter()
+        # The rate_hz and burst_count is overridden on a per-user basis
+        self.request_ratelimiter = Ratelimiter(
+            rate_hz=0,
+            burst_count=0,
+        )
+        if config.rc_admin_redaction:
+            self.admin_redaction_ratelimiter = Ratelimiter(
+                rate_hz=config.rc_admin_redaction.per_second,
+                burst_count=config.rc_admin_redaction.burst_count,
+            )
+        else:
+            self.admin_redaction_ratelimiter = None
+
+        self.registration_ratelimiter = Ratelimiter(
+            rate_hz=config.rc_registration.per_second,
+            burst_count=config.rc_registration.burst_count,
+        )
+        self.login_ratelimiter = Ratelimiter(
+            rate_hz=config.rc_login_account.per_second,
+            burst_count=config.rc_login_account.burst_count,
+        )
+        self.login_failed_attempts_ratelimiter = Ratelimiter(
+            rate_hz=config.rc_login_failed_attempts.per_second,
+            burst_count=config.rc_login_failed_attempts.burst_count,
+        )
 
         self.datastores = None
 
@@ -314,15 +337,21 @@ class HomeServer(object):
     def get_distributor(self):
         return self.distributor
 
-    def get_ratelimiter(self):
-        return self.ratelimiter
+    def get_request_ratelimiter(self) -> Ratelimiter:
+        return self.request_ratelimiter
 
-    def get_registration_ratelimiter(self):
+    def get_registration_ratelimiter(self) -> Ratelimiter:
         return self.registration_ratelimiter
 
-    def get_admin_redaction_ratelimiter(self):
+    def get_admin_redaction_ratelimiter(self) -> Optional[Ratelimiter]:
         return self.admin_redaction_ratelimiter
 
+    def get_login_ratelimiter(self) -> Ratelimiter:
+        return self.login_ratelimiter
+
+    def get_login_failed_attempts_ratelimiter(self) -> Ratelimiter:
+        return self.login_failed_attempts_ratelimiter
+
     def build_federation_client(self):
         return FederationClient(self)