]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/dnsdist.hh
dnsdist: Add TCP management options from rfc7766 section 10
[thirdparty/pdns.git] / pdns / dnsdist.hh
index a8d877fbab9b66fbdcec012a4082cc5438bab2f5..eeb2a6d7d3e45ab049507c4a9e28f7e9fb282b59 100644 (file)
@@ -363,23 +363,40 @@ struct ClientState
 
 class TCPClientCollection {
   std::vector<int> d_tcpclientthreads;
+  std::atomic<uint64_t> d_numthreads{0};
   std::atomic<uint64_t> d_pos{0};
-public:
-  std::atomic<uint64_t> d_queued{0}, d_numthreads{0};
+  std::atomic<uint64_t> d_queued{0};
   uint64_t d_maxthreads{0};
+  std::mutex d_mutex;
+public:
 
   TCPClientCollection(size_t maxThreads)
   {
     d_maxthreads = maxThreads;
     d_tcpclientthreads.reserve(maxThreads);
   }
-
   int getThread()
   {
     uint64_t pos = d_pos++;
     ++d_queued;
     return d_tcpclientthreads[pos % d_numthreads];
   }
+  bool hasReachedMaxThreads() const
+  {
+    return d_numthreads >= d_maxthreads;
+  }
+  uint64_t getThreadsCount() const
+  {
+    return d_numthreads;
+  }
+  uint64_t getQueuedCount() const
+  {
+    return d_queued;
+  }
+  void decrementQueuedCount()
+  {
+    --d_queued;
+  }
   void addTCPClientThread();
 };
 
@@ -569,100 +586,6 @@ enum ednsHeaderFlags {
   EDNS_HEADER_FLAG_DO = 32768
 };
 
-/* Quest in life: serve as a rapid block list. If you add a DNSName to a root SuffixMatchNode, 
-   anything part of that domain will return 'true' in check */
-template<typename T>
-struct SuffixMatchTree
-{
-  SuffixMatchTree(const std::string& name_="", bool endNode_=false) : name(name_), endNode(endNode_)
-  {}
-
-  SuffixMatchTree(const SuffixMatchTree& rhs)
-  {
-    name = rhs.name;
-    d_human = rhs.d_human;
-    children = rhs.children;
-    endNode = rhs.endNode;
-    d_value = rhs.d_value;
-  }
-  std::string name;
-  std::string d_human;
-  mutable std::set<SuffixMatchTree> children;
-  mutable bool endNode;
-  mutable T d_value;
-  bool operator<(const SuffixMatchTree& rhs) const
-  {
-    return strcasecmp(name.c_str(), rhs.name.c_str()) < 0;
-  }
-  typedef SuffixMatchTree value_type;
-
-  template<typename V>
-  void visit(const V& v) const {
-    for(const auto& c : children) 
-      c.visit(v);
-    if(endNode)
-      v(*this);
-  }
-
-  void add(const DNSName& name, const T& t) 
-  {
-    add(name.getRawLabels(), t);
-  }
-
-  void add(std::vector<std::string> labels, const T& value) const
-  {
-    if(labels.empty()) { // this allows insertion of the root
-      endNode=true;
-      d_value=value;
-    }
-    else if(labels.size()==1) {
-      SuffixMatchTree newChild(*labels.begin(), true);
-      newChild.d_value=value;
-      children.insert(newChild);
-    }
-    else {
-      SuffixMatchTree newnode(*labels.rbegin(), false);
-      auto res=children.insert(newnode);
-      if(!res.second) {
-        children.erase(newnode);
-        res=children.insert(newnode);
-      }
-      labels.pop_back();
-      res.first->add(labels, value);
-    }
-  }
-
-  T* lookup(const DNSName& name)  const
-  {
-    if(children.empty()) { // speed up empty set
-      if(endNode)
-        return &d_value;
-      return 0;
-    }
-    return lookup(name.getRawLabels());
-  }
-
-  T* lookup(std::vector<std::string> labels) const
-  {
-    if(labels.empty()) { // optimization
-      if(endNode)
-        return &d_value;
-      return 0;
-    }
-
-    SuffixMatchTree smn(*labels.rbegin());
-    auto child = children.find(smn);
-    if(child == children.end()) {
-      if(endNode)
-        return &d_value;
-      return 0;
-    }
-    labels.pop_back();
-    return child->lookup(labels);
-  }
-  
-};
-
 extern GlobalStateHolder<SuffixMatchTree<DynBlock>> g_dynblockSMT;
 extern DNSAction::Action g_dynBlockAction;
 
@@ -688,7 +611,11 @@ extern uint16_t g_maxOutstanding;
 extern std::atomic<bool> g_configurationDone;
 extern uint64_t g_maxTCPClientThreads;
 extern uint64_t g_maxTCPQueuedConnections;
+extern size_t g_maxTCPQueriesPerConn;
+extern size_t g_maxTCPConnectionDuration;
+extern size_t g_maxTCPConnectionsPerClient;
 extern std::atomic<uint16_t> g_cacheCleaningDelay;
+extern std::atomic<uint16_t> g_cacheCleaningPercentage;
 extern bool g_verboseHealthChecks;
 extern uint32_t g_staleCacheEntriesTTL;
 extern bool g_apiReadWrite;