2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class FloodControlFast {
|
|
|
|
public:
|
2022-11-22 15:25:53 +01:00
|
|
|
void add_event(double now) {
|
|
|
|
for (auto &bucket : buckets_) {
|
|
|
|
bucket.add_event(now);
|
|
|
|
wakeup_at_ = td::max(wakeup_at_, bucket.get_wakeup_at());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-16 21:36:59 +02:00
|
|
|
|
2022-11-22 15:25:53 +01:00
|
|
|
double get_wakeup_at() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return wakeup_at_;
|
|
|
|
}
|
|
|
|
|
2022-11-22 15:25:53 +01:00
|
|
|
void add_limit(double duration, double count) {
|
2022-11-23 14:32:09 +01:00
|
|
|
buckets_.emplace_back(duration, count);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear_events() {
|
2022-11-22 15:25:53 +01:00
|
|
|
for (auto &bucket : buckets_) {
|
|
|
|
bucket.clear_events();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
wakeup_at_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-11-22 22:45:17 +01:00
|
|
|
class FloodControlBucket {
|
|
|
|
public:
|
|
|
|
FloodControlBucket(double duration, double count)
|
|
|
|
: max_capacity_(count - 1), speed_(count / duration), volume_(max_capacity_) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_event(double now, double size = 1) {
|
|
|
|
CHECK(now >= wakeup_at_);
|
|
|
|
update_volume(now);
|
|
|
|
if (volume_ >= size) {
|
|
|
|
volume_ -= size;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size -= volume_;
|
|
|
|
volume_ = 0;
|
|
|
|
wakeup_at_ = volume_at_ + size / speed_;
|
|
|
|
volume_at_ = wakeup_at_;
|
|
|
|
}
|
|
|
|
|
|
|
|
double get_wakeup_at() const {
|
|
|
|
return wakeup_at_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_events() {
|
|
|
|
volume_ = max_capacity_;
|
|
|
|
volume_at_ = 0;
|
|
|
|
wakeup_at_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const double max_capacity_{1};
|
|
|
|
const double speed_{1};
|
|
|
|
double volume_{1};
|
|
|
|
|
|
|
|
double volume_at_{0};
|
|
|
|
double wakeup_at_{0};
|
|
|
|
|
|
|
|
void update_volume(double now) {
|
|
|
|
CHECK(now >= volume_at_);
|
|
|
|
auto passed = now - volume_at_;
|
|
|
|
volume_ = td::min(volume_ + passed * speed_, max_capacity_);
|
|
|
|
volume_at_ = now;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-22 15:25:53 +01:00
|
|
|
double wakeup_at_ = 0;
|
2022-11-22 22:45:17 +01:00
|
|
|
vector<FloodControlBucket> buckets_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|