ratelimiting.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from ._base import Config
  15. class RatelimitConfig(Config):
  16. def read_config(self, config):
  17. self.rc_messages_per_second = config["rc_messages_per_second"]
  18. self.rc_message_burst_count = config["rc_message_burst_count"]
  19. self.federation_rc_window_size = config["federation_rc_window_size"]
  20. self.federation_rc_sleep_limit = config["federation_rc_sleep_limit"]
  21. self.federation_rc_sleep_delay = config["federation_rc_sleep_delay"]
  22. self.federation_rc_reject_limit = config["federation_rc_reject_limit"]
  23. self.federation_rc_concurrent = config["federation_rc_concurrent"]
  24. def default_config(self, **kwargs):
  25. return """\
  26. ## Ratelimiting ##
  27. # Number of messages a client can send per second
  28. rc_messages_per_second: 0.2
  29. # Number of message a client can send before being throttled
  30. rc_message_burst_count: 10.0
  31. # The federation window size in milliseconds
  32. federation_rc_window_size: 1000
  33. # The number of federation requests from a single server in a window
  34. # before the server will delay processing the request.
  35. federation_rc_sleep_limit: 10
  36. # The duration in milliseconds to delay processing events from
  37. # remote servers by if they go over the sleep limit.
  38. federation_rc_sleep_delay: 500
  39. # The maximum number of concurrent federation requests allowed
  40. # from a single server
  41. federation_rc_reject_limit: 50
  42. # The number of federation requests to concurrently process from a
  43. # single server
  44. federation_rc_concurrent: 3
  45. """