From: Remi Gacogne Date: Wed, 3 Aug 2016 09:07:10 +0000 (+0200) Subject: dnsdist: Add unreachable servers, connect when they are up X-Git-Tag: rec-4.1.0-alpha1~334^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7565f4e613a68ab25085203ac0daae1676b1efdc;p=thirdparty%2Fpdns.git dnsdist: Add unreachable servers, connect when they are up --- diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index 8e122046cf..53e5b43e25 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -226,13 +226,15 @@ vector> setupLua(bool client, const std::string& confi addServerToPool(localPools, "", ret); g_pools.setState(localPools); - if(g_launchWork) { - g_launchWork->push_back([ret]() { - ret->tid = move(thread(responderThread, ret)); + if (ret->connected) { + if(g_launchWork) { + g_launchWork->push_back([ret]() { + ret->tid = move(thread(responderThread, ret)); }); - } - else { - ret->tid = move(thread(responderThread, ret)); + } + else { + ret->tid = move(thread(responderThread, ret)); + } } return ret; @@ -372,13 +374,15 @@ vector> setupLua(bool client, const std::string& confi ret->maxCheckFailures=std::stoi(boost::get(vars["maxCheckFailures"])); } - if(g_launchWork) { - g_launchWork->push_back([ret]() { + if (ret->connected) { + if(g_launchWork) { + g_launchWork->push_back([ret]() { ret->tid = move(thread(responderThread, ret)); }); - } - else { - ret->tid = move(thread(responderThread, ret)); + } + else { + ret->tid = move(thread(responderThread, ret)); + } } auto states = g_dstates.getCopy(); diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index dbefaa8599..150028ce07 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -472,7 +472,13 @@ DownstreamState::DownstreamState(const ComboAddress& remote_, const ComboAddress SSetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 1); SBind(fd, sourceAddr); } - SConnect(fd, remote); + try { + SConnect(fd, remote); + connected = true; + } + catch(const std::runtime_error& error) { + infolog("Error connecting to new server with address %s: %s", remote.toStringWithPort(), error.what()); + } idStates.resize(g_maxOutstanding); sw.start(); infolog("Added downstream server %s", remote.toStringWithPort()); @@ -1271,6 +1277,18 @@ void* healthChecksThread() if(newState != dss->upStatus) { warnlog("Marking downstream %s as '%s'", dss->getNameWithAddr(), newState ? "up" : "down"); + + if (newState && !dss->connected) { + try { + SConnect(dss->fd, dss->remote); + dss->connected = true; + dss->tid = move(thread(responderThread, dss)); + } + catch(const std::runtime_error& error) { + infolog("Error connecting to new server with address %s: %s", dss->remote.toStringWithPort(), error.what()); + } + } + dss->upStatus = newState; dss->currentCheckFailures = 0; } @@ -1871,7 +1889,9 @@ try for(const auto& address : g_cmdLine.remotes) { auto ret=std::make_shared(ComboAddress(address, 53)); addServerToPool(localPools, "", ret); - ret->tid = move(thread(responderThread, ret)); + if (ret->connected) { + ret->tid = move(thread(responderThread, ret)); + } g_dstates.modify([ret](servers_t& servers) { servers.push_back(ret); }); } } diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index 6e9538c1f5..8398c140a0 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -356,6 +356,7 @@ struct DownstreamState bool upStatus{false}; bool useECS{false}; bool setCD{false}; + std::atomic connected{false}; bool isUp() const { if(availability == Availability::Down)