Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
LimitTimer Class Reference

This timer takes care of limiting the number of speed updates we send out in a second. More...

#include <LimitTimer.hxx>

Inheritance diagram for LimitTimer:
Timer Executable Notifiable QMember Destructable

Public Member Functions

 LimitTimer (ExecutorBase *ex, uint16_t update_delay_msec, uint8_t max_tokens, std::function< void()> callback)
 Constructor.
 
 ~LimitTimer ()
 Destructor.
 
bool try_take ()
 Attempts to take a token out of the bucket.
 
void take_no_callback ()
 Takes one entry from the bucket, and does not give a callback if there is no entry left.
 
- Public Member Functions inherited from Timer
 Timer (ActiveTimers *timers)
 Constructor.
 
 ~Timer ()
 Destructor.
 
void run () override
 Callback from the executor when this timer is scheduled.
 
long long schedule_time ()
 
void start (long long period=-1)
 Starts a timer.
 
void start_absolute (long long expiry_time_nsec)
 Starts the timer with an absolute deadline.
 
void restart ()
 Restart a timer with the existing period but from the current time.
 
void trigger ()
 This will wakeup the timer prematurely, immediately.
 
void ensure_triggered ()
 Triggers the timer if it is not expired yet.
 
void cancel ()
 Dangerous, do not call.
 
bool is_triggered ()
 
void set_triggered ()
 Sets the timer as if it was woken up by a trigger(), even if it was never started.
 
- Public Member Functions inherited from Executable
void test_deletion ()
 
void notify () override
 Crashes the program – everyone who is expecting notify calls must override this function.
 
virtual void alloc_result (QMember *item)
 Return the result of an alloc_async() from a memory Pool.
 
- Public Member Functions inherited from QMember
void init ()
 Initiailize a QMember, in place of a public placement construction.
 

Private Member Functions

long long timeout () override
 Callback from the timer infrastructure. Called periodically.
 

Private Attributes

uint16_t updateDelayMsec_
 cooldown delay in msec
 
uint8_t bucket_
 number of available tokens
 
uint8_t bucketMax_: 7
 maximum number of tokens in the bucket
 
uint8_t needUpdate_: 1
 if non-zero, wake up parent when token is available.
 
std::function< void()> callback_
 callback after cooldown period.
 

Additional Inherited Members

- Public Types inherited from Timer
enum  { NONE = 0 , RESTART = 1 , DELETE = -1 }
 Special return values from the timeout function. More...
 
- Protected Member Functions inherited from Timer
void update_period (long long period)
 Updates the period, to be used after the next expiration of the timer in order to restart it.
 
- Protected Member Functions inherited from QMember
 QMember ()
 Constructor.
 
 ~QMember ()
 Destructor.
 
- Protected Attributes inherited from QMember
QMembernext
 pointer to the next member in the queue
 

Detailed Description

This timer takes care of limiting the number of speed updates we send out in a second.

It is a token bucket filter.

Definition at line 45 of file LimitTimer.hxx.

Constructor & Destructor Documentation

◆ LimitTimer()

LimitTimer::LimitTimer ( ExecutorBase ex,
uint16_t  update_delay_msec,
uint8_t  max_tokens,
std::function< void()>  callback 
)
inline

Constructor.

Parameters
exexecutor to run on
update_delay_mseccooldown time delay in milliseconds
max_tokensnumber of available tokens, <= 127 max
callbackcallback called once after cooldown time delay

Definition at line 53 of file LimitTimer.hxx.

◆ ~LimitTimer()

LimitTimer::~LimitTimer ( )
inline

Destructor.

Definition at line 66 of file LimitTimer.hxx.

Member Function Documentation

◆ take_no_callback()

void LimitTimer::take_no_callback ( )
inline

Takes one entry from the bucket, and does not give a callback if there is no entry left.

Must be called from the same executor that was passed in the object construction.

Definition at line 97 of file LimitTimer.hxx.

◆ timeout()

long long LimitTimer::timeout ( )
inlineoverrideprivatevirtual

Callback from the timer infrastructure. Called periodically.

Implements Timer.

Definition at line 107 of file LimitTimer.hxx.

◆ try_take()

bool LimitTimer::try_take ( )
inline

Attempts to take a token out of the bucket.

Must be called from the same executor that was passed in the object construction.

Returns
true if the take is successful, false if there are no available tokens, in which case there will be a callback generated when tokens become available.

Definition at line 76 of file LimitTimer.hxx.

Member Data Documentation

◆ bucket_

uint8_t LimitTimer::bucket_
private

number of available tokens

Definition at line 129 of file LimitTimer.hxx.

◆ bucketMax_

uint8_t LimitTimer::bucketMax_
private

maximum number of tokens in the bucket

Definition at line 132 of file LimitTimer.hxx.

◆ callback_

std::function<void()> LimitTimer::callback_
private

callback after cooldown period.

Definition at line 138 of file LimitTimer.hxx.

◆ needUpdate_

uint8_t LimitTimer::needUpdate_
private

if non-zero, wake up parent when token is available.

Definition at line 135 of file LimitTimer.hxx.

◆ updateDelayMsec_

uint16_t LimitTimer::updateDelayMsec_
private

cooldown delay in msec

Definition at line 126 of file LimitTimer.hxx.


The documentation for this class was generated from the following file: