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,12 +554,15 @@ static void loadRedisClients(const Json::Value &redisClients)
{
if (!redisClients)
return;
std::promise<std::string> promise;
auto future = promise.get_future();
for (auto const &client : redisClients)
{
auto host = client.get("host", "127.0.0.1").asString();
trantor::Resolver::newResolver()->resolve(
host, [client](const trantor::InetAddress &address) {
auto hostIp = address.toIp();
host, [&promise](const trantor::InetAddress &address) {
promise.set_value(address.toIp());
});
auto port = client.get("port", 6379).asUInt();
auto password = client.get("passwd", "").asString();
if (password.empty())
@ -575,9 +578,9 @@ static void loadRedisClients(const Json::Value &redisClients)
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);
});
}
}