Avoid needless wakeups for token bucket refills.
In 0.2.3.5-alpha, we increased our default token bucket refill interval from 1 time per second to 10 times per second. (See legacy/trac#3630 (moved) and proposal 183.)
AFAICT, this is now the most frequently running timer causing wakeups on an idle Tor instance. It even causes wakeups on Tor instances when DisableNetwork is set. We can do better!
Two key insights:
-
First, it is not necessary to actually refill these buckets periodically. Instead, we can store the last time at which we refilled each one, and calculate its current size at immediately before we read or write. The only thing we'll need to use a timer for is to wake up connections on which we've stopped reading or writing.
-
Second, we only need to have this timer active when at least one connection is blocked on bandwidth.