mirror of
https://github.com/SChernykh/p2pool.git
synced 2024-08-20 05:28:47 -06:00
ZMQReader: more reliable connect()
This commit is contained in:
parent
3d60ae8c32
commit
a008eac8c6
@ -19,6 +19,7 @@
|
|||||||
#include "zmq_reader.h"
|
#include "zmq_reader.h"
|
||||||
#include "json_parsers.h"
|
#include "json_parsers.h"
|
||||||
#include <rapidjson/document.h>
|
#include <rapidjson/document.h>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
static constexpr char log_category_prefix[] = "ZMQReader ";
|
static constexpr char log_category_prefix[] = "ZMQReader ";
|
||||||
|
|
||||||
@ -88,10 +89,14 @@ void ZMQReader::run()
|
|||||||
char addr[32];
|
char addr[32];
|
||||||
|
|
||||||
snprintf(addr, sizeof(addr), "tcp://%s:%u", m_address, m_zmqPort);
|
snprintf(addr, sizeof(addr), "tcp://%s:%u", m_address, m_zmqPort);
|
||||||
while (!connect(addr, m_zmqPort)) { if (m_finished.load()) return; }
|
if (!connect(addr)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", m_publisherPort);
|
snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", m_publisherPort);
|
||||||
while (!connect(addr, m_publisherPort)) { if (m_finished.load()) return; }
|
if (!connect(addr)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
m_subscriber.set(zmq::sockopt::subscribe, "json-full-chain_main");
|
m_subscriber.set(zmq::sockopt::subscribe, "json-full-chain_main");
|
||||||
m_subscriber.set(zmq::sockopt::subscribe, "json-full-miner_data");
|
m_subscriber.set(zmq::sockopt::subscribe, "json-full-miner_data");
|
||||||
@ -126,7 +131,7 @@ void ZMQReader::run()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ZMQReader::connect(const char* address, uint32_t id)
|
bool ZMQReader::connect(const char* address)
|
||||||
{
|
{
|
||||||
struct ConnectMonitor : public zmq::monitor_t
|
struct ConnectMonitor : public zmq::monitor_t
|
||||||
{
|
{
|
||||||
@ -139,20 +144,34 @@ bool ZMQReader::connect(const char* address, uint32_t id)
|
|||||||
bool connected = false;
|
bool connected = false;
|
||||||
} monitor;
|
} monitor;
|
||||||
|
|
||||||
char buf[32];
|
static uint64_t id = 0;
|
||||||
snprintf(buf, sizeof(buf), "inproc://connect-mon-%u", id);
|
|
||||||
|
if (!id) {
|
||||||
|
std::random_device rd;
|
||||||
|
id = (static_cast<uint64_t>(rd()) << 32) | static_cast<uint32_t>(rd());
|
||||||
|
}
|
||||||
|
|
||||||
|
char buf[log::Stream::BUF_SIZE + 1];
|
||||||
|
log::Stream s(buf);
|
||||||
|
s << "inproc://p2pool-connect-mon-" << id << '\0';
|
||||||
|
++id;
|
||||||
|
|
||||||
monitor.init(m_subscriber, buf);
|
monitor.init(m_subscriber, buf);
|
||||||
m_subscriber.connect(address);
|
m_subscriber.connect(address);
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
const system_clock::time_point start_time = system_clock::now();
|
system_clock::time_point start_time = system_clock::now();
|
||||||
|
|
||||||
while (!monitor.connected && monitor.check_event(-1)) {
|
while (!monitor.connected && monitor.check_event(-1)) {
|
||||||
const int64_t elapsed_time = duration_cast<milliseconds>(system_clock::now() - start_time).count();
|
const system_clock::time_point cur_time = system_clock::now();
|
||||||
|
const int64_t elapsed_time = duration_cast<milliseconds>(cur_time - start_time).count();
|
||||||
if (elapsed_time >= 3000) {
|
if (elapsed_time >= 3000) {
|
||||||
LOGERR(1, "failed to connect to " << address);
|
LOGERR(1, "failed to connect to " << address);
|
||||||
|
if (m_finished.load()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
start_time = cur_time;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -30,7 +30,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
static void run_wrapper(void* arg);
|
static void run_wrapper(void* arg);
|
||||||
void run();
|
void run();
|
||||||
bool connect(const char* address, uint32_t id);
|
bool connect(const char* address);
|
||||||
|
|
||||||
void parse(char* data, size_t size);
|
void parse(char* data, size_t size);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user