Crate ferris [] [src]

A hierarchical timer wheel

There are 3 wheels in the hierarchy, of resolution 10ms, 1s, and 1m. The max timeout length is 1 hour. Any timer scheduled over 1 hour will expire in 1 hour.

There is no migration between wheels. A timer is assigned to a single wheel and is scheduled at the max resolution of the wheel. E.g. If a timer is scheduled for 1.3s it will be scheduled to fire 2 second ticks later. This is most useful for coarse grain timers and is more efficient computationally and uses less memory than being more precise. The wheels don't have to keep track of offsets for the next inner wheel so the timer can be rescheduled when the outer wheel slot expires. And it doesn't have to actually do the reschedule, saving cpu, and potentially extra allocations.

Structs

AllocWheel

This wheel requires an allocation for each timer as it creates an Rc for its key. This allows the key to be stored in a global hashset that can be used for O(1) cancel. A Weak<T> is stored in the wheel slot, so that if the timer is cancelled, the memory is de-allocatd. When the expiry for that slot comes around, an attempt to promote the Weak reference will return None and so it will be ignored when draining the wheel slot. If the timer expires before it is cancelled, the weak reference can be used to remove the Rc from the HashMap, as well as trigger the user timeout behavior.

CopyWheel

This wheel maintains a copy of the timer key in both the appropriate inner timer wheel slot and the global hashset. This does not require an allocation for each timer but may use more memory than an CopyWheel depending upon the size of the keys. When the expiry for a slot occurs, the global hashmap is checked for the expiring keys. If they are still there it means they are valid to expire, otherwise they have already been cancelled.

Enums

Resolution

A resolution for a wheel in the hierarchy

Traits

Wheel

Functions

wheel_sizes

Determine the wheel size for each resolution.