1 #ifndef LIBFILEZILLA_RATE_LIMITER_HEADER
2 #define LIBFILEZILLA_RATE_LIMITER_HEADER
20 using type = uint64_t;
22 unlimited =
static_cast<type>(-1)
60 void set_burst_tolerance(rate::type tolerance);
67 void record_activity();
70 void on_timer(timer_id
const&);
74 std::atomic<int> activity_{2};
76 std::vector<rate_limiter*> limiters_;
78 std::atomic<timer_id> timer_{};
80 std::atomic<rate::type> burst_tolerance_{1};
94 virtual void remove_bucket();
111 virtual void update_stats(
bool & active) = 0;
118 virtual size_t weight()
const {
return 1; }
125 virtual size_t unsaturated(direction::type
const )
const {
return 0; }
143 virtual rate::type add_tokens(direction::type
const , rate::type , rate::type ) = 0;
166 virtual std::array<rate::type, 2> gather_unspent_for_removal() = 0;
169 rate_limit_manager * mgr_{};
171 size_t idx_{
static_cast<size_t>(-1)};
206 void set_limits(rate::type download_limit, rate::type upload_limit);
209 rate::type limit(direction::type
const d);
215 virtual void lock_tree()
override;
217 bool do_set_limit(direction::type
const d, rate::type limit);
219 virtual void update_stats(
bool & active)
override;
220 virtual size_t weight()
const override {
return weight_; }
221 virtual size_t unsaturated(direction::type
const d)
const override {
return data_[d].unused_capacity_ ? data_[d].unsaturated_ : 0; }
224 virtual rate::type add_tokens(direction::type
const d, rate::type tokens, rate::type limit)
override;
225 virtual rate::type distribute_overflow(direction::type
const d, rate::type tokens)
override;
227 virtual void unlock_tree()
override;
229 void pay_debt(direction::type
const d);
231 virtual std::array<rate::type, 2> gather_unspent_for_removal()
override;
233 std::vector<bucket_base*> buckets_;
234 std::vector<size_t> scratch_buffer_;
238 rate::type limit_{rate::unlimited};
239 rate::type merged_tokens_;
240 rate::type overflow_{};
242 rate::type unused_capacity_{};
244 size_t unsaturated_{};
256 virtual void remove_bucket()
override;
263 rate::type available(direction::type
const d);
271 void consume(direction::type
const d, rate::type amount);
285 virtual void update_stats(
bool & active)
override;
286 virtual size_t unsaturated(direction::type
const d)
const override {
return data_[d].unsaturated_ ? 1 : 0; }
288 virtual rate::type add_tokens(direction::type
const d, rate::type tokens, rate::type limit)
override;
289 virtual rate::type distribute_overflow(direction::type
const d, rate::type tokens)
override;
291 virtual void unlock_tree()
override;
293 virtual std::array<rate::type, 2> gather_unspent_for_removal()
override;
296 rate::type available_{rate::unlimited};
297 rate::type overflow_multiplier_{1};
298 rate::type bucket_size_{rate::unlimited};