forked from libbitcoin/libbitcoin-node
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprotocol_performer.cpp
More file actions
161 lines (133 loc) · 4.16 KB
/
protocol_performer.cpp
File metadata and controls
161 lines (133 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/node/protocols/protocol_performer.hpp>
#include <chrono>
#include <bitcoin/node/protocols/protocol.hpp>
#include <bitcoin/node/define.hpp>
namespace libbitcoin {
namespace node {
#define CLASS protocol_performer
using namespace system;
using namespace network;
using namespace std::chrono;
using namespace std::placeholders;
void protocol_performer::start_performance() NOEXCEPT
{
if (stopped())
return;
if (enabled_)
{
bytes_ = zero;
start_ = steady_clock::now();
performance_timer_->start(BIND(handle_performance_timer, _1));
}
}
void protocol_performer::handle_performance_timer(const code& ec) NOEXCEPT
{
BC_ASSERT(stranded());
if (ec == network::error::operation_canceled ||
ec == network::error::service_stopped)
return;
if (stopped())
return;
if (ec)
{
LOGF("Performance timer failure, " << ec.message());
stop(ec);
return;
}
if (is_idle())
{
// Channel is exhausted, performance no longer relevant.
pause_performance();
return;
}
// Submit performance to (outbound session) aggregate monitor in bytes/sec.
send_performance(floored_divide(bytes_, greater(sign_cast<uint64_t>(
duration_cast<seconds>(steady_clock::now() - start_).count()), one)));
}
void protocol_performer::pause_performance() NOEXCEPT
{
BC_ASSERT(stranded());
send_performance(max_uint64);
}
void protocol_performer::stop_performance() NOEXCEPT
{
BC_ASSERT(stranded());
send_performance(zero);
}
void protocol_performer::send_performance(uint64_t rate) NOEXCEPT
{
BC_ASSERT(stranded());
if (enabled_)
{
// Must come first as this takes priority as per configuration.
// Shared performance manager detects slow and stalled channels.
if (deviation_)
{
performance_timer_->stop();
performance(rate, BIND(handle_send_performance, _1));
return;
}
// Protocol performance manager detects only stalled channels.
const auto ec = is_zero(rate) ? error::stalled_channel :
(rate == max_uint64 ? error::exhausted_channel :
error::success);
performance_timer_->stop();
do_handle_performance(ec);
}
}
void protocol_performer::handle_send_performance(const code& ec) NOEXCEPT
{
if (stopped())
return;
POST(do_handle_performance, ec);
}
void protocol_performer::do_handle_performance(const code& ec) NOEXCEPT
{
BC_ASSERT(stranded());
if (stopped())
return;
// Caused only by performance(max) - had no outstanding work.
// Timer stopped until chase::download event restarts it.
if (ec == error::exhausted_channel)
return;
// Caused only by performance(zero|xxx) - had outstanding work.
if (ec == error::stalled_channel || ec == error::slow_channel)
{
LOGP("Channel dropped [" << opposite() << "] " << ec.message());
stop(ec);
return;
}
if (ec)
{
LOGF("Performance failure [" << opposite() << "] " << ec.message());
stop(ec);
return;
}
// Restart performance timing cycle.
start_performance();
}
void protocol_performer::count(size_t bytes) NOEXCEPT
{
BC_ASSERT(stranded());
bytes_ = ceilinged_add(bytes_, possible_wide_cast<uint64_t>(bytes));
}
} // namespace node
} // namespace libbitcoin