Skip to content

Commit 1778557

Browse files
authored
Merge pull request #880 from evoskuil/master
Change node.priority to .thread_priority, implement .memory_priority.
2 parents 0ce322a + 2d70b8f commit 1778557

File tree

11 files changed

+115
-141
lines changed

11 files changed

+115
-141
lines changed

builds/msvc/vs2022/libbitcoin-system.import.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<!-- WITH_ICU always defined in Visual Studio builds. -->
5050
<!-- NOMINMAX enables use of std::min/max without conflict. -->
5151
<!-- WIN32_LEAN_AND_MEAN avoids inclusion of certain headers, winsock.h conflicts with boost and protocol use of winsock2.h. -->
52-
<PreprocessorDefinitions>WITH_ICU;WIN32_LEAN_AND_MEAN;NOMINMAX;_WIN32_WINNT=0x0600;%(PreprocessorDefinitions)</PreprocessorDefinitions>
52+
<PreprocessorDefinitions>WITH_ICU;WIN32_LEAN_AND_MEAN;NOMINMAX;_WIN32_WINNT=0x0602;%(PreprocessorDefinitions)</PreprocessorDefinitions>
5353
<!-- Disable auto-linking for all boost-json and its dependency boost-container so they can be header only. -->
5454
<PreprocessorDefinitions>BOOST_JSON_NO_LIB;BOOST_CONTAINER_NO_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
5555
<PreprocessorDefinitions Condition="'$(Linkage-libbitcoin-system)' == 'static' Or '$(Linkage-libbitcoin-system)' == 'ltcg'">BC_STATIC;%(PreprocessorDefinitions)</PreprocessorDefinitions>

console/main.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,17 @@ BC_USE_LIBBITCOIN_MAIN
7979
int bc::system::main(int argc, char* argv[])
8080
{
8181
using namespace bc;
82-
using namespace bc::node;
8382
using namespace bc::system;
83+
using namespace bc::network;
84+
using namespace bc::node;
85+
using namespace bc::server;
8486

8587
// en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio
8688
std::ios_base::sync_with_stdio(false);
8789
set_utf8_stdio();
8890

89-
const server::web_pages web_server{};
90-
const server::explore_pages block_explorer{};
91+
const web_pages web_server{};
92+
const explore_pages block_explorer{};
9193
parser metadata(chain::selection::mainnet, block_explorer, web_server);
9294

9395
const auto& args = const_cast<const char**>(argv);
@@ -99,18 +101,7 @@ int bc::system::main(int argc, char* argv[])
99101
symbols_path = metadata.configured.log.symbols;
100102
#endif
101103

102-
// Requires _WIN32_WINNT set to 0x0602 (defaults 0x0602 in vc++ 2022).
103-
#if defined(HAVE_MSC) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
104-
105-
// Set low memory priority on the current process (testing).
106-
MEMORY_PRIORITY_INFORMATION priority{ MEMORY_PRIORITY_LOW };
107-
SetProcessInformation(
108-
GetCurrentProcess(),
109-
ProcessMemoryPriority,
110-
&priority,
111-
sizeof(priority));
112-
113-
#endif
104+
set_memory_priority(metadata.configured.node.memory_priority_());
114105

115106
executor host(metadata, cin, cout, cerr);
116107
return host.dispatch() ? 0 : -1;

include/bitcoin/node/protocols/protocol_explore.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,9 @@ class BCN_API protocol_explore
4848
}
4949

5050
protected:
51-
/// Receivers.
52-
void handle_receive_get(const code& ec,
53-
const network::http::method::get& request) NOEXCEPT override;
54-
5551
/// Dispatch.
56-
virtual bool dispatch_object(
57-
const network::http::request& request) NOEXCEPT;
52+
bool try_dispatch_object(
53+
const network::http::request& request) NOEXCEPT override;
5854
};
5955

6056
} // namespace node

include/bitcoin/node/protocols/protocol_html.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ class BCN_API protocol_html
5252
const network::http::method::get& request) NOEXCEPT override;
5353

5454
/// Dispatch.
55-
virtual bool dispatch_embedded(
55+
virtual bool try_dispatch_object(
56+
const network::http::request& request) NOEXCEPT;
57+
virtual void dispatch_file(
58+
const network::http::request& request) NOEXCEPT;
59+
virtual void dispatch_embedded(
5660
const network::http::request& request) NOEXCEPT;
5761

5862
/// Senders.

include/bitcoin/node/settings.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ class BCN_API settings
7070
settings(system::chain::selection context) NOEXCEPT;
7171

7272
/// Properties.
73-
bool priority;
7473
bool delay_inbound;
7574
bool headers_first;
75+
bool thread_priority;
76+
bool memory_priority;
7677
float allowed_deviation;
7778
uint16_t announcement_cache;
7879
uint16_t allocation_multiple;
@@ -91,7 +92,8 @@ class BCN_API settings
9192
virtual size_t maximum_concurrency_() const NOEXCEPT;
9293
virtual network::steady_clock::duration sample_period() const NOEXCEPT;
9394
virtual network::wall_clock::duration currency_window() const NOEXCEPT;
94-
virtual network::thread_priority priority_() const NOEXCEPT;
95+
virtual network::processing_priority thread_priority_() const NOEXCEPT;
96+
virtual network::memory_priority memory_priority_() const NOEXCEPT;
9597
};
9698

9799
} // namespace node

src/chasers/chaser_validate.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
3737
// Independent threadpool and strand (base class strand uses network pool).
3838
chaser_validate::chaser_validate(full_node& node) NOEXCEPT
3939
: chaser(node),
40-
threadpool_(node.config().node.threads_(), node.config().node.priority_()),
40+
threadpool_(node.config().node.threads_(),
41+
node.config().node.thread_priority_()),
4142
independent_strand_(threadpool_.service().get_executor()),
4243
subsidy_interval_(node.config().bitcoin.subsidy_interval_blocks),
4344
initial_subsidy_(node.config().bitcoin.initial_subsidy()),

src/parser.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,9 +1004,14 @@ options_metadata parser::load_settings() THROWS
10041004
"The number of threads in the validation threadpool, defaults to '32'."
10051005
)
10061006
(
1007-
"node.priority",
1008-
value<bool>(&configured.node.priority),
1009-
"Set the validation threadpool to high priority, defaults to 'true'."
1007+
"node.thread_priority",
1008+
value<bool>(&configured.node.thread_priority),
1009+
"Set validation threads to high processing priority, defaults to 'true'."
1010+
)
1011+
(
1012+
"node.memory_priority",
1013+
value<bool>(&configured.node.memory_priority),
1014+
"Set the process to high memory priority, defaults to 'true'."
10101015
)
10111016
(
10121017
"node.delay_inbound",

src/protocols/protocol_explore.cpp

Lines changed: 4 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -24,95 +24,19 @@ namespace libbitcoin {
2424
namespace node {
2525

2626
#define CLASS protocol_explore
27-
28-
using namespace boost::json;
29-
using namespace std::placeholders;
30-
using namespace network::http;
27+
3128
using namespace system;
29+
using namespace network::http;
3230

3331
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
3432

35-
// Handle get method.
36-
// ----------------------------------------------------------------------------
37-
38-
void protocol_explore::handle_receive_get(const code& ec,
39-
const method::get& request) NOEXCEPT
40-
{
41-
BC_ASSERT(stranded());
42-
43-
if (stopped(ec))
44-
return;
45-
46-
// Enforce http origin policy (requires configured hosts).
47-
if (!is_allowed_origin(*request, request->version()))
48-
{
49-
send_forbidden(*request);
50-
return;
51-
}
52-
53-
// Enforce http host header (if any hosts are configured).
54-
if (!is_allowed_host(*request, request->version()))
55-
{
56-
send_bad_host(*request);
57-
return;
58-
}
59-
60-
// Dispatch object with specified encoding.
61-
if (dispatch_object(*request))
62-
return;
63-
64-
// Embedded page site.
65-
if (dispatch_embedded(*request))
66-
return;
67-
68-
// Empty path implies malformed target (terminal).
69-
auto path = to_local_path(request->target());
70-
if (path.empty())
71-
{
72-
send_bad_target(*request);
73-
return;
74-
}
75-
76-
// If no file extension it's REST on the single/default html page.
77-
if (!path.has_extension())
78-
{
79-
path = to_local_path();
80-
81-
// Default html page (e.g. index.html) is not configured.
82-
if (path.empty())
83-
{
84-
send_not_implemented(*request);
85-
return;
86-
}
87-
}
88-
89-
// Get the single/default or explicitly requested page.
90-
auto file = get_file_body(path);
91-
if (!file.is_open())
92-
{
93-
send_not_found(*request);
94-
return;
95-
}
96-
97-
send_file(*request, std::move(file),
98-
file_mime_type(path, mime_type::application_octet_stream));
99-
}
100-
10133
// Dispatch.
10234
// ----------------------------------------------------------------------------
10335

104-
bool protocol_explore::dispatch_object(
105-
const network::http::request& request) NOEXCEPT
36+
bool protocol_explore::try_dispatch_object(const request& request) NOEXCEPT
10637
{
107-
const auto target = request.target();
108-
if (!is_origin_form(target))
109-
{
110-
send_bad_target(request);
111-
return true;
112-
}
113-
11438
wallet::uri uri{};
115-
if (!uri.decode(target))
39+
if (!uri.decode(request.target()))
11640
{
11741
send_bad_target(request);
11842
return true;

src/protocols/protocol_html.cpp

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ void protocol_html::handle_receive_get(const code& ec,
4141
if (stopped(ec))
4242
return;
4343

44-
// Enforce http origin policy (requires configured hosts).
44+
// Enforce http origin form for get.
45+
if (!is_origin_form(request->target()))
46+
{
47+
send_bad_target(*request);
48+
return;
49+
}
50+
51+
// Enforce http origin policy (if any origins are configured).
4552
if (!is_allowed_origin(*request, request->version()))
4653
{
4754
send_forbidden(*request);
@@ -55,67 +62,99 @@ void protocol_html::handle_receive_get(const code& ec,
5562
return;
5663
}
5764

58-
// Embedded page site.
59-
if (dispatch_embedded(*request))
65+
// Always try API dispatch, false if unhandled.
66+
if (try_dispatch_object(*request))
6067
return;
6168

62-
// Empty path implies malformed target (terminal).
63-
const auto path = to_local_path(request->target());
64-
if (path.empty())
69+
// Require file system dispatch if path is configured (always handles).
70+
if (!options_.path.empty())
6571
{
66-
// TODO: split out sanitize from canonicalize so that this can return
67-
// send_not_found() when the request is sanitary but not found.
68-
send_bad_target(*request);
72+
dispatch_file(*request);
6973
return;
7074
}
7175

72-
// Not open implies file not found (non-terminal).
73-
auto file = get_file_body(path);
74-
if (!file.is_open())
76+
// Require embedded dispatch if site is configured (always handles).
77+
if (options_.pages.enabled())
7578
{
76-
send_not_found(*request);
79+
dispatch_embedded(*request);
7780
return;
7881
}
7982

80-
send_file(*request, std::move(file),
81-
file_mime_type(path, mime_type::application_octet_stream));
83+
// Neither site is enabled and object dispatch doesn't support.
84+
send_not_implemented(*request);
8285
}
8386

8487
// Dispatch.
8588
// ----------------------------------------------------------------------------
8689

87-
bool protocol_html::dispatch_embedded(const request& request) NOEXCEPT
90+
bool protocol_html::try_dispatch_object(const request&) NOEXCEPT
8891
{
89-
// False only if not enabled, otherwise handled below.
90-
if (!options_.pages.enabled())
91-
return false;
92+
return false;
93+
}
9294

95+
void protocol_html::dispatch_embedded(const request& request) NOEXCEPT
96+
{
9397
const auto& pages = config().server.explore.pages;
9498
switch (const auto mime = file_mime_type(to_path(request.target())))
9599
{
96100
case mime_type::text_css:
97101
send_span(request, pages.css(), mime);
98-
return true;
102+
break;
99103
case mime_type::text_html:
100104
send_span(request, pages.html(), mime);
101-
return true;
105+
break;
102106
case mime_type::application_javascript:
103107
send_span(request, pages.ecma(), mime);
104-
return true;
108+
break;
105109
case mime_type::font_woff:
106110
case mime_type::font_woff2:
107111
send_span(request, pages.font(), mime);
108-
return true;
112+
break;
109113
case mime_type::image_png:
110114
case mime_type::image_gif:
111115
case mime_type::image_jpeg:
112116
send_span(request, pages.icon(), mime);
113-
return true;
117+
break;
114118
default:
115-
return false;
119+
send_not_found(request);
116120
}
117121
}
118122

123+
void protocol_html::dispatch_file(const request& request) NOEXCEPT
124+
{
125+
// Empty path implies malformed target (terminal).
126+
auto path = to_local_path(request.target());
127+
if (path.empty())
128+
{
129+
send_bad_target(request);
130+
return;
131+
}
132+
133+
// If no file extension it's REST on the single/default html page.
134+
if (!path.has_extension())
135+
{
136+
path = to_local_path();
137+
138+
// Default html page (e.g. index.html) is not configured.
139+
if (path.empty())
140+
{
141+
send_not_implemented(request);
142+
return;
143+
}
144+
}
145+
146+
// Get the single/default or explicitly requested page.
147+
auto file = get_file_body(path);
148+
if (!file.is_open())
149+
{
150+
send_not_found(request);
151+
return;
152+
}
153+
154+
const auto octet_stream = mime_type::application_octet_stream;
155+
send_file(request, std::move(file), file_mime_type(path, octet_stream));
156+
}
157+
119158
// Senders.
120159
// ----------------------------------------------------------------------------
121160

0 commit comments

Comments
 (0)