@@ -28,7 +28,7 @@ namespace cpp2sky {
2828TracerImpl::TracerImpl (TracerConfig& config,
2929 std::shared_ptr<grpc::ChannelCredentials> cred)
3030 : config_(config),
31- grpc_callback_thread_ ([this ] { this ->run (); }),
31+ evloop_thread_ ([this ] { this ->run (); }),
3232 segment_factory_(config) {
3333 init (config, cred);
3434}
@@ -38,7 +38,7 @@ TracerImpl::TracerImpl(
3838 AsyncClientPtr<TracerRequestType, TracerResponseType> reporter_client)
3939 : config_(config),
4040 reporter_client_(std::move(reporter_client)),
41- grpc_callback_thread_ ([this ] { this ->run (); }),
41+ evloop_thread_ ([this ] { this ->run (); }),
4242 segment_factory_(config) {
4343 init (config, nullptr );
4444}
@@ -47,11 +47,7 @@ TracerImpl::~TracerImpl() {
4747 reporter_client_.reset ();
4848 cds_client_.reset ();
4949 cq_.Shutdown ();
50- grpc_callback_thread_.join ();
51-
52- if (cds_thread_.joinable ()) {
53- cds_thread_.join ();
54- }
50+ evloop_thread_.join ();
5551}
5652
5753TracingContextPtr TracerImpl::newContext () { return segment_factory_.create (); }
@@ -80,23 +76,28 @@ void TracerImpl::run() {
8076 void * got_tag;
8177 bool ok = false ;
8278 while (true ) {
83- grpc::CompletionQueue::NextStatus status =
84- cq_.AsyncNext (&got_tag, &ok, gpr_inf_future (GPR_CLOCK_REALTIME));
85- if (status == grpc::CompletionQueue::SHUTDOWN) {
86- return ;
79+ // TODO(shikugawa): cleanup evloop handler.
80+ if (cds_timer_ != nullptr && cds_timer_->check ()) {
81+ cdsRequest ();
82+ }
83+
84+ grpc::CompletionQueue::NextStatus status = cq_.AsyncNext (
85+ &got_tag, &ok, gpr_time_from_nanos (0 , GPR_CLOCK_REALTIME));
86+ switch (status) {
87+ case grpc::CompletionQueue::TIMEOUT:
88+ continue ;
89+ case grpc::CompletionQueue::SHUTDOWN:
90+ return ;
8791 }
8892 static_cast <StreamCallbackTag*>(got_tag)->callback (!ok);
8993 }
9094}
9195
92- void TracerImpl::startCds (std::chrono::seconds seconds) {
93- while (true ) {
94- skywalking::v3::ConfigurationSyncRequest request;
95- request.set_service (config_.tracerConfig ().service_name ());
96- request.set_uuid (config_.uuid ());
97- cds_client_->sendMessage (request);
98- std::this_thread::sleep_for (seconds);
99- }
96+ void TracerImpl::cdsRequest () {
97+ skywalking::v3::ConfigurationSyncRequest request;
98+ request.set_service (config_.tracerConfig ().service_name ());
99+ request.set_uuid (config_.uuid ());
100+ cds_client_->sendMessage (request);
100101}
101102
102103void TracerImpl::init (TracerConfig& config,
@@ -124,10 +125,8 @@ void TracerImpl::init(TracerConfig& config,
124125 config.address (), cq_,
125126 std::make_unique<GrpcAsyncConfigDiscoveryServiceStreamBuilder>(config_),
126127 cred);
127- cds_thread_ = std::thread ([this ] {
128- this ->startCds (
129- std::chrono::seconds (config_.tracerConfig ().cds_request_interval ()));
130- });
128+ cds_timer_ =
129+ std::make_unique<Timer>(config_.tracerConfig ().cds_request_interval ());
131130 }
132131}
133132
0 commit comments