• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-network / 35561354381

21 Sep 2026 04:31AM UTC coverage: 48.498% (+0.04%) from 48.46%
35561354381

push

github

web-flow
Merge pull request #908 from evoskuil/master

Implement outbound notification HWM.

110 of 147 new or added lines in 15 files covered. (74.83%)

21 existing lines in 11 files now uncovered.

28017 of 57769 relevant lines covered (48.5%)

163473.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.88
/src/net/proxy.cpp
1
/**
2
 * Copyright (c) 2011-2026 libbitcoin developers
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <bitcoin/network/net/proxy.hpp>
20

21
#include <bitcoin/network/define.hpp>
22
#include <bitcoin/network/log/log.hpp>
23
#include <bitcoin/network/net/socket.hpp>
24

25
namespace libbitcoin {
26
namespace network {
27

28
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
29

30
// Factory for variable deadline timer pointer construction (or null).
31
inline deadline::ptr make_throttle(const logger& log, asio::strand& strand,
32
    uint32_t rate_limit) NOEXCEPT
33
{
34
    return to_bool(rate_limit) ?
66 ✔
35
        system::emplace_shared<deadline>(log, strand) : nullptr;
36
}
37

38
// This is created in a started state and must be stopped, as the subscribers
39
// assert if not stopped. Subscribers may hold protocols even if the service
40
// is not started.
41
proxy::proxy(const socket::ptr& socket, uint32_t rate_limit,
66 ✔
42
    size_t maximum_backlog) NOEXCEPT
66 ✔
43
  : rate_limit_(rate_limit),
66 ✔
44
    maximum_backlog_(maximum_backlog),
66 ✔
45
    socket_(socket),
46
    throttle_(make_throttle(socket->log, socket->strand(), rate_limit)),
66 ✔
47
    reporter(socket->log)
132 ✔
48
{
49
}
66 ✔
50

51
proxy::~proxy() NOEXCEPT
66 ✔
52
{
53
    BC_ASSERT_MSG(stopped(), "proxy is not stopped");
66 ✔
54
    if (!stopped()) { LOGF("~proxy is not stopped."); }
66 ✔
55
}
132 ✔
56

57
// Stop (socket/proxy started upon create).
58
// ----------------------------------------------------------------------------
59
// The proxy does not (must not) stop itself.
60

61
void proxy::stop(const code& ec) NOEXCEPT
81 ✔
62
{
63
    if (stopped())
81 ✔
64
        return;
65

66
    // Overruled by stop, set only for consistency.
67
    paused_.store(true);
68

69
    // An open batch response is closed (written) before the socket stops.
70
    // Batch state is protected by the strand, so only a stranded stop can
71
    // close gracefully (an unstranded stop, e.g. shutdown, is immediate).
72
    if (stranded() && batched_ && parted_)
66 ✔
73
    {
74
        do_stop(ec);
×
75
        return;
×
76
    }
77

78
    finish_stop(ec);
66 ✔
79
}
80

81
// private
82
// Close the open batch response, then stop (batch open rides on the first
83
// part, so there is nothing to close unless parted).
84
void proxy::do_stop(const code& ec) NOEXCEPT
×
85
{
86
    BC_ASSERT(stranded());
×
87
    using namespace std::placeholders;
88

89
    // The socket is not yet stopped, so a deferred send still holds the queue.
90
    // Release it here so that the close part is not delayed by the throttle.
91
    if (throttle_) throttle_->stop();
×
92

93
    batched_ = false;
×
94
    parted_ = false;
×
95

96
    rpc::response close{};
×
97
    close.batch = true;
×
98
    close.changed = true;
×
99

100
    const count_handler complete = std::bind(&proxy::handle_stop_write,
×
101
        shared_from_this(), _1, _2, ec);
×
102

103
    // The close part is written per framing (http chunk or stream).
104
    if (parser_)
×
105
    {
106
        socket_->rpc_write_chunk(std::move(close), move_copy(complete));
×
107
        return;
108
    }
109

110
    // This is the final message of the channel, so it is not bounded.
111
    const auto bounded = false;
112
    const auto out = system::move_shared(std::move(close));
NEW
113
    do_write({ zero, std::bind(&proxy::do_response_write,
×
NEW
114
        shared_from_this(), out, complete), complete }, bounded);
×
UNCOV
115
}
×
116

117
// private
118
void proxy::handle_stop_write(const code&, size_t,
×
119
    const code& reason) NOEXCEPT
120
{
121
    BC_ASSERT(stranded());
×
122
    finish_stop(reason);
×
123
}
×
124

125
// private
126
// Thread safe (called directly from stop or from the strand).
127
void proxy::finish_stop(const code& ec) NOEXCEPT
66 ✔
128
{
129
    // Client websocket or timer initated (when ws) stop is async (graceful).
130
    // error::service_stopped will invoke socket stop() below, which will
131
    // immediately terminate outstanding lazy_stop().
132
    if (ec == error::websocket_closed ||
66 ✔
133
        ec == error::channel_expired ||
66 ✔
134
        ec == error::channel_inactive)
66 ✔
135
    {
136
        // Stop the read loop, stop accepting new work, cancel pending work.
137
        // Allows for graceful ws/ssl::close, which would hang threadpool if
138
        // attempted within socket::stop(), as it issues a follow-on iocontext
139
        // job. A subsequent call to socket::stop() will terminate directly.
140
        socket_->lazy_stop();
×
141
    }
142
    else
143
    {
144
        // Stop the read loop, stop accepting new work, cancel pending work.
145
        socket_->stop();
66 ✔
146
    }
147

148
    boost::asio::post(strand(),
66 ✔
149
        std::bind(&proxy::stopping, shared_from_this(), ec));
132 ✔
150
}
66 ✔
151

152
// protected
153
// This should not be called internally (invoked by stop()).
154
void proxy::stopping(const code& ec) NOEXCEPT
66 ✔
155
{
156
    BC_ASSERT(stranded());
66 ✔
157

158
    // Release any deferred send (fires pending charge with canceled).
159
    if (throttle_) throttle_->stop();
66 ✔
160

161
    // Release any http message parse in progress.
162
    parser_.reset();
66 ✔
163

164
    // Post stop handlers to strand and clear/stop accepting subscriptions.
165
    // The code provides information on the reason that the channel stopped.
166
    stop_subscriber_.stop(ec);
66 ✔
167
}
66 ✔
168

169
// protected
170
void proxy::subscribe_stop(result_handler&& handler) NOEXCEPT
10 ✔
171
{
172
    BC_ASSERT(stranded());
10 ✔
173
    stop_subscriber_.subscribe(std::move(handler));
10 ✔
174
}
10 ✔
175

176
void proxy::subscribe_stop(result_handler&& handler,
14 ✔
177
    result_handler&& complete) NOEXCEPT
178
{
179
    if (stopped())
14 ✔
180
    {
181
        complete(error::channel_stopped);
4 ✔
182
        handler(error::channel_stopped);
4 ✔
183
        return;
4 ✔
184
    }
185

186
    boost::asio::dispatch(strand(),
10 ✔
187
        std::bind(&proxy::do_subscribe_stop,
20 ✔
188
            shared_from_this(), std::move(handler), std::move(complete)));
10 ✔
189
}
190

191
// private
192
void proxy::do_subscribe_stop(const result_handler& handler,
10 ✔
193
    const result_handler& complete) NOEXCEPT
194
{
195
    BC_ASSERT(stranded());
10 ✔
196
    stop_subscriber_.subscribe(move_copy(handler));
20 ✔
197
    complete(error::success);
10 ✔
198
}
10 ✔
199

200
// Pause (proxy is created paused).
201
// ----------------------------------------------------------------------------
202
// public
203

204
void proxy::pause() NOEXCEPT
15 ✔
205
{
206
    BC_ASSERT(stranded());
15 ✔
207
    paused_ = true;
208
}
15 ✔
209

210
void proxy::resume() NOEXCEPT
20 ✔
211
{
212
    BC_ASSERT(stranded());
20 ✔
213
    paused_ = false;
214
}
20 ✔
215

216
bool proxy::paused() const NOEXCEPT
49 ✔
217
{
218
    BC_ASSERT(stranded());
49 ✔
219
    return paused_;
49 ✔
220
}
221

222
// Signal activity.
223
// ----------------------------------------------------------------------------
224
// override reading() to update timers.
225

226
// protected
227
void proxy::reading() NOEXCEPT
7 ✔
228
{
229
}
7 ✔
230

231
// protected
232
void proxy::writing() NOEXCEPT
8 ✔
233
{
234
}
8 ✔
235

236
// private
237
void proxy::do_reading() NOEXCEPT
22 ✔
238
{
239
    boost::asio::dispatch(strand(),
22 ✔
240
        std::bind(&proxy::reading, shared_from_this()));
44 ✔
241
}
22 ✔
242

243
// Properties.
244
// ----------------------------------------------------------------------------
245

246
asio::context& proxy::service() const NOEXCEPT
×
247
{
248
    return socket_->service();
×
249
}
250

251
asio::strand& proxy::strand() NOEXCEPT
192 ✔
252
{
253
    return socket_->strand();
192 ✔
254
}
255

256
bool proxy::stranded() const NOEXCEPT
961 ✔
257
{
258
    return socket_->stranded();
961 ✔
259
}
260

261
bool proxy::stopped() const NOEXCEPT
517 ✔
262
{
263
    return socket_->stopped();
517 ✔
264
}
265

266
bool proxy::inbound() const NOEXCEPT
54 ✔
267
{
268
    return socket_->inbound();
54 ✔
269
}
270

271
bool proxy::secure() const NOEXCEPT
2 ✔
272
{
273
    return socket_->secure();
2 ✔
274
}
275

276
bool proxy::encrypted() const NOEXCEPT
2 ✔
277
{
278
    return socket_->encrypted();
2 ✔
279
}
280

281
bool proxy::websocket() const NOEXCEPT
5 ✔
282
{
283
    return socket_->websocket();
5 ✔
284
}
285

286
bool proxy::downgraded() const NOEXCEPT
×
287
{
288
    return socket_->downgraded();
×
289
}
290

291
bool proxy::detected() const NOEXCEPT
×
292
{
293
    return socket_->detected();
×
294
}
295

296
code proxy::accept_websocket(const http::request& request) NOEXCEPT
×
297
{
298
    return socket_->accept_websocket(request);
×
299
}
300

301
uint64_t proxy::received() const NOEXCEPT
4 ✔
302
{
303
    return received_.load();
4 ✔
304
}
305

306
uint64_t proxy::sent() const NOEXCEPT
4 ✔
307
{
308
    return sent_.load();
4 ✔
309
}
310

311
const config::address& proxy::address() const NOEXCEPT
5 ✔
312
{
313
    return socket_->address();
5 ✔
314
}
315

316
const config::endpoint& proxy::endpoint() const NOEXCEPT
66 ✔
317
{
318
    return socket_->endpoint();
66 ✔
319
}
320

321
const config::endpoint& proxy::binding() const NOEXCEPT
×
322
{
323
    return socket_->binding();
×
324
}
325

326
BC_POP_WARNING()
327

328
} // namespace network
329
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc