mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-20 02:09:24 +01:00
Refactor "timestamp" not to be its own type coupled to token buffers
Really, the uint32_t is only an optimization; any kind of unit should work fine. Some users might want to use time_t or monotime_coarse_t or something like that.
This commit is contained in:
parent
2307bef7a2
commit
003e6595bf
@ -48,12 +48,9 @@ token_bucket_cfg_init(token_bucket_cfg_t *cfg,
|
||||
*/
|
||||
void
|
||||
token_bucket_raw_reset(token_bucket_raw_t *bucket,
|
||||
token_bucket_timestamp_t *stamp,
|
||||
const token_bucket_cfg_t *cfg,
|
||||
uint32_t now)
|
||||
const token_bucket_cfg_t *cfg)
|
||||
{
|
||||
bucket->bucket = cfg->burst;
|
||||
stamp->last_refilled_at = now;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -165,10 +162,9 @@ void
|
||||
token_bucket_rw_reset(token_bucket_rw_t *bucket,
|
||||
uint32_t now_ts)
|
||||
{
|
||||
token_bucket_raw_reset(&bucket->read_bucket, &bucket->stamp,
|
||||
&bucket->cfg, now_ts);
|
||||
token_bucket_raw_reset(&bucket->write_bucket, &bucket->stamp,
|
||||
&bucket->cfg, now_ts);
|
||||
token_bucket_raw_reset(&bucket->read_bucket, &bucket->cfg);
|
||||
token_bucket_raw_reset(&bucket->write_bucket, &bucket->cfg);
|
||||
bucket->last_refilled_at_timestamp = now_ts;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -180,9 +176,10 @@ token_bucket_rw_reset(token_bucket_rw_t *bucket,
|
||||
*/
|
||||
int
|
||||
token_bucket_rw_refill(token_bucket_rw_t *bucket,
|
||||
uint32_t now_ts)
|
||||
uint32_t now_ts)
|
||||
{
|
||||
const uint32_t elapsed_ticks = (now_ts - bucket->stamp.last_refilled_at);
|
||||
const uint32_t elapsed_ticks =
|
||||
(now_ts - bucket->last_refilled_at_timestamp);
|
||||
if (elapsed_ticks > UINT32_MAX-(300*1000)) {
|
||||
/* Either about 48 days have passed since the last refill, or the
|
||||
* monotonic clock has somehow moved backwards. (We're looking at you,
|
||||
@ -208,7 +205,7 @@ token_bucket_rw_refill(token_bucket_rw_t *bucket,
|
||||
&bucket->cfg, elapsed_steps))
|
||||
flags |= TB_WRITE;
|
||||
|
||||
bucket->stamp.last_refilled_at = now_ts;
|
||||
bucket->last_refilled_at_timestamp = now_ts;
|
||||
return flags;
|
||||
}
|
||||
|
||||
|
@ -27,12 +27,6 @@ typedef struct token_bucket_raw_t {
|
||||
int32_t bucket;
|
||||
} token_bucket_raw_t;
|
||||
|
||||
/** A timestamp for a token bucket. The units of this timestamp are
|
||||
* unspecified, but must match with the rate set in the token_bucket_cfg_t. */
|
||||
typedef struct token_bucket_timestamp_t {
|
||||
uint32_t last_refilled_at;
|
||||
} token_bucket_timestamp_t;
|
||||
|
||||
void token_bucket_cfg_init(token_bucket_cfg_t *cfg,
|
||||
uint32_t rate,
|
||||
uint32_t burst);
|
||||
@ -41,9 +35,7 @@ void token_bucket_raw_adjust(token_bucket_raw_t *bucket,
|
||||
const token_bucket_cfg_t *cfg);
|
||||
|
||||
void token_bucket_raw_reset(token_bucket_raw_t *bucket,
|
||||
token_bucket_timestamp_t *stamp,
|
||||
const token_bucket_cfg_t *cfg,
|
||||
uint32_t now);
|
||||
const token_bucket_cfg_t *cfg);
|
||||
|
||||
int token_bucket_raw_dec(token_bucket_raw_t *bucket,
|
||||
ssize_t n);
|
||||
@ -67,7 +59,7 @@ typedef struct token_bucket_rw_t {
|
||||
token_bucket_cfg_t cfg;
|
||||
token_bucket_raw_t read_bucket;
|
||||
token_bucket_raw_t write_bucket;
|
||||
token_bucket_timestamp_t stamp;
|
||||
uint32_t last_refilled_at_timestamp;
|
||||
} token_bucket_rw_t;
|
||||
|
||||
void token_bucket_rw_init(token_bucket_rw_t *bucket,
|
||||
|
@ -36,7 +36,7 @@ test_bwmgt_token_buf_init(void *arg)
|
||||
tt_uint_op(rate_per_sec, OP_LT, 16*KB+160);
|
||||
}
|
||||
// Bucket starts out full:
|
||||
tt_uint_op(b.stamp.last_refilled_at, OP_EQ, START_TS);
|
||||
tt_uint_op(b.last_refilled_at_timestamp, OP_EQ, START_TS);
|
||||
tt_int_op(b.read_bucket.bucket, OP_EQ, 64*KB);
|
||||
|
||||
done:
|
||||
@ -138,7 +138,7 @@ test_bwmgt_token_buf_refill(void *arg)
|
||||
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, START_TS + SEC*3/2));
|
||||
tt_int_op(b.read_bucket.bucket, OP_GT, 40*KB - 400);
|
||||
tt_int_op(b.read_bucket.bucket, OP_LT, 40*KB + 400);
|
||||
tt_uint_op(b.stamp.last_refilled_at, OP_EQ, START_TS + SEC*3/2);
|
||||
tt_uint_op(b.last_refilled_at_timestamp, OP_EQ, START_TS + SEC*3/2);
|
||||
|
||||
/* No time: nothing happens. */
|
||||
{
|
||||
@ -150,12 +150,12 @@ test_bwmgt_token_buf_refill(void *arg)
|
||||
/* Another 30 seconds: fill the bucket. */
|
||||
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, START_TS + SEC*3/2 + SEC*30));
|
||||
tt_int_op(b.read_bucket.bucket, OP_EQ, b.cfg.burst);
|
||||
tt_uint_op(b.stamp.last_refilled_at, OP_EQ, START_TS + SEC*3/2 + SEC*30);
|
||||
tt_uint_op(b.last_refilled_at_timestamp, OP_EQ, START_TS + SEC*3/2 + SEC*30);
|
||||
|
||||
/* Another 30 seconds: nothing happens. */
|
||||
tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, START_TS + SEC*3/2 + SEC*60));
|
||||
tt_int_op(b.read_bucket.bucket, OP_EQ, b.cfg.burst);
|
||||
tt_uint_op(b.stamp.last_refilled_at, OP_EQ, START_TS + SEC*3/2 + SEC*60);
|
||||
tt_uint_op(b.last_refilled_at_timestamp, OP_EQ, START_TS + SEC*3/2 + SEC*60);
|
||||
|
||||
/* Empty the bucket, let two seconds pass, and make sure that a refill is
|
||||
* noticed. */
|
||||
|
Loading…
Reference in New Issue
Block a user