use CScheduler for HTTPRPCTimer

This removes the dependency on libevent for scheduled events,
like re-locking a wallet some time after decryption.
This commit is contained in:
Matthew Zipkin 2025-01-17 10:04:59 -05:00 committed by Matthew Zipkin
parent 196698c43e
commit a792549519
No known key found for this signature in database
GPG key ID: E7E2984B6289C93A

View file

@ -9,8 +9,10 @@
#include <httpserver.h> #include <httpserver.h>
#include <logging.h> #include <logging.h>
#include <netaddress.h> #include <netaddress.h>
#include <node/context.h>
#include <rpc/protocol.h> #include <rpc/protocol.h>
#include <rpc/server.h> #include <rpc/server.h>
#include <scheduler.h>
#include <util/fs.h> #include <util/fs.h>
#include <util/fs_helpers.h> #include <util/fs_helpers.h>
#include <util/strencodings.h> #include <util/strencodings.h>
@ -27,6 +29,7 @@
#include <vector> #include <vector>
using http_libevent::HTTPRequest; using http_libevent::HTTPRequest;
using node::NodeContext;
using util::SplitString; using util::SplitString;
using util::TrimStringView; using util::TrimStringView;
@ -39,22 +42,16 @@ static const char* WWW_AUTH_HEADER_DATA = "Basic realm=\"jsonrpc\"";
class HTTPRPCTimer : public RPCTimerBase class HTTPRPCTimer : public RPCTimerBase
{ {
public: public:
HTTPRPCTimer(struct event_base* eventBase, std::function<void()>& func, int64_t millis) : HTTPRPCTimer(NodeContext* context, std::function<void()>& func, int64_t millis)
ev(eventBase, false, func)
{ {
struct timeval tv; context->scheduler->scheduleFromNow(func, std::chrono::milliseconds(millis));
tv.tv_sec = millis/1000;
tv.tv_usec = (millis%1000)*1000;
ev.trigger(&tv);
} }
private:
HTTPEvent ev;
}; };
class HTTPRPCTimerInterface : public RPCTimerInterface class HTTPRPCTimerInterface : public RPCTimerInterface
{ {
public: public:
explicit HTTPRPCTimerInterface(struct event_base* _base) : base(_base) explicit HTTPRPCTimerInterface(const std::any& context) : m_context(std::any_cast<NodeContext*>(context))
{ {
} }
const char* Name() override const char* Name() override
@ -63,10 +60,10 @@ public:
} }
RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) override RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) override
{ {
return new HTTPRPCTimer(base, func, millis); return new HTTPRPCTimer(m_context, func, millis);
} }
private: private:
struct event_base* base; NodeContext* m_context;
}; };
@ -371,9 +368,7 @@ bool StartHTTPRPC(const std::any& context)
if (g_wallet_init_interface.HasWalletSupport()) { if (g_wallet_init_interface.HasWalletSupport()) {
RegisterHTTPHandler("/wallet/", false, handle_rpc); RegisterHTTPHandler("/wallet/", false, handle_rpc);
} }
struct event_base* eventBase = EventBase(); httpRPCTimerInterface = std::make_unique<HTTPRPCTimerInterface>(context);
assert(eventBase);
httpRPCTimerInterface = std::make_unique<HTTPRPCTimerInterface>(eventBase);
RPCSetTimerInterface(httpRPCTimerInterface.get()); RPCSetTimerInterface(httpRPCTimerInterface.get());
return true; return true;
} }