rippled
Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
ripple::TimeoutCounter Class Referenceabstract

This class is an "active" object. More...

Inheritance diagram for ripple::TimeoutCounter:
Inheritance graph
[legend]
Collaboration diagram for ripple::TimeoutCounter:
Collaboration graph
[legend]

Classes

struct  QueueJobParameter
 

Public Member Functions

virtual void cancel ()
 Cancel the task by marking it as failed if the task is not done. More...
 

Protected Types

using ScopedLockType = std::unique_lock< std::recursive_mutex >
 

Protected Member Functions

 TimeoutCounter (Application &app, uint256 const &targetHash, std::chrono::milliseconds timeoutInterval, QueueJobParameter &&jobParameter, beast::Journal journal)
 
virtual ~TimeoutCounter ()=default
 
void setTimer (ScopedLockType &)
 Schedule a call to queueJob() after mTimerInterval. More...
 
void queueJob (ScopedLockType &)
 Queue a job to call invokeOnTimer(). More...
 
virtual void onTimer (bool progress, ScopedLockType &)=0
 Hook called from invokeOnTimer(). More...
 
virtual std::weak_ptr< TimeoutCounterpmDowncast ()=0
 Return a weak pointer to this. More...
 
bool isDone () const
 

Protected Attributes

Applicationapp_
 
beast::Journal journal_
 
std::recursive_mutex mtx_
 
const uint256 hash_
 The hash of the object (in practice, always a ledger) we are trying to fetch. More...
 
int timeouts_
 
bool complete_
 
bool failed_
 
bool progress_
 Whether forward progress has been made. More...
 
std::chrono::milliseconds timerInterval_
 The minimum time to wait between calls to execute(). More...
 
QueueJobParameter queueJobParameter_
 

Private Member Functions

void invokeOnTimer ()
 Calls onTimer() if in the right state. More...
 

Private Attributes

boost::asio::basic_waitable_timer< std::chrono::steady_clocktimer_
 

Detailed Description

This class is an "active" object.

It maintains its own timer and dispatches work to a job queue. Implementations derive from this class and override the abstract hook functions in the base.

This class implements an asynchronous loop:

  1. The entry point is setTimer.
  2. After mTimerInterval, queueJob is called, which schedules a job to call invokeOnTimer (or loops back to setTimer if there are too many concurrent jobs).
  3. The job queue calls invokeOnTimer which either breaks the loop if isDone or calls onTimer.
  4. onTimer is the only "real" virtual method in this class. It is the callback for when the timeout expires. Generally, its only responsibility is to set mFailed = true. However, if it wants to implement a policy of retries, then it has a chance to just increment a count of expired timeouts.
  5. Once onTimer returns, if the object is still not isDone, then invokeOnTimer sets another timeout by looping back to setTimer.

This loop executes concurrently with another asynchronous sequence, implemented by the subtype, that is trying to make progress and eventually set mComplete = true. While it is making progress but not complete, it should set mProgress = true, which is passed to onTimer so it can decide whether to postpone failure and reset the timeout. However, if it can complete all its work in one synchronous step (while it holds the lock), then it can ignore mProgress.

Definition at line 66 of file TimeoutCounter.h.

Member Typedef Documentation

◆ ScopedLockType

Definition at line 81 of file TimeoutCounter.h.

Constructor & Destructor Documentation

◆ TimeoutCounter()

ripple::TimeoutCounter::TimeoutCounter ( Application app,
uint256 const &  targetHash,
std::chrono::milliseconds  timeoutInterval,
QueueJobParameter &&  jobParameter,
beast::Journal  journal 
)
protected

Definition at line 29 of file TimeoutCounter.cpp.

◆ ~TimeoutCounter()

virtual ripple::TimeoutCounter::~TimeoutCounter ( )
protectedvirtualdefault

Member Function Documentation

◆ cancel()

void ripple::TimeoutCounter::cancel ( )
virtual

Cancel the task by marking it as failed if the task is not done.

Note
this function does not attempt to cancel the scheduled timer or to remove the queued job if any. When the timer expires or the queued job starts, however, the code will see that the task is done and returns immediately, if it can lock the weak pointer of the task.

Definition at line 118 of file TimeoutCounter.cpp.

◆ setTimer()

void ripple::TimeoutCounter::setTimer ( ScopedLockType sl)
protected

Schedule a call to queueJob() after mTimerInterval.

Definition at line 50 of file TimeoutCounter.cpp.

◆ queueJob()

void ripple::TimeoutCounter::queueJob ( ScopedLockType sl)
protected

Queue a job to call invokeOnTimer().

Definition at line 69 of file TimeoutCounter.cpp.

◆ onTimer()

virtual void ripple::TimeoutCounter::onTimer ( bool  progress,
ScopedLockType  
)
protectedpure virtual

◆ pmDowncast()

virtual std::weak_ptr<TimeoutCounter> ripple::TimeoutCounter::pmDowncast ( )
protectedpure virtual

◆ isDone()

bool ripple::TimeoutCounter::isDone ( ) const
protected

Definition at line 116 of file TimeoutCounter.h.

◆ invokeOnTimer()

void ripple::TimeoutCounter::invokeOnTimer ( )
private

Calls onTimer() if in the right state.

Only called by queueJob().

Definition at line 93 of file TimeoutCounter.cpp.

Member Data Documentation

◆ app_

Application& ripple::TimeoutCounter::app_
protected

Definition at line 123 of file TimeoutCounter.h.

◆ journal_

beast::Journal ripple::TimeoutCounter::journal_
protected

Definition at line 124 of file TimeoutCounter.h.

◆ mtx_

std::recursive_mutex ripple::TimeoutCounter::mtx_
mutableprotected

Definition at line 125 of file TimeoutCounter.h.

◆ hash_

const uint256 ripple::TimeoutCounter::hash_
protected

The hash of the object (in practice, always a ledger) we are trying to fetch.

Definition at line 129 of file TimeoutCounter.h.

◆ timeouts_

int ripple::TimeoutCounter::timeouts_
protected

Definition at line 130 of file TimeoutCounter.h.

◆ complete_

bool ripple::TimeoutCounter::complete_
protected

Definition at line 131 of file TimeoutCounter.h.

◆ failed_

bool ripple::TimeoutCounter::failed_
protected

Definition at line 132 of file TimeoutCounter.h.

◆ progress_

bool ripple::TimeoutCounter::progress_
protected

Whether forward progress has been made.

Definition at line 134 of file TimeoutCounter.h.

◆ timerInterval_

std::chrono::milliseconds ripple::TimeoutCounter::timerInterval_
protected

The minimum time to wait between calls to execute().

Definition at line 136 of file TimeoutCounter.h.

◆ queueJobParameter_

QueueJobParameter ripple::TimeoutCounter::queueJobParameter_
protected

Definition at line 138 of file TimeoutCounter.h.

◆ timer_

boost::asio::basic_waitable_timer<std::chrono::steady_clock> ripple::TimeoutCounter::timer_
private

Definition at line 147 of file TimeoutCounter.h.