[minor] Refactor to expose RedisContext::PingPort (#12022)

This commit is contained in:
dHannasch
2020-11-23 20:39:50 -08:00
committed by GitHub
parent 40cbc638ee
commit 2c4514a2c0
2 changed files with 30 additions and 5 deletions
+23 -5
View File
@@ -284,6 +284,12 @@ void SetDisconnectCallback(RedisAsyncContext *redis_async_context) {
RedisAsyncContextDisconnectCallback);
}
void FreeRedisContext(redisContext *context) { redisFree(context); }
void FreeRedisContext(redisAsyncContext *context) {}
void FreeRedisContext(RedisAsyncContext *context) {}
template <typename RedisContext, typename RedisConnectFunction>
Status ConnectWithoutRetries(const std::string &address, int port,
const RedisConnectFunction &connect_function,
@@ -291,17 +297,23 @@ Status ConnectWithoutRetries(const std::string &address, int port,
// This currently returns the errorMessage in two different ways,
// as an output parameter and in the Status::RedisError,
// because we're not sure whether we'll want to change what this returns.
*context = connect_function(address.c_str(), port);
if (*context == nullptr || (*context)->err) {
RedisContext *newContext = connect_function(address.c_str(), port);
if (newContext == nullptr || (newContext)->err) {
std::ostringstream oss(errorMessage);
if (*context == nullptr) {
if (newContext == nullptr) {
oss << "Could not allocate Redis context.";
} else if ((*context)->err) {
} else if (newContext->err) {
oss << "Could not establish connection to Redis " << address << ":" << port
<< " (context.err = " << (*context)->err << ")";
<< " (context.err = " << newContext->err << ")";
}
return Status::RedisError(errorMessage);
}
if (context != nullptr) {
// Don't crash if the RedisContext** is null.
*context = newContext;
} else {
FreeRedisContext(newContext);
}
return Status::OK();
}
@@ -342,6 +354,12 @@ Status ConnectWithRetries(const std::string &address, int port,
return Status::OK();
}
Status RedisContext::PingPort(const std::string &address, int port) {
std::string errorMessage;
return ConnectWithoutRetries(address, port, redisConnect,
static_cast<redisContext **>(nullptr), errorMessage);
}
Status RedisContext::Connect(const std::string &address, int port, bool sharding,
const std::string &password = "") {
RAY_CHECK(!context_);
+7
View File
@@ -170,6 +170,13 @@ class RedisContext {
~RedisContext();
/// Test whether the address and port has a reachable Redis service.
///
/// \param address IP address to test.
/// \param port port number to test.
/// \return The Status that we would get if we Connected.
Status PingPort(const std::string &address, int port);
Status Connect(const std::string &address, int port, bool sharding,
const std::string &password);