2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-09-03 10:43:11 +02:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2014-09-01 18:54:54 +02:00
|
|
|
import collections
|
|
|
|
|
2019-03-15 18:46:16 +01:00
|
|
|
from synapse.api.errors import LimitExceededError
|
|
|
|
|
2014-09-01 18:54:54 +02:00
|
|
|
|
|
|
|
class Ratelimiter(object):
|
2014-09-02 16:06:20 +02:00
|
|
|
"""
|
|
|
|
Ratelimit message sending by user.
|
|
|
|
"""
|
2014-09-01 18:54:54 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.message_counts = collections.OrderedDict()
|
|
|
|
|
2019-03-05 15:25:33 +01:00
|
|
|
def can_do_action(self, key, time_now_s, rate_hz, burst_count, update=True):
|
|
|
|
"""Can the entity (e.g. user or IP address) perform the action?
|
2014-09-02 16:06:20 +02:00
|
|
|
Args:
|
2019-03-05 15:25:33 +01:00
|
|
|
key: The key we should use when rate limiting. Can be a user ID
|
|
|
|
(when sending events), an IP address, etc.
|
2014-09-02 16:06:20 +02:00
|
|
|
time_now_s: The time now.
|
2019-03-05 15:25:33 +01:00
|
|
|
rate_hz: The long term number of messages a user can send in a
|
2014-09-02 16:06:20 +02:00
|
|
|
second.
|
|
|
|
burst_count: How many messages the user can send before being
|
|
|
|
limited.
|
2016-10-19 11:45:24 +02:00
|
|
|
update (bool): Whether to update the message rates or not. This is
|
|
|
|
useful to check if a message would be allowed to be sent before
|
|
|
|
its ready to be actually sent.
|
2014-09-02 16:06:20 +02:00
|
|
|
Returns:
|
|
|
|
A pair of a bool indicating if they can send a message now and a
|
|
|
|
time in seconds of when they can next send a message.
|
|
|
|
"""
|
|
|
|
self.prune_message_counts(time_now_s)
|
2016-10-19 11:45:24 +02:00
|
|
|
message_count, time_start, _ignored = self.message_counts.get(
|
2019-03-05 15:25:33 +01:00
|
|
|
key, (0., time_now_s, None),
|
2014-09-01 18:54:54 +02:00
|
|
|
)
|
2014-09-02 16:06:20 +02:00
|
|
|
time_delta = time_now_s - time_start
|
2019-03-05 15:25:33 +01:00
|
|
|
sent_count = message_count - time_delta * rate_hz
|
2014-09-02 16:06:20 +02:00
|
|
|
if sent_count < 0:
|
|
|
|
allowed = True
|
|
|
|
time_start = time_now_s
|
2014-09-03 10:43:11 +02:00
|
|
|
message_count = 1.
|
2014-09-02 16:06:20 +02:00
|
|
|
elif sent_count > burst_count - 1.:
|
2014-09-01 18:54:54 +02:00
|
|
|
allowed = False
|
|
|
|
else:
|
|
|
|
allowed = True
|
|
|
|
message_count += 1
|
2014-09-02 16:06:20 +02:00
|
|
|
|
2016-10-19 11:45:24 +02:00
|
|
|
if update:
|
2019-03-05 15:25:33 +01:00
|
|
|
self.message_counts[key] = (
|
|
|
|
message_count, time_start, rate_hz
|
2016-10-19 11:45:24 +02:00
|
|
|
)
|
2014-09-02 16:06:20 +02:00
|
|
|
|
2019-03-05 15:25:33 +01:00
|
|
|
if rate_hz > 0:
|
2014-09-02 16:06:20 +02:00
|
|
|
time_allowed = (
|
2019-03-05 15:25:33 +01:00
|
|
|
time_start + (message_count - burst_count + 1) / rate_hz
|
2014-09-02 16:06:20 +02:00
|
|
|
)
|
|
|
|
if time_allowed < time_now_s:
|
|
|
|
time_allowed = time_now_s
|
|
|
|
else:
|
|
|
|
time_allowed = -1
|
|
|
|
|
|
|
|
return allowed, time_allowed
|
|
|
|
|
|
|
|
def prune_message_counts(self, time_now_s):
|
2019-03-05 15:25:33 +01:00
|
|
|
for key in list(self.message_counts.keys()):
|
|
|
|
message_count, time_start, rate_hz = (
|
|
|
|
self.message_counts[key]
|
2014-09-02 16:06:20 +02:00
|
|
|
)
|
|
|
|
time_delta = time_now_s - time_start
|
2019-03-05 15:25:33 +01:00
|
|
|
if message_count - time_delta * rate_hz > 0:
|
2014-09-02 16:06:20 +02:00
|
|
|
break
|
|
|
|
else:
|
2019-03-05 15:25:33 +01:00
|
|
|
del self.message_counts[key]
|
2019-03-15 18:46:16 +01:00
|
|
|
|
|
|
|
def ratelimit(self, key, time_now_s, rate_hz, burst_count, update=True):
|
|
|
|
allowed, time_allowed = self.can_do_action(
|
|
|
|
key, time_now_s, rate_hz, burst_count, update
|
|
|
|
)
|
|
|
|
|
|
|
|
if not allowed:
|
|
|
|
raise LimitExceededError(
|
|
|
|
retry_after_ms=int(1000 * (time_allowed - time_now_s)),
|
|
|
|
)
|