mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-09-07 07:12:52 +02:00
rework the pubsub listener again so the subscription is set up on its own thread
This commit is contained in:
parent
b114896e7b
commit
db6e698245
2 changed files with 33 additions and 19 deletions
|
@ -58,7 +58,6 @@ PubSubListener::PubSubListener(std::string controller_id, std::string project, s
|
||||||
PubSubListener::~PubSubListener()
|
PubSubListener::~PubSubListener()
|
||||||
{
|
{
|
||||||
_run = false;
|
_run = false;
|
||||||
_session.cancel();
|
|
||||||
if (_subscriberThread.joinable()) {
|
if (_subscriberThread.joinable()) {
|
||||||
_subscriberThread.join();
|
_subscriberThread.join();
|
||||||
}
|
}
|
||||||
|
@ -67,7 +66,9 @@ PubSubListener::~PubSubListener()
|
||||||
void PubSubListener::subscribe()
|
void PubSubListener::subscribe()
|
||||||
{
|
{
|
||||||
while (_run) {
|
while (_run) {
|
||||||
_session = _subscriber->Subscribe([this](pubsub::Message const& m, pubsub::AckHandler h) {
|
try {
|
||||||
|
fprintf(stderr, "Starting new subscription session\n");
|
||||||
|
auto session = _subscriber->Subscribe([this](pubsub::Message const& m, pubsub::AckHandler h) {
|
||||||
auto provider = opentelemetry::trace::Provider::GetTracerProvider();
|
auto provider = opentelemetry::trace::Provider::GetTracerProvider();
|
||||||
auto tracer = provider->GetTracer("PubSubListener");
|
auto tracer = provider->GetTracer("PubSubListener");
|
||||||
auto span = tracer->StartSpan("PubSubListener::onMessage");
|
auto span = tracer->StartSpan("PubSubListener::onMessage");
|
||||||
|
@ -82,9 +83,23 @@ void PubSubListener::subscribe()
|
||||||
span->SetStatus(opentelemetry::trace::StatusCode::kOk);
|
span->SetStatus(opentelemetry::trace::StatusCode::kOk);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
auto status = _session.get();
|
|
||||||
if (! status.ok() && _run) {
|
auto result = session.wait_for(std::chrono::seconds(10));
|
||||||
fprintf(stderr, "Error during Subscribe: %s\n", status.message().c_str());
|
if (result == std::future_status::timeout) {
|
||||||
|
session.cancel();
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! session.valid()) {
|
||||||
|
fprintf(stderr, "Subscription session no longer valid\n");
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (google::cloud::Status const& status) {
|
||||||
|
fprintf(stderr, "Subscription terminated with status: %s\n", status.message().c_str());
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,6 @@ class PubSubListener : public NotificationListener {
|
||||||
google::cloud::pubsub_admin::SubscriptionAdminClient _adminClient;
|
google::cloud::pubsub_admin::SubscriptionAdminClient _adminClient;
|
||||||
google::cloud::pubsub::Subscription _subscription;
|
google::cloud::pubsub::Subscription _subscription;
|
||||||
std::shared_ptr<google::cloud::pubsub::Subscriber> _subscriber;
|
std::shared_ptr<google::cloud::pubsub::Subscriber> _subscriber;
|
||||||
google::cloud::future<google::cloud::Status> _session;
|
|
||||||
std::thread _subscriberThread;
|
std::thread _subscriberThread;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue