19 #define TOKEN_BUCKET_PRIVATE
40 tor_assert_nonfatal(rate > 0);
41 tor_assert_nonfatal(burst > 0);
57 bucket->bucket = cfg->burst;
67 bucket->bucket = MIN(bucket->bucket, cfg->burst);
79 const uint32_t elapsed)
81 const int was_empty = (bucket->bucket <= 0);
88 const size_t gap = ((size_t)cfg->burst) - ((size_t)bucket->bucket);
90 if (elapsed > gap / cfg->rate) {
91 bucket->bucket = cfg->burst;
93 bucket->bucket += cfg->rate * elapsed;
96 return was_empty && bucket->bucket > 0;
109 const int becomes_empty = bucket->bucket > 0 && n >= bucket->bucket;
111 return becomes_empty;
124 uint64_t units = (uint64_t) rate * TICKS_PER_STEP;
125 uint32_t val = (uint32_t)
127 return val ? val : 1;
172 bucket->last_refilled_at_timestamp = now_ts;
186 const uint32_t elapsed_ticks =
187 (now_ts - bucket->last_refilled_at_timestamp);
188 if (elapsed_ticks > UINT32_MAX-(300*1000)) {
196 const uint32_t elapsed_steps = elapsed_ticks / TICKS_PER_STEP;
198 if (!elapsed_steps) {
207 &bucket->cfg, elapsed_steps))
210 &bucket->cfg, elapsed_steps))
213 bucket->last_refilled_at_timestamp = now_ts;
250 ssize_t n_read, ssize_t n_written)
266 uint32_t burst, uint32_t now_ts)
288 bucket->last_refilled_at_timestamp = now_ts;
296 const uint32_t elapsed_ticks =
297 (now_ts - bucket->last_refilled_at_timestamp);
298 if (elapsed_ticks > UINT32_MAX-(300*1000)) {
309 bucket->last_refilled_at_timestamp = now_ts;
Macro definitions for MIN, MAX, and CLAMP.
uint64_t monotime_coarse_stamp_units_to_approx_msec(uint64_t units)
Functions and types for monotonic times.
int token_bucket_rw_dec(token_bucket_rw_t *bucket, ssize_t n_read, ssize_t n_written)
STATIC uint32_t rate_per_sec_to_rate_per_step(uint32_t rate)
int token_bucket_rw_dec_read(token_bucket_rw_t *bucket, ssize_t n)
void token_bucket_ctr_init(token_bucket_ctr_t *bucket, uint32_t rate, uint32_t burst, uint32_t now_ts)
int token_bucket_raw_dec(token_bucket_raw_t *bucket, ssize_t n)
void token_bucket_rw_init(token_bucket_rw_t *bucket, uint32_t rate, uint32_t burst, uint32_t now_ts)
void token_bucket_raw_adjust(token_bucket_raw_t *bucket, const token_bucket_cfg_t *cfg)
void token_bucket_rw_adjust(token_bucket_rw_t *bucket, uint32_t rate, uint32_t burst)
void token_bucket_cfg_init(token_bucket_cfg_t *cfg, uint32_t rate, uint32_t burst)
void token_bucket_ctr_reset(token_bucket_ctr_t *bucket, uint32_t now_ts)
void token_bucket_ctr_refill(token_bucket_ctr_t *bucket, uint32_t now_ts)
void token_bucket_ctr_adjust(token_bucket_ctr_t *bucket, uint32_t rate, uint32_t burst)
int token_bucket_rw_dec_write(token_bucket_rw_t *bucket, ssize_t n)
int token_bucket_raw_refill_steps(token_bucket_raw_t *bucket, const token_bucket_cfg_t *cfg, const uint32_t elapsed)
int token_bucket_rw_refill(token_bucket_rw_t *bucket, uint32_t now_ts)
void token_bucket_raw_reset(token_bucket_raw_t *bucket, const token_bucket_cfg_t *cfg)
void token_bucket_rw_reset(token_bucket_rw_t *bucket, uint32_t now_ts)
Headers for token_bucket.c.
#define TOKEN_BUCKET_MAX_BURST
Macros to manage assertions, fatal and non-fatal.