From: Jeremy Clerc Date: Fri, 22 Nov 2019 07:31:16 +0000 (+0100) Subject: geoipbackend: stop looking after first weighted match X-Git-Tag: rec-4.4.0-beta1~37^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2f2e58af3d6246276056376de0809c1b3fb100e0;p=thirdparty%2Fpdns.git geoipbackend: stop looking after first weighted match 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. --- diff --git a/modules/geoipbackend/geoipbackend.cc b/modules/geoipbackend/geoipbackend.cc index b56d16f5d9..de4fc00286 100644 --- a/modules/geoipbackend/geoipbackend.cc +++ b/modules/geoipbackend/geoipbackend.cc @@ -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 cumul_probabilities; + map 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) {