xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
app
ledger
detail
TimeoutCounter.cpp
1
#include <xrpld/app/ledger/detail/TimeoutCounter.h>
2
3
#include <xrpld/app/main/Application.h>
4
5
#include <xrpl/basics/Log.h>
6
#include <xrpl/basics/base_uint.h>
7
#include <xrpl/beast/utility/Journal.h>
8
#include <xrpl/beast/utility/instrumentation.h>
9
#include <xrpl/core/JobQueue.h>
10
11
#include <boost/asio/error.hpp>
12
#include <boost/system/detail/error_code.hpp>
13
14
#include <
chrono
>
15
#include <
utility
>
16
17
namespace
xrpl
{
18
19
using namespace
std::chrono_literals;
20
21
TimeoutCounter::TimeoutCounter
(
22
Application
& app,
23
UInt256
const
& hash,
24
std::chrono::milliseconds
interval,
25
QueueJobParameter
&& jobParameter,
26
beast::Journal
journal)
27
:
app_
(app)
28
,
journal_
(journal)
29
,
hash_
(hash)
30
,
timerInterval_
(interval)
31
,
queueJobParameter_
(
std
::move(jobParameter))
32
,
timer_
(
app_
.getIOContext())
33
{
34
XRPL_ASSERT(
35
(
timerInterval_
> 10ms) && (
timerInterval_
< 30s),
36
"xrpl::TimeoutCounter::TimeoutCounter : interval input inside range"
);
37
}
38
39
void
40
TimeoutCounter::setTimer
(
ScopedLockType
& sl)
41
{
42
if
(
isDone
())
43
return
;
44
timer_
.expires_after(
timerInterval_
);
45
timer_
.async_wait([wptr =
pmDowncast
()](boost::system::error_code
const
& ec) {
46
if
(ec == boost::asio::error::operation_aborted)
47
return
;
48
49
if
(
auto
ptr = wptr.lock())
50
{
51
ScopedLockType
sl(ptr->mtx_);
52
ptr->queueJob(sl);
53
}
54
});
55
}
56
57
void
58
TimeoutCounter::queueJob
(
ScopedLockType
& sl)
59
{
60
if
(
isDone
())
61
return
;
62
if
(
queueJobParameter_
.jobLimit &&
63
app_
.getJobQueue().getJobCountTotal(
queueJobParameter_
.jobType) >=
64
queueJobParameter_
.jobLimit)
65
{
66
JLOG(
journal_
.debug()) <<
"Deferring "
<<
queueJobParameter_
.jobName
67
<<
" timer due to load"
;
68
setTimer
(sl);
69
return
;
70
}
71
72
app_
.getJobQueue().addJob(
73
queueJobParameter_
.jobType,
queueJobParameter_
.jobName, [wptr =
pmDowncast
()]() {
74
if
(
auto
sptr = wptr.lock(); sptr)
75
sptr->invokeOnTimer();
76
});
77
}
78
79
void
80
TimeoutCounter::invokeOnTimer
()
81
{
82
ScopedLockType
sl(
mtx_
);
83
84
if
(
isDone
())
85
return
;
86
87
if
(!
progress_
)
88
{
89
++
timeouts_
;
90
JLOG(
journal_
.debug()) <<
"Timeout("
<<
timeouts_
<<
") "
91
<<
" acquiring "
<<
hash_
;
92
onTimer
(
false
, sl);
93
}
94
else
95
{
96
progress_
=
false
;
97
onTimer
(
true
, sl);
98
}
99
100
if
(!
isDone
())
101
setTimer
(sl);
102
}
103
104
void
105
TimeoutCounter::cancel
()
106
{
107
ScopedLockType
const
sl(
mtx_
);
108
if
(!
isDone
())
109
{
110
failed_
=
true
;
111
JLOG(
journal_
.info()) <<
"Cancel "
<<
hash_
;
112
}
113
}
114
115
}
// namespace xrpl
chrono
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
xrpl::Application
Definition
Application.h:94
xrpl::TimeoutCounter::timer_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
Definition
TimeoutCounter.h:150
xrpl::TimeoutCounter::mtx_
std::recursive_mutex mtx_
Definition
TimeoutCounter.h:121
xrpl::TimeoutCounter::pmDowncast
virtual std::weak_ptr< TimeoutCounter > pmDowncast()=0
Return a weak pointer to this.
xrpl::TimeoutCounter::onTimer
virtual void onTimer(bool progress, ScopedLockType &)=0
Hook called from invokeOnTimer().
xrpl::TimeoutCounter::isDone
bool isDone() const
Definition
TimeoutCounter.h:112
xrpl::TimeoutCounter::ScopedLockType
std::unique_lock< std::recursive_mutex > ScopedLockType
Definition
TimeoutCounter.h:71
xrpl::TimeoutCounter::hash_
UInt256 const hash_
The hash of the object (in practice, always a ledger) we are trying to fetch.
Definition
TimeoutCounter.h:127
xrpl::TimeoutCounter::queueJob
void queueJob(ScopedLockType &)
Queue a job to call invokeOnTimer().
Definition
TimeoutCounter.cpp:58
xrpl::TimeoutCounter::TimeoutCounter
TimeoutCounter(Application &app, UInt256 const &targetHash, std::chrono::milliseconds timeoutInterval, QueueJobParameter &&jobParameter, beast::Journal journal)
Definition
TimeoutCounter.cpp:21
xrpl::TimeoutCounter::progress_
bool progress_
Whether forward progress has been made.
Definition
TimeoutCounter.h:134
xrpl::TimeoutCounter::app_
Application & app_
Definition
TimeoutCounter.h:119
xrpl::TimeoutCounter::journal_
beast::Journal journal_
Definition
TimeoutCounter.h:120
xrpl::TimeoutCounter::queueJobParameter_
QueueJobParameter queueJobParameter_
Definition
TimeoutCounter.h:140
xrpl::TimeoutCounter::failed_
bool failed_
Definition
TimeoutCounter.h:130
xrpl::TimeoutCounter::invokeOnTimer
void invokeOnTimer()
Calls onTimer() if in the right state.
Definition
TimeoutCounter.cpp:80
xrpl::TimeoutCounter::setTimer
void setTimer(ScopedLockType &)
Schedule a call to queueJob() after timerInterval_.
Definition
TimeoutCounter.cpp:40
xrpl::TimeoutCounter::timerInterval_
std::chrono::milliseconds timerInterval_
The minimum time to wait between calls to execute().
Definition
TimeoutCounter.h:138
xrpl::TimeoutCounter::timeouts_
int timeouts_
Definition
TimeoutCounter.h:128
xrpl::TimeoutCounter::cancel
virtual void cancel()
Cancel the task by marking it as failed if the task is not done.
Definition
TimeoutCounter.cpp:105
std::chrono::milliseconds
std
STL namespace.
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::UInt256
BaseUInt< 256 > UInt256
Definition
base_uint.h:580
xrpl::TimeoutCounter::QueueJobParameter
Definition
TimeoutCounter.h:74
utility
Generated by
1.17.0