mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
Ratelimit nochannel ADNL packets (#1147)
* Get ADNL stats in validator console * Add timestamp to stats * Limit nochannel adnl packets --------- Co-authored-by: SpyCheese <mikle98@yandex.ru>
This commit is contained in:
parent
e08111159f
commit
b2b79fead1
18 changed files with 838 additions and 175 deletions
|
@ -40,6 +40,40 @@ inline bool adnl_node_is_older(AdnlNode &a, AdnlNode &b) {
|
|||
return a.addr_list().version() < b.addr_list().version();
|
||||
}
|
||||
|
||||
class RateLimiter {
|
||||
public:
|
||||
explicit RateLimiter(td::uint32 capacity, double period) : capacity_(capacity), period_(period), remaining_(capacity) {
|
||||
}
|
||||
|
||||
bool take() {
|
||||
while (remaining_ < capacity_ && increment_at_.is_in_past()) {
|
||||
++remaining_;
|
||||
increment_at_ += period_;
|
||||
}
|
||||
if (remaining_) {
|
||||
--remaining_;
|
||||
if (increment_at_.is_in_past()) {
|
||||
increment_at_ = td::Timestamp::in(period_);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
td::Timestamp ready_at() const {
|
||||
if (remaining_) {
|
||||
return td::Timestamp::now();
|
||||
}
|
||||
return increment_at_;
|
||||
}
|
||||
|
||||
private:
|
||||
td::uint32 capacity_;
|
||||
double period_;
|
||||
td::uint32 remaining_;
|
||||
td::Timestamp increment_at_ = td::Timestamp::never();
|
||||
};
|
||||
|
||||
} // namespace adnl
|
||||
|
||||
} // namespace ton
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue