Skip to content

Commit

Permalink
Merge pull request #212 from aivve/dev/rpc_httplib_client
Browse files Browse the repository at this point in the history
Improve httplib client use in NodeRpcProxy
  • Loading branch information
aivve authored Feb 9, 2023
2 parents 8733b51 + 6816238 commit 32f7829
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/NodeRpcProxy/NodeRpcProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ NodeRpcProxy::NodeRpcProxy(const std::string& nodeHost, unsigned short nodePort,
std::stringstream userAgent;
userAgent << "NodeRpcProxy";
m_requestHeaders = { {"User-Agent", userAgent.str()}, { "Connection", "keep-alive" } };

resetInternalState();
}

NodeRpcProxy::~NodeRpcProxy() {
try {
shutdown();

} catch (std::exception&) {
}
}
Expand Down Expand Up @@ -181,7 +183,11 @@ void NodeRpcProxy::workerThread(const INode::Callback& initialized_callback) {
m_dispatcher = &dispatcher;
ContextGroup contextGroup(dispatcher);
m_context_group = &contextGroup;

httplib::Client httpClient(m_node_url);
m_httpClient = &httpClient;
m_httpClient->enable_server_certificate_verification(false);
m_httpClient->set_connection_timeout(1000);
m_httpClient->set_keep_alive(true);
Event httpEvent(dispatcher);
m_httpEvent = &httpEvent;
m_httpEvent->set();
Expand Down Expand Up @@ -213,6 +219,7 @@ void NodeRpcProxy::workerThread(const INode::Callback& initialized_callback) {

m_dispatcher = nullptr;
m_context_group = nullptr;
m_httpClient = nullptr;
m_httpEvent = nullptr;
m_connected = false;
m_rpcProxyObserverManager.notify(&INodeRpcProxyObserver::connectionStatusUpdated, m_connected);
Expand Down Expand Up @@ -983,11 +990,7 @@ std::error_code NodeRpcProxy::binaryCommand(const std::string& comm, const Reque
try {
EventLock eventLock(*m_httpEvent);

httplib::Client httpClient(m_node_url);
httpClient.enable_server_certificate_verification(false);
httpClient.set_connection_timeout(1000);

const auto rsp = httpClient.Post(rpc_url.c_str(), m_requestHeaders, storeToBinaryKeyValue(req), "application/octet-stream");
const auto rsp = m_httpClient->Post(rpc_url.c_str(), m_requestHeaders, storeToBinaryKeyValue(req), "application/octet-stream");
if (rsp) {
if (rsp->status == 200) {
if (!loadFromBinaryKeyValue(res, rsp->body)) {
Expand All @@ -1013,11 +1016,7 @@ std::error_code NodeRpcProxy::jsonCommand(const std::string& comm, const Request
try {
EventLock eventLock(*m_httpEvent);

httplib::Client httpClient(m_node_url);
httpClient.enable_server_certificate_verification(false);
httpClient.set_connection_timeout(1000);

const auto rsp = httpClient.Post(rpc_url.c_str(), m_requestHeaders, storeToJson(req), "application/json");
const auto rsp = m_httpClient->Post(rpc_url.c_str(), m_requestHeaders, storeToJson(req), "application/json");
if (rsp) {
if (rsp->status == 200) {
if (!loadFromJson(res, rsp->body)) {
Expand Down Expand Up @@ -1047,11 +1046,7 @@ std::error_code NodeRpcProxy::jsonRpcCommand(const std::string& method, const Re
jsReq.setParams(req);
JsonRpc::JsonRpcResponse jsRes;

httplib::Client httpClient(m_node_url);
httpClient.enable_server_certificate_verification(false);
httpClient.set_connection_timeout(1000);

const auto rsp = httpClient.Post(rpc_url.c_str(), m_requestHeaders, jsReq.getBody(), "application/json");
const auto rsp = m_httpClient->Post(rpc_url.c_str(), m_requestHeaders, jsReq.getBody(), "application/json");
if (rsp) {
if (rsp->status == 200) {
jsRes.parse(rsp->body);
Expand Down
2 changes: 2 additions & 0 deletions src/NodeRpcProxy/NodeRpcProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ class NodeRpcProxy : public CryptoNote::INode {

unsigned int m_rpcTimeout;

httplib::Client* m_httpClient = nullptr;

httplib::Headers m_requestHeaders;
System::Event* m_httpEvent = nullptr;

Expand Down

0 comments on commit 32f7829

Please sign in to comment.