Skip to content

Commit fc34087

Browse files
testillanoEduardo Ramos Testillano (eramedu)
authored andcommitted
fix: reconnect on demand when connection is down at send time
When async_send() finds the connection down, it calls reconnect() but then unconditionally marks the request as unsent (-1) without checking if reconnect succeeded. This causes requests to be lost even when the server is already available. Now after reconnect(), check isConnected() and fall through to send if the connection was restored. Also change try_to_lock to a timed lock (2.5s) so concurrent threads wait for an ongoing reconnect instead of giving up immediately.
1 parent 63929ec commit fc34087

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

src/Http2Client.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,17 @@ void Http2Client::enableMetrics(ert::metrics::Metrics *metrics,
124124

125125
void Http2Client::reconnect()
126126
{
127-
std::unique_lock<std::shared_timed_mutex> lock(mutex_, std::try_to_lock);
127+
std::unique_lock<std::shared_timed_mutex> lock(mutex_, std::chrono::milliseconds(2500));
128128
if (!lock.owns_lock())
129129
{
130130
return;
131131
}
132132

133+
if (connection_ && connection_->isConnected())
134+
{
135+
return; // another thread already reconnected
136+
}
137+
133138
connection_->reconnect();
134139
}
135140

@@ -153,15 +158,22 @@ void Http2Client::async_send(
153158

154159
reconnect();
155160

156-
// metrics
157-
if (metrics_) {
158-
auto& counter = observed_requests_unsents_counter_family_ptr_->Add({{"method", method}});
159-
counter.Increment();
160-
}
161+
if (!connection_ || !connection_->isConnected())
162+
{
163+
// metrics
164+
if (metrics_) {
165+
auto& counter = observed_requests_unsents_counter_family_ptr_->Add({{"method", method}});
166+
counter.Increment();
167+
}
161168

162-
// Invoke callback
163-
cb(Http2Client::response{"", -1});
164-
return;
169+
// Invoke callback
170+
cb(Http2Client::response{"", -1});
171+
return;
172+
}
173+
// Reconnect succeeded: fall through to send
174+
LOGINFORMATIONAL(
175+
ert::tracing::Logger::informational("Reconnection succeeded, proceeding with request", ERT_FILE_LOCATION);
176+
);
165177
}
166178

167179
// Ignore Body on GET, DELETE and HEAD:

0 commit comments

Comments
 (0)