Skip to content

Commit b899611

Browse files
committed
[Refactor] CpuBoundWork#CpuBoundWork(): require an io_context::strand
1 parent f70bf0e commit b899611

File tree

5 files changed

+25
-22
lines changed

5 files changed

+25
-22
lines changed

lib/base/io-engine.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
using namespace icinga;
1818

19-
CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc)
19+
CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc, boost::asio::io_context::strand&)
2020
: m_Done(false)
2121
{
2222
auto& ioEngine (IoEngine::Get());

lib/base/io-engine.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <boost/exception/all.hpp>
1818
#include <boost/asio/deadline_timer.hpp>
1919
#include <boost/asio/io_context.hpp>
20+
#include <boost/asio/io_context_strand.hpp>
2021
#include <boost/asio/spawn.hpp>
2122

2223
namespace icinga
@@ -30,7 +31,7 @@ namespace icinga
3031
class CpuBoundWork
3132
{
3233
public:
33-
CpuBoundWork(boost::asio::yield_context yc);
34+
CpuBoundWork(boost::asio::yield_context yc, boost::asio::io_context::strand&);
3435
CpuBoundWork(const CpuBoundWork&) = delete;
3536
CpuBoundWork(CpuBoundWork&&) = delete;
3637
CpuBoundWork& operator=(const CpuBoundWork&) = delete;

lib/methods/ifwapichecktask.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ static void ReportIfwCheckResult(
7272
}
7373

7474
static void ReportIfwCheckResult(
75-
boost::asio::yield_context yc, const Checkable::Ptr& checkable, const Value& cmdLine,
76-
const CheckResult::Ptr& cr, const String& output, double start
75+
boost::asio::yield_context yc, boost::asio::io_context::strand& strand, const Checkable::Ptr& checkable,
76+
const Value& cmdLine, const CheckResult::Ptr& cr, const String& output, double start
7777
)
7878
{
7979
double end = Utility::GetTime();
80-
CpuBoundWork cbw (yc);
80+
CpuBoundWork cbw (yc, strand);
8181

8282
ReportIfwCheckResult(checkable, cmdLine, cr, output, start, end);
8383
}
@@ -94,9 +94,9 @@ static const char* GetUnderstandableError(const std::exception& ex)
9494
}
9595

9696
static void DoIfwNetIo(
97-
boost::asio::yield_context yc, const Checkable::Ptr& checkable, const Array::Ptr& cmdLine,
98-
const CheckResult::Ptr& cr, const String& psCommand, const String& psHost, const String& san, const String& psPort,
99-
AsioTlsStream& conn, boost::beast::http::request<boost::beast::http::string_body>& req, double start
97+
boost::asio::yield_context yc, boost::asio::io_context::strand& strand, const Checkable::Ptr& checkable,
98+
const Array::Ptr& cmdLine, const CheckResult::Ptr& cr, const String& psCommand, const String& psHost, const String& san,
99+
const String& psPort, AsioTlsStream& conn, boost::beast::http::request<boost::beast::http::string_body>& req, double start
100100
)
101101
{
102102
namespace http = boost::beast::http;
@@ -108,7 +108,7 @@ static void DoIfwNetIo(
108108
Connect(conn.lowest_layer(), psHost, psPort, yc);
109109
} catch (const std::exception& ex) {
110110
ReportIfwCheckResult(
111-
yc, checkable, cmdLine, cr,
111+
yc, strand, checkable, cmdLine, cr,
112112
"Can't connect to IfW API on host '" + psHost + "' port '" + psPort + "': " + GetUnderstandableError(ex),
113113
start
114114
);
@@ -121,7 +121,7 @@ static void DoIfwNetIo(
121121
sslConn.async_handshake(conn.next_layer().client, yc);
122122
} catch (const std::exception& ex) {
123123
ReportIfwCheckResult(
124-
yc, checkable, cmdLine, cr,
124+
yc, strand, checkable, cmdLine, cr,
125125
"TLS handshake with IfW API on host '" + psHost + "' (SNI: '" + san
126126
+ "') port '" + psPort + "' failed: " + GetUnderstandableError(ex),
127127
start
@@ -139,7 +139,7 @@ static void DoIfwNetIo(
139139
}
140140

141141
ReportIfwCheckResult(
142-
yc, checkable, cmdLine, cr,
142+
yc, strand, checkable, cmdLine, cr,
143143
"Certificate validation failed for IfW API on host '" + psHost + "' (SNI: '" + san + "'; CN: "
144144
+ (cn.IsString() ? "'" + cn + "'" : "N/A") + ") port '" + psPort + "': " + sslConn.GetVerifyError(),
145145
start
@@ -152,7 +152,7 @@ static void DoIfwNetIo(
152152
conn.async_flush(yc);
153153
} catch (const std::exception& ex) {
154154
ReportIfwCheckResult(
155-
yc, checkable, cmdLine, cr,
155+
yc, strand, checkable, cmdLine, cr,
156156
"Can't send HTTP request to IfW API on host '" + psHost + "' port '" + psPort + "': " + GetUnderstandableError(ex),
157157
start
158158
);
@@ -163,7 +163,7 @@ static void DoIfwNetIo(
163163
http::async_read(conn, buf, resp, yc);
164164
} catch (const std::exception& ex) {
165165
ReportIfwCheckResult(
166-
yc, checkable, cmdLine, cr,
166+
yc, strand, checkable, cmdLine, cr,
167167
"Can't read HTTP response from IfW API on host '" + psHost + "' port '" + psPort + "': " + GetUnderstandableError(ex),
168168
start
169169
);
@@ -177,7 +177,7 @@ static void DoIfwNetIo(
177177
sslConn.async_shutdown(yc[ec]);
178178
}
179179

180-
CpuBoundWork cbw (yc);
180+
CpuBoundWork cbw (yc, strand);
181181
Value jsonRoot;
182182

183183
try {
@@ -525,7 +525,7 @@ void IfwApiCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
525525

526526
Defer cancelTimeout ([&timeout]() { timeout->Cancel(); });
527527

528-
DoIfwNetIo(yc, checkable, cmdLine, cr, psCommand, psHost, expectedSan, psPort, *conn, *req, start);
528+
DoIfwNetIo(yc, *strand, checkable, cmdLine, cr, psCommand, psHost, expectedSan, psPort, *conn, *req, start);
529529
}
530530
);
531531
}

lib/remote/httpserverconnection.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ bool EnsureValidBody(
348348
ApiUser::Ptr& authenticatedUser,
349349
boost::beast::http::response<boost::beast::http::string_body>& response,
350350
bool& shuttingDown,
351-
boost::asio::yield_context& yc
351+
boost::asio::yield_context& yc,
352+
boost::asio::io_context::strand& strand
352353
)
353354
{
354355
namespace http = boost::beast::http;
@@ -358,7 +359,7 @@ bool EnsureValidBody(
358359
Array::Ptr permissions = authenticatedUser->GetPermissions();
359360

360361
if (permissions) {
361-
CpuBoundWork evalPermissions (yc);
362+
CpuBoundWork evalPermissions (yc, strand);
362363

363364
ObjectLock olock(permissions);
364365

@@ -440,13 +441,14 @@ bool ProcessRequest(
440441
boost::beast::http::response<boost::beast::http::string_body>& response,
441442
HttpServerConnection& server,
442443
bool& hasStartedStreaming,
443-
boost::asio::yield_context& yc
444+
boost::asio::yield_context& yc,
445+
boost::asio::io_context::strand& strand
444446
)
445447
{
446448
namespace http = boost::beast::http;
447449

448450
try {
449-
CpuBoundWork handlingRequest (yc);
451+
CpuBoundWork handlingRequest (yc, strand);
450452

451453
HttpHandler::ProcessRequest(stream, authenticatedUser, request, response, yc, server, handlingRequest);
452454
} catch (const std::exception& ex) {
@@ -558,13 +560,13 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
558560
break;
559561
}
560562

561-
if (!EnsureValidBody(*m_Stream, buf, parser, authenticatedUser, response, m_ShuttingDown, yc)) {
563+
if (!EnsureValidBody(*m_Stream, buf, parser, authenticatedUser, response, m_ShuttingDown, yc, m_IoStrand)) {
562564
break;
563565
}
564566

565567
m_Seen = std::numeric_limits<decltype(m_Seen)>::max();
566568

567-
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HasStartedStreaming, yc)) {
569+
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HasStartedStreaming, yc, m_IoStrand)) {
568570
break;
569571
}
570572

lib/remote/jsonrpcconnection.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void JsonRpcConnection::HandleIncomingMessages(boost::asio::yield_context yc)
7878
m_Seen = Utility::GetTime();
7979

8080
try {
81-
CpuBoundWork handleMessage (yc);
81+
CpuBoundWork handleMessage (yc, m_IoStrand);
8282

8383
MessageHandler(message);
8484

0 commit comments

Comments
 (0)