mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-07-31 22:48:33 -04:00
Add ratelimiting function to basehandler
This commit is contained in:
parent
dd2cd9312a
commit
c7a7cdf734
5 changed files with 27 additions and 1 deletions
|
@ -14,6 +14,7 @@
|
|||
# limitations under the License.
|
||||
|
||||
from twisted.internet import defer
|
||||
from synapse.api.errors import cs_error, Codes
|
||||
|
||||
class BaseHandler(object):
|
||||
|
||||
|
@ -25,8 +26,24 @@ class BaseHandler(object):
|
|||
self.room_lock = hs.get_room_lock_manager()
|
||||
self.state_handler = hs.get_state_handler()
|
||||
self.distributor = hs.get_distributor()
|
||||
self.ratelimiter = hs.get_ratelimiter()
|
||||
self.clock = hs.get_clock()
|
||||
self.hs = hs
|
||||
|
||||
def ratelimit(self, user_id):
|
||||
time_now = self.clock.time()
|
||||
allowed, time_allowed = self.ratelimiter.send_message(
|
||||
user_id, time_now,
|
||||
msg_rate_hz=self.hs.config.rc_messages_per_second,
|
||||
burst_count=self.hs.config.rc_messsage_burst_count,
|
||||
)
|
||||
if not allowed:
|
||||
raise cs_error(
|
||||
"Limit exceeded",
|
||||
Codes.M_LIMIT_EXCEEDED,
|
||||
retry_after_ms=1000*(time_allowed - time_now),
|
||||
)
|
||||
|
||||
|
||||
class BaseRoomHandler(BaseHandler):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue