@@ -136,6 +136,18 @@ absl::Status AcsAgentClient::SendMessage(MessageBody message_body) {
136
136
return AddRequest (request);
137
137
}
138
138
139
+ bool AcsAgentClient::IsDead () {
140
+ // If the stream_state_ is kStreamFailedToInitialize or kShutdown, the client
141
+ // is dead, no point to retry sending messages. If the stream_state_ is
142
+ // kStreamClosed, the restart_client_thread_ will restart the client soon, the
143
+ // caller can retry sending messages later with a backoff mechanism. If the
144
+ // stream_state_ is kStreamNotInitialized, the client is not dead yet, just
145
+ // waiting for the successful registration.
146
+ absl::MutexLock lock (&reactor_mtx_);
147
+ return stream_state_ == ClientState::kStreamFailedToInitialize ||
148
+ stream_state_ == ClientState::kShutdown ;
149
+ }
150
+
139
151
absl::Status AcsAgentClient::Init () {
140
152
// Register the connection. The registration request should only be sent once.
141
153
std::unique_ptr<Request> registration_request =
@@ -169,7 +181,7 @@ absl::Status AcsAgentClient::AddRequestAndWaitForResponse(
169
181
absl::MutexLock lock (&reactor_mtx_);
170
182
if (request.has_register_connection () &&
171
183
(stream_state_ != ClientState::kStreamNotInitialized &&
172
- stream_state_ != ClientState::kStreamClosed )) {
184
+ stream_state_ != ClientState::kStreamTemporarilyDown )) {
173
185
return absl::InternalError (
174
186
" The stream is not in the correct state to accept new registration "
175
187
" request." );
@@ -272,7 +284,7 @@ void AcsAgentClient::ReactorReadCallback(Response response, bool ok) {
272
284
ABSL_LOG (WARNING) << " ReactorReadCallback not ok" ;
273
285
// Wakes up RestartReactor() to restart the stream.
274
286
absl::MutexLock lock (&reactor_mtx_);
275
- stream_state_ = ClientState::kStreamClosed ;
287
+ stream_state_ = ClientState::kStreamTemporarilyDown ;
276
288
return ;
277
289
}
278
290
// Wake up ClientReadMessage().
@@ -346,7 +358,7 @@ void AcsAgentClient::RestartClient() {
346
358
while (true ) {
347
359
reactor_mtx_.LockWhen (absl::Condition (
348
360
+[](ClientState* stream_state) {
349
- return *stream_state == ClientState::kStreamClosed ||
361
+ return *stream_state == ClientState::kStreamTemporarilyDown ||
350
362
*stream_state == ClientState::kShutdown ;
351
363
},
352
364
&stream_state_));
@@ -369,7 +381,7 @@ void AcsAgentClient::RestartClient() {
369
381
}
370
382
std::unique_ptr<AcsStub> stub = GenerateConnectionIdAndStub ();
371
383
if (stub == nullptr ) {
372
- stream_state_ = ClientState::kStreamFailedToRestart ;
384
+ stream_state_ = ClientState::kStreamFailedToInitialize ;
373
385
reactor_mtx_.Unlock ();
374
386
return ;
375
387
}
@@ -378,7 +390,7 @@ void AcsAgentClient::RestartClient() {
378
390
absl::bind_front (&AcsAgentClient::ReactorReadCallback, this ),
379
391
connection_id_);
380
392
if (reactor_ == nullptr ) {
381
- stream_state_ = ClientState::kStreamFailedToRestart ;
393
+ stream_state_ = ClientState::kStreamFailedToInitialize ;
382
394
reactor_mtx_.Unlock ();
383
395
ABSL_LOG (WARNING) << " Failed to generate connection id and reactor." ;
384
396
return ;
@@ -387,7 +399,7 @@ void AcsAgentClient::RestartClient() {
387
399
// Initialize the client.
388
400
absl::Status init_status = Init ();
389
401
if (!init_status.ok ()) {
390
- stream_state_ = ClientState::kStreamFailedToRestart ;
402
+ stream_state_ = ClientState::kStreamFailedToInitialize ;
391
403
reactor_mtx_.Unlock ();
392
404
return ;
393
405
}
0 commit comments