rippled
Loading...
Searching...
No Matches
TimeoutCounter.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/ledger/detail/TimeoutCounter.h>
21#include <xrpld/app/main/Application.h>
22#include <xrpld/core/JobQueue.h>
23#include <xrpld/overlay/Overlay.h>
24
25namespace ripple {
26
27using namespace std::chrono_literals;
28
30 Application& app,
31 uint256 const& hash,
33 QueueJobParameter&& jobParameter,
34 beast::Journal journal)
35 : app_(app)
36 , journal_(journal)
37 , hash_(hash)
38 , timeouts_(0)
39 , complete_(false)
40 , failed_(false)
41 , progress_(false)
42 , timerInterval_(interval)
43 , queueJobParameter_(std::move(jobParameter))
44 , timer_(app_.getIOService())
45{
46 XRPL_ASSERT(
47 (timerInterval_ > 10ms) && (timerInterval_ < 30s),
48 "ripple::TimeoutCounter::TimeoutCounter : interval input inside range");
49}
50
51void
53{
54 if (isDone())
55 return;
56 timer_.expires_after(timerInterval_);
57 timer_.async_wait(
58 [wptr = pmDowncast()](boost::system::error_code const& ec) {
59 if (ec == boost::asio::error::operation_aborted)
60 return;
61
62 if (auto ptr = wptr.lock())
63 {
64 ScopedLockType sl(ptr->mtx_);
65 ptr->queueJob(sl);
66 }
67 });
68}
69
70void
72{
73 if (isDone())
74 return;
78 {
79 JLOG(journal_.debug()) << "Deferring " << queueJobParameter_.jobName
80 << " timer due to load";
81 setTimer(sl);
82 return;
83 }
84
88 [wptr = pmDowncast()]() {
89 if (auto sptr = wptr.lock(); sptr)
90 sptr->invokeOnTimer();
91 });
92}
93
94void
96{
98
99 if (isDone())
100 return;
101
102 if (!progress_)
103 {
104 ++timeouts_;
105 JLOG(journal_.debug())
106 << "Timeout(" << timeouts_ << ") " << " acquiring " << hash_;
107 onTimer(false, sl);
108 }
109 else
110 {
111 progress_ = false;
112 onTimer(true, sl);
113 }
114
115 if (!isDone())
116 setTimer(sl);
117}
118
119void
121{
123 if (!isDone())
124 {
125 failed_ = true;
126 JLOG(journal_.info()) << "Cancel " << hash_;
127 }
128}
129
130} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:59
Stream debug() const
Definition: Journal.h:317
Stream info() const
Definition: Journal.h:323
virtual JobQueue & getJobQueue()=0
int getJobCountTotal(JobType t) const
Jobs waiting plus running at this priority.
Definition: JobQueue.cpp:150
bool addJob(JobType type, std::string const &name, JobHandler &&jobHandler)
Adds a job to the JobQueue.
Definition: JobQueue.h:166
void queueJob(ScopedLockType &)
Queue a job to call invokeOnTimer().
virtual void onTimer(bool progress, ScopedLockType &)=0
Hook called from invokeOnTimer().
bool progress_
Whether forward progress has been made.
void setTimer(ScopedLockType &)
Schedule a call to queueJob() after mTimerInterval.
void invokeOnTimer()
Calls onTimer() if in the right state.
std::chrono::milliseconds timerInterval_
The minimum time to wait between calls to execute().
QueueJobParameter queueJobParameter_
beast::Journal journal_
uint256 const hash_
The hash of the object (in practice, always a ledger) we are trying to fetch.
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
virtual std::weak_ptr< TimeoutCounter > pmDowncast()=0
Return a weak pointer to this.
TimeoutCounter(Application &app, uint256 const &targetHash, std::chrono::milliseconds timeoutInterval, QueueJobParameter &&jobParameter, beast::Journal journal)
virtual void cancel()
Cancel the task by marking it as failed if the task is not done.
std::recursive_mutex mtx_
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
STL namespace.
std::optional< std::uint32_t > jobLimit