fix bug when resolving redis server hostname (#1229)

Calling `drogon::app().createRedisClient` from the resolver callback
function results in a failure because of `assert(!running_);` in
`HttpAppFrameworkImpl::createRedisClient`.

This fix uses a promise whose value is set in the callback and the
client creation is done outside the callback.
This commit is contained in:
Omar Mohamed Khallaf 2022-04-27 10:46:27 +02:00 committed by GitHub
parent c9c2675ba9
commit 5892fa2f9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -554,30 +554,33 @@ static void loadRedisClients(const Json::Value &redisClients)
{ {
if (!redisClients) if (!redisClients)
return; return;
std::promise<std::string> promise;
auto future = promise.get_future();
for (auto const &client : redisClients) for (auto const &client : redisClients)
{ {
auto host = client.get("host", "127.0.0.1").asString(); auto host = client.get("host", "127.0.0.1").asString();
trantor::Resolver::newResolver()->resolve( trantor::Resolver::newResolver()->resolve(
host, [client](const trantor::InetAddress &address) { host, [&promise](const trantor::InetAddress &address) {
auto hostIp = address.toIp(); promise.set_value(address.toIp());
auto port = client.get("port", 6379).asUInt();
auto password = client.get("passwd", "").asString();
if (password.empty())
{
password = client.get("password", "").asString();
}
auto connNum = client.get("connection_number", 1).asUInt();
if (connNum == 1)
{
connNum = client.get("number_of_connections", 1).asUInt();
}
auto name = client.get("name", "default").asString();
auto isFast = client.get("is_fast", false).asBool();
auto timeout = client.get("timeout", -1.0).asDouble();
auto db = client.get("db", 0).asUInt();
drogon::app().createRedisClient(
hostIp, port, name, password, connNum, isFast, timeout, db);
}); });
auto port = client.get("port", 6379).asUInt();
auto password = client.get("passwd", "").asString();
if (password.empty())
{
password = client.get("password", "").asString();
}
auto connNum = client.get("connection_number", 1).asUInt();
if (connNum == 1)
{
connNum = client.get("number_of_connections", 1).asUInt();
}
auto name = client.get("name", "default").asString();
auto isFast = client.get("is_fast", false).asBool();
auto timeout = client.get("timeout", -1.0).asDouble();
auto db = client.get("db", 0).asUInt();
auto hostIp = future.get();
drogon::app().createRedisClient(
hostIp, port, name, password, connNum, isFast, timeout, db);
} }
} }