]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
geoipbackend: stop looking after first weighted match 8565/head
authorJeremy Clerc <j.clerc@criteo.com>
Fri, 22 Nov 2019 07:31:16 +0000 (08:31 +0100)
committerJeremy Clerc <j.clerc@criteo.com>
Fri, 22 Nov 2019 07:31:16 +0000 (08:31 +0100)
In the case of weighted round robin, we will give back only one
GeoIPDNSResourceRecord, so once we found it, there is no point to
keep looking except inducing a performance penalty.

This commit ensures we stop looking after we found our first weighted
match.

modules/geoipbackend/geoipbackend.cc

index b56d16f5d9279827e9b013beecef1d4632dd4de0..de4fc00286c32ce1a38dcbc137f95b581b8fe232 100644 (file)
@@ -314,11 +314,13 @@ GeoIPBackend::~GeoIPBackend() {
 bool GeoIPBackend::lookup_static(const GeoIPDomain &dom, const DNSName &search, const QType &qtype, const DNSName& qdomain, const Netmask& addr, GeoIPNetmask &gl) {
   const auto& i = dom.records.find(search);
   map<uint16_t,int> cumul_probabilities;
+  map<uint16_t,bool> weighted_match;
   int probability_rnd = 1+(dns_random(1000)); // setting probability=0 means it never is used
 
   if (i != dom.records.end()) { // return static value
     for(const auto& rr : i->second) {
-      if (qtype != QType::ANY && rr.qtype != qtype) continue;
+      if ((qtype != QType::ANY && rr.qtype != qtype) || weighted_match[rr.qtype.getCode()])
+        continue;
 
       if (rr.has_weight) {
         gl.netmask = (addr.isIpv6()?128:32);
@@ -332,6 +334,10 @@ bool GeoIPBackend::lookup_static(const GeoIPDomain &dom, const DNSName &search,
       d_result.push_back(rr);
       d_result.back().content = content;
       d_result.back().qname = qdomain;
+      // If we are weighted we only return one resource and we found a matching resource,
+      // so no need to check the other ones.
+      if (rr.has_weight)
+        weighted_match[rr.qtype.getCode()] = true;
     }
     // ensure we get most strict netmask
     for(DNSResourceRecord& rr: d_result) {