]> granicus.if.org Git - pdns/commitdiff
move ring stuff to own file, so it can share between dnsdist-lua and dnsdist-web...
authorbert hubert <bert.hubert@netherlabs.nl>
Thu, 3 Dec 2015 19:21:16 +0000 (20:21 +0100)
committerbert hubert <bert.hubert@netherlabs.nl>
Thu, 3 Dec 2015 19:21:16 +0000 (20:21 +0100)
pdns/Makefile.am
pdns/dnsdist-lua.cc
pdns/dnsdist-rings.cc [new file with mode: 0644]
pdns/dnsdist.hh

index ab4c1252211c8ba94daca5d7edb520a7595c5a0b..118727b7823a737d67d065b19cae3d21faa338fd 100644 (file)
@@ -617,6 +617,7 @@ dnsdist_SOURCES = \
        dnsdist-carbon.cc \
        dnsdist-lua.cc \
        dnsdist-lua2.cc \
+       dnsdist-rings.cc \
        dnsdist-tcp.cc \
        dnsdist-web.cc \
        dnslabeltext.cc \
index 0b99313424547f7122bccd0768587cec35b5eec2..a5ff50334989f9abde2367fad7774bfc29709771 100644 (file)
@@ -653,7 +653,7 @@ vector<std::function<void(void)>> setupLua(bool client, const std::string& confi
       }
     });
 
-  // something needs to be done about this, unlocked will 'mostly' work
+
   g_lua.writeFunction("topClients", [](unsigned int top) {
       map<ComboAddress, int,ComboAddress::addressOnlyLessThan > counts;
       unsigned int total=0;
diff --git a/pdns/dnsdist-rings.cc b/pdns/dnsdist-rings.cc
new file mode 100644 (file)
index 0000000..828935a
--- /dev/null
@@ -0,0 +1,38 @@
+#include "dnsdist.hh"
+#include "lock.hh"
+
+unsigned int Rings::numDistinctRequestors()
+{
+  std::set<ComboAddress, ComboAddress::addressOnlyLessThan> s;
+  WriteLock wl(&queryLock);
+  for(const auto& q : queryRing)
+    s.insert(q.requestor);
+  return s.size();
+}
+
+vector<pair<unsigned int,ComboAddress> > Rings::getTopBandwidth(int numentries)
+{
+  map<ComboAddress, unsigned int, ComboAddress::addressOnlyLessThan> counts;
+  {
+    WriteLock wl(&queryLock);
+    for(const auto& q : queryRing)
+      counts[q.requestor]+=q.size;
+  }
+
+  {
+    std::lock_guard<std::mutex> lock(respMutex);
+    for(const auto& r : respRing)
+      counts[r.requestor]+=r.size;
+  }
+
+  typedef vector<pair<unsigned int, ComboAddress>> ret_t;
+  ret_t ret;
+  for(const auto& p : counts)
+    ret.push_back({p.second, p.first});
+  partial_sort(ret.begin(), ret.begin()+numentries, ret.end(), [](const ret_t::value_type&a, const ret_t::value_type&b)
+              {
+                return(b.second < a.second);
+              });
+  ret.resize(numentries);
+  return ret;
+}
index 54e56e64fc994c0853213a2a3ff47c8268e8f8eb..edc9d15f0bcf442f0e07c18c103e37c751ee2cd6 100644 (file)
@@ -194,6 +194,7 @@ struct Rings {
     struct timespec when;
     ComboAddress requestor;
     DNSName name;
+    uint16_t size;
     uint16_t qtype;
   };
   boost::circular_buffer<Query> queryRing;
@@ -210,6 +211,9 @@ struct Rings {
   boost::circular_buffer<Response> respRing;
   std::mutex respMutex;
   pthread_rwlock_t queryLock;
+
+  vector<pair<unsigned int, ComboAddress> > getTopBandwidth(int numentries);
+  unsigned int numDistinctRequestors();
 };
 
 extern Rings g_rings;