public class DeadlineTimerWheel extends Object
Based on netty's HashedTimerWheel, which is based on George Varghese and Tony Lauck's paper, 'Hashed and Hierarchical Timing Wheels: data structures to efficiently implement a timer facility'. More comprehensive slides are located here.
Wheel is backed by arrays. Timer cancellation is O(1). Timer scheduling might be slightly longer if a lot of timers are in the same tick. The underlying tick contains an array. That array grows when needed, but does not shrink.
Caveats
Timers that expire in the same tick will not be ordered with one another. As ticks are fairly coarse resolution normally, this means that some timers may expire out of order.
Note: Not threadsafe.
Modifier and Type | Class and Description |
---|---|
static interface |
DeadlineTimerWheel.TimerConsumer
Consumer of timer entries as deadline to timerId.
|
static interface |
DeadlineTimerWheel.TimerHandler
Handler for processing expired timers.
|
Modifier and Type | Field and Description |
---|---|
static long |
NULL_DEADLINE
Represents a deadline not set in the wheel.
|
Constructor and Description |
---|
DeadlineTimerWheel(TimeUnit timeUnit,
long startTime,
long tickResolution,
int ticksPerWheel)
Construct timer wheel and configure timing with default initial allocation.
|
DeadlineTimerWheel(TimeUnit timeUnit,
long startTime,
long tickResolution,
int ticksPerWheel,
int initialTickAllocation)
Construct timer wheel and configure timing with provided initial allocation.
|
Modifier and Type | Method and Description |
---|---|
boolean |
cancelTimer(long timerId)
Cancel a previously scheduled timer.
|
void |
clear()
Clear out all active timers in the wheel.
|
long |
currentTickTime()
Time of current tick of the wheel in
timeUnit() s. |
void |
currentTickTime(long now)
Set the current tick of the wheel to examine on the next
poll(long, org.agrona.DeadlineTimerWheel.TimerHandler, int) . |
long |
deadline(long timerId)
Get the deadline for the given timerId.
|
void |
forEach(DeadlineTimerWheel.TimerConsumer consumer)
Iterate over wheel so all active timers can be consumed without expiring them.
|
int |
poll(long now,
DeadlineTimerWheel.TimerHandler handler,
int expiryLimit)
Poll for timers expired by the deadline passing.
|
void |
resetStartTime(long startTime)
Reset the start time of the wheel.
|
long |
scheduleTimer(long deadline)
Schedule a timer for a given absolute time as a deadline in
timeUnit() s. |
long |
startTime()
The start time tick for the wheel from which it advances.
|
long |
tickResolution()
Resolution of a tick of the wheel in
timeUnit() s. |
int |
ticksPerWheel()
The number of ticks, or spokes, per wheel.
|
long |
timerCount()
Number of active timers.
|
TimeUnit |
timeUnit()
Time unit for the time values.
|
public static final long NULL_DEADLINE
public DeadlineTimerWheel(TimeUnit timeUnit, long startTime, long tickResolution, int ticksPerWheel)
public DeadlineTimerWheel(TimeUnit timeUnit, long startTime, long tickResolution, int ticksPerWheel, int initialTickAllocation)
timeUnit
- for the values used to express the time.startTime
- for the wheel (in given TimeUnit
).tickResolution
- for the wheel, i.e. how many TimeUnit
s per tick.ticksPerWheel
- or spokes, for the wheel (must be power of 2).initialTickAllocation
- space allocated per tick of the wheel (must be power of 2).public TimeUnit timeUnit()
public long tickResolution()
timeUnit()
s.timeUnit()
s.public int ticksPerWheel()
public long startTime()
public long timerCount()
public void resetStartTime(long startTime)
startTime
- to set the wheel to.IllegalStateException
- if wheel has any active timers.public long currentTickTime()
timeUnit()
s.timeUnit()
s.public void currentTickTime(long now)
poll(long, org.agrona.DeadlineTimerWheel.TimerHandler, int)
.
If the time passed in is less than the current time, nothing is changed.
No timers will be expired when winding forward and thus are still in the wheel and will be expired as
encountered in the wheel during poll(long, org.agrona.DeadlineTimerWheel.TimerHandler, int)
operations. No guarantee of order for expired timers is
assumed when later polled.now
- current time to advance to or stay at current time.public void clear()
public long scheduleTimer(long deadline)
timeUnit()
s. A timerId will be assigned
and returned for future reference.deadline
- after which the timer should expire.public boolean cancelTimer(long timerId)
timerId
- of the timer to cancel.public int poll(long now, DeadlineTimerWheel.TimerHandler handler, int expiryLimit)
now
- current time to compare deadlines against.handler
- to call for each expired timer.expiryLimit
- to process in one poll operation.public void forEach(DeadlineTimerWheel.TimerConsumer consumer)
consumer
- to call for each active timer.public long deadline(long timerId)
timerId
- of the timer to return the deadline of.NULL_DEADLINE
if timerId is not running.Copyright © 2014-2020 Real Logic Limited. All Rights Reserved.