2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2023-10-27 23:06:56 +02:00
|
|
|
// More strict implementations of flood control than FloodControlFast.
|
2018-12-31 20:04:05 +01:00
|
|
|
// Should be just fine for small counters.
|
|
|
|
class FloodControlStrict {
|
|
|
|
public:
|
2020-07-16 21:36:59 +02:00
|
|
|
// there is no reason to return wakeup_at_, because it will be a time before the next allowed event, not current
|
2022-11-21 19:07:56 +01:00
|
|
|
void add_event(double now) {
|
2018-12-31 20:04:05 +01:00
|
|
|
events_.push_back(Event{now});
|
|
|
|
if (without_update_ > 0) {
|
|
|
|
without_update_--;
|
|
|
|
} else {
|
|
|
|
update(now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 21:36:59 +02:00
|
|
|
// no more than count in each duration
|
|
|
|
void add_limit(int32 duration, size_t count) {
|
2018-12-31 20:04:05 +01:00
|
|
|
limits_.push_back(Limit{duration, count, 0});
|
2020-07-16 21:36:59 +02:00
|
|
|
without_update_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 19:07:56 +01:00
|
|
|
double get_wakeup_at() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return wakeup_at_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_events() {
|
|
|
|
events_.clear();
|
|
|
|
for (auto &limit : limits_) {
|
|
|
|
limit.pos_ = 0;
|
|
|
|
}
|
|
|
|
without_update_ = 0;
|
2022-11-21 19:07:56 +01:00
|
|
|
wakeup_at_ = 0.0;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 18:52:12 +01:00
|
|
|
private:
|
2022-11-21 19:07:56 +01:00
|
|
|
void update(double now) {
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t min_pos = events_.size();
|
|
|
|
|
|
|
|
without_update_ = std::numeric_limits<size_t>::max();
|
|
|
|
for (auto &limit : limits_) {
|
2020-07-16 21:36:59 +02:00
|
|
|
if (limit.count_ < events_.size() - limit.pos_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
limit.pos_ = events_.size() - limit.count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// binary-search? :D
|
2022-11-21 19:07:56 +01:00
|
|
|
auto end_time = now - limit.duration_;
|
|
|
|
while (limit.pos_ < events_.size() && events_[limit.pos_].timestamp_ < end_time) {
|
2018-12-31 20:04:05 +01:00
|
|
|
limit.pos_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit.count_ + limit.pos_ <= events_.size()) {
|
|
|
|
CHECK(limit.count_ + limit.pos_ == events_.size());
|
2018-02-12 11:37:54 +01:00
|
|
|
wakeup_at_ = max(wakeup_at_, events_[limit.pos_].timestamp_ + limit.duration_);
|
2018-12-31 20:04:05 +01:00
|
|
|
without_update_ = 0;
|
|
|
|
} else {
|
2020-07-16 21:36:59 +02:00
|
|
|
without_update_ = min(without_update_, limit.count_ + limit.pos_ - events_.size() - 1);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:37:54 +01:00
|
|
|
min_pos = min(min_pos, limit.pos_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (min_pos * 2 > events_.size()) {
|
|
|
|
for (auto &limit : limits_) {
|
|
|
|
limit.pos_ -= min_pos;
|
|
|
|
}
|
|
|
|
events_.erase(events_.begin(), events_.begin() + min_pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 19:07:56 +01:00
|
|
|
double wakeup_at_ = 0.0;
|
2018-12-31 20:04:05 +01:00
|
|
|
struct Event {
|
2022-11-21 19:07:56 +01:00
|
|
|
double timestamp_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
struct Limit {
|
|
|
|
int32 duration_;
|
2020-07-16 21:36:59 +02:00
|
|
|
size_t count_;
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t pos_;
|
|
|
|
};
|
|
|
|
size_t without_update_ = 0;
|
2022-11-21 18:52:12 +01:00
|
|
|
vector<Event> events_;
|
|
|
|
vector<Limit> limits_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|