From b146a84f7fe1f680441b54399b9b023024d72b40 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Tue, 28 Sep 2021 15:38:42 +0200 Subject: [PATCH] dnsdist: Better handling of outgoing DoH workers This commit raises the number of DoH workers to be at least 1, always, unless told otherwise via setOutgoingDoHWorkerThreads(0). In that last case it raises an exception if the console is used to declare a new DoH backend later on. --- pdns/dnsdist-lua.cc | 7 ++++++- pdns/dnsdistdist/dnsdist-nghttp2.cc | 14 ++++++++++---- pdns/dnsdistdist/dnsdist-nghttp2.hh | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index c5daacf693..2aaa8d3df9 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -536,7 +536,12 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) if (ret->d_tlsCtx) { setupDoHClientProtocolNegotiation(ret->d_tlsCtx); } - if (g_outgoingDoHWorkerThreads == 0) { + + if (g_configurationDone && g_outgoingDoHWorkerThreads && *g_outgoingDoHWorkerThreads == 0) { + throw std::runtime_error("Error: setOutgoingDoHWorkerThreads() is set to 0 so no outgoing DoH worker thread is available to serve queries"); + } + + if (!g_outgoingDoHWorkerThreads || *g_outgoingDoHWorkerThreads == 0) { g_outgoingDoHWorkerThreads = 1; } diff --git a/pdns/dnsdistdist/dnsdist-nghttp2.cc b/pdns/dnsdistdist/dnsdist-nghttp2.cc index abebdec1b4..0f1e78d8fa 100644 --- a/pdns/dnsdistdist/dnsdist-nghttp2.cc +++ b/pdns/dnsdistdist/dnsdist-nghttp2.cc @@ -40,7 +40,7 @@ std::atomic g_dohStatesDumpRequested{0}; std::unique_ptr g_dohClientThreads{nullptr}; -uint16_t g_outgoingDoHWorkerThreads{0}; +std::optional g_outgoingDoHWorkerThreads{std::nullopt}; #ifdef HAVE_NGHTTP2 class DoHConnectionToBackend : public TCPConnectionToBackend @@ -1229,9 +1229,15 @@ void DoHClientCollection::addThread() bool initDoHWorkers() { #ifdef HAVE_NGHTTP2 - if (g_outgoingDoHWorkerThreads > 0) { - g_dohClientThreads = std::make_unique(g_outgoingDoHWorkerThreads); - for (size_t idx = 0; idx < g_outgoingDoHWorkerThreads; idx++) { + if (!g_outgoingDoHWorkerThreads) { + /* Unless the value has been set to 0 explicitly, always start at least one outgoing DoH worker thread, in case a DoH backend + is added at a later time. */ + g_outgoingDoHWorkerThreads = 1; + } + + if (g_outgoingDoHWorkerThreads && *g_outgoingDoHWorkerThreads > 0) { + g_dohClientThreads = std::make_unique(*g_outgoingDoHWorkerThreads); + for (size_t idx = 0; idx < *g_outgoingDoHWorkerThreads; idx++) { g_dohClientThreads->addThread(); } } diff --git a/pdns/dnsdistdist/dnsdist-nghttp2.hh b/pdns/dnsdistdist/dnsdist-nghttp2.hh index f57e4e46fa..a44930f4ca 100644 --- a/pdns/dnsdistdist/dnsdist-nghttp2.hh +++ b/pdns/dnsdistdist/dnsdist-nghttp2.hh @@ -57,7 +57,7 @@ private: extern std::unique_ptr g_dohClientThreads; extern std::atomic g_dohStatesDumpRequested; -extern uint16_t g_outgoingDoHWorkerThreads; +extern std::optional g_outgoingDoHWorkerThreads; class TLSCtx; -- 2.47.2