From 5892fa2f9f5ef80e2c3593f2a20818a611e34534 Mon Sep 17 00:00:00 2001 From: Omar Mohamed Khallaf <51155980+OmarMohamedKhallaf@users.noreply.github.com> Date: Wed, 27 Apr 2022 10:46:27 +0200 Subject: [PATCH] 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. --- lib/src/ConfigLoader.cc | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/src/ConfigLoader.cc b/lib/src/ConfigLoader.cc index 5e05b313..3c516863 100644 --- a/lib/src/ConfigLoader.cc +++ b/lib/src/ConfigLoader.cc @@ -554,30 +554,33 @@ static void loadRedisClients(const Json::Value &redisClients) { if (!redisClients) return; + std::promise 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(); - 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); + 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()) + { + 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); } }