Redis: disconnect cleanly if INFO request fails

This commit is contained in:
Tim Wojtulewicz 2025-05-30 07:48:43 -07:00
parent 0d18ce4e13
commit 9f12208f57
2 changed files with 19 additions and 8 deletions

View file

@ -124,14 +124,14 @@ void redisGeneric(redisAsyncContext* ctx, void* reply, void* privdata) {
}
/**
* Callback handler for ZADD commands.
* Callback handler for INFO commands.
*
* @param ctx The async context that called this callback.
* @param reply The reply from the server for the command.
* @param privdata A pointer to private data passed in the command.
*/
void redisINFO(redisAsyncContext* ctx, void* reply, void* privdata) {
auto t = Tracer("generic");
auto t = Tracer("info");
auto backend = static_cast<zeek::storage::backend::redis::Redis*>(ctx->data);
backend->HandleInfoResult(static_cast<redisReply*>(reply));
}
@ -630,9 +630,14 @@ void Redis::HandleInfoResult(redisReply* reply) {
}
}
if ( ! connected && res.err_str.empty() )
if ( ! connected ) {
if ( res.err_str.empty() )
res.err_str = "INFO command did not return server version";
disconnect_reason = res.err_str;
redisAsyncDisconnect(async_ctx);
}
freeReplyObject(reply);
CompleteCallback(open_cb, res);
}
@ -678,7 +683,12 @@ void Redis::OnDisconnect(int status) {
else {
--active_ops;
if ( disconnect_reason.empty() )
EnqueueBackendLost("Client disconnected");
else
EnqueueBackendLost(util::fmt("Client disconnected: %s", disconnect_reason.c_str()));
if ( close_cb )
CompleteCallback(close_cb, {ReturnCode::SUCCESS});
}

View file

@ -70,12 +70,13 @@ private:
// poll.
std::deque<redisReply*> reply_queue;
OpenResultCallback* open_cb;
ResultCallback* close_cb;
OpenResultCallback* open_cb = nullptr;
ResultCallback* close_cb = nullptr;
std::mutex expire_mutex;
std::string server_addr;
std::string key_prefix;
std::string disconnect_reason;
std::atomic<bool> connected = false;
std::atomic<bool> expire_running = false;