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/core/JobQueue.h>
22
23namespace ripple {
24
25using namespace std::chrono_literals;
26
28 Application& app,
29 uint256 const& hash,
31 QueueJobParameter&& jobParameter,
32 beast::Journal journal)
33 : app_(app)
34 , journal_(journal)
35 , hash_(hash)
36 , timeouts_(0)
37 , complete_(false)
38 , failed_(false)
39 , progress_(false)
40 , timerInterval_(interval)
41 , queueJobParameter_(std::move(jobParameter))
42 , timer_(app_.getIOService())
43{
44 XRPL_ASSERT(
45 (timerInterval_ > 10ms) && (timerInterval_ < 30s),
46 "ripple::TimeoutCounter::TimeoutCounter : interval input inside range");
47}
48
49void
51{
52 if (isDone())
53 return;
54 timer_.expires_after(timerInterval_);
55 timer_.async_wait(
56 [wptr = pmDowncast()](boost::system::error_code const& ec) {
57 if (ec == boost::asio::error::operation_aborted)
58 return;
59
60 if (auto ptr = wptr.lock())
61 {
62 ScopedLockType sl(ptr->mtx_);
63 ptr->queueJob(sl);
64 }
65 });
66}
67
68void
70{
71 if (isDone())
72 return;
76 {
77 JLOG(journal_.debug()) << "Deferring " << queueJobParameter_.jobName
78 << " timer due to load";
79 setTimer(sl);
80 return;
81 }
82
86 [wptr = pmDowncast()]() {
87 if (auto sptr = wptr.lock(); sptr)
88 sptr->invokeOnTimer();
89 });
90}
91
92void
94{
96
97 if (isDone())
98 return;
99
100 if (!progress_)
101 {
102 ++timeouts_;
103 JLOG(journal_.debug())
104 << "Timeout(" << timeouts_ << ") " << " acquiring " << hash_;
105 onTimer(false, sl);
106 }
107 else
108 {
109 progress_ = false;
110 onTimer(true, sl);
111 }
112
113 if (!isDone())
114 setTimer(sl);
115}
116
117void
119{
121 if (!isDone())
122 {
123 failed_ = true;
124 JLOG(journal_.info()) << "Cancel " << hash_;
125 }
126}
127
128} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:60
Stream debug() const
Definition: Journal.h:328
Stream info() const
Definition: Journal.h:334
virtual JobQueue & getJobQueue()=0
int getJobCountTotal(JobType t) const
Jobs waiting plus running at this priority.
Definition: JobQueue.cpp:152
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