@@ -41,37 +41,30 @@ GrpcAsyncSegmentReporterClient::GrpcAsyncSegmentReporterClient(
4141}
4242
4343GrpcAsyncSegmentReporterClient::~GrpcAsyncSegmentReporterClient () {
44- // It will wait until there is no drained messages.
45- // There are no timeout option to handle this, so if you would like to stop
46- // them, you should send signals like SIGTERM.
47- // If server stopped with accidental issue, the event loop handle that it
48- // failed to send message and close stream, then recreate new stream and try
49- // to do it. This process will continue forever without sending explicit
50- // signal.
51- // TODO(shikugawa): Block to wait drained messages to be clear with createing
52- // condition variable wrapper.
53- #ifndef GRPC_TEST
44+ // It will wait until there is no drained messages with 5 second timeout.
5445 if (stream_) {
5546 std::unique_lock<std::mutex> lck (mux_);
56- while (!drained_messages_.empty ()) {
57- cv_.wait (lck);
47+ while (!pending_messages_.empty ()) {
48+ cv_.wait_for (lck, std::chrono::seconds (5 ));
49+ pending_messages_.clear ();
5850 }
5951 }
60- #endif
6152
6253 resetStream ();
6354}
6455
6556void GrpcAsyncSegmentReporterClient::sendMessage (TracerRequestType message) {
57+ pending_messages_.push (message);
58+
6659 if (!stream_) {
67- drained_messages_.push (message);
6860 info (
69- " [Reporter] No active stream, inserted message into draining message "
61+ " [Reporter] No active stream, inserted message into pending message "
7062 " queue. "
7163 " pending message size: {}" ,
72- drained_messages_ .size ());
64+ pending_messages_ .size ());
7365 return ;
7466 }
67+
7568 stream_->sendMessage (message);
7669}
7770
@@ -80,19 +73,6 @@ void GrpcAsyncSegmentReporterClient::startStream() {
8073
8174 stream_ = factory_->create (*this , cv_);
8275 info (" [Reporter] Stream {} had created." , fmt::ptr (stream_.get ()));
83-
84- const auto drained_messages_size = drained_messages_.size ();
85-
86- while (!drained_messages_.empty ()) {
87- auto msg = drained_messages_.front ();
88- drained_messages_.pop ();
89- if (msg.has_value ()) {
90- stream_->sendMessage (msg.value ());
91- }
92- }
93-
94- info (" [Reporter] {} drained messages inserted into pending messages." ,
95- drained_messages_size);
9676}
9777
9878void GrpcAsyncSegmentReporterClient::resetStream () {
@@ -121,28 +101,15 @@ GrpcAsyncSegmentReporterStream::GrpcAsyncSegmentReporterStream(
121101 request_writer_->StartCall (reinterpret_cast <void *>(&ready_));
122102}
123103
124- GrpcAsyncSegmentReporterStream::~GrpcAsyncSegmentReporterStream () {
125- const auto pending_messages_size = pending_messages_.size ();
126- while (!pending_messages_.empty ()) {
127- auto msg = pending_messages_.front ();
128- pending_messages_.pop ();
129- if (msg.has_value ()) {
130- client_.drainPendingMessage (msg.value ());
131- }
132- }
133- info (" [Reporter] {} pending messages drained." , pending_messages_size);
134- }
135-
136104void GrpcAsyncSegmentReporterStream::sendMessage (TracerRequestType message) {
137- pending_messages_.push (message);
138105 clearPendingMessage ();
139106}
140107
141108bool GrpcAsyncSegmentReporterStream::clearPendingMessage () {
142- if (state_ != StreamState::Idle || pending_messages_ .empty ()) {
109+ if (state_ != StreamState::Idle || client_. pendingMessages () .empty ()) {
143110 return false ;
144111 }
145- auto message = pending_messages_ .front ();
112+ auto message = client_. pendingMessages () .front ();
146113 if (!message.has_value ()) {
147114 return false ;
148115 }
@@ -164,21 +131,19 @@ void GrpcAsyncSegmentReporterStream::onIdle() {
164131
165132 // Release pending messages which are inserted when stream is not ready
166133 // to write.
167- clearPendingMessage ();
168-
169- if (pending_messages_.empty ()) {
134+ if (!clearPendingMessage ()) {
170135 cv_.notify_all ();
171136 }
172137}
173138
174139void GrpcAsyncSegmentReporterStream::onWriteDone () {
175140 info (" [Reporter] Write finished" );
176141
177- // Enqueue message after sending message finished.
142+ // Dequeue message after sending message finished.
178143 // With this, messages which failed to sent never lost even if connection
179144 // was closed. because pending messages with messages which failed to send
180145 // will drained and resend another stream.
181- pending_messages_ .pop ();
146+ client_. pendingMessages () .pop ();
182147 state_ = StreamState::Idle;
183148
184149 onIdle ();
0 commit comments