Skip to content

Instantly share code, notes, and snippets.

@RoyBellingan
Last active September 14, 2015 20:42
Show Gist options
  • Save RoyBellingan/dddf713b28936f735910 to your computer and use it in GitHub Desktop.
Save RoyBellingan/dddf713b28936f735910 to your computer and use it in GitHub Desktop.
Pdns mod for poor man load balancing
diff --git a/pdns/backends/gsql/gsqlbackend.cc b/pdns/backends/gsql/gsqlbackend.cc
index 9337e53..179b6ef 100644
--- a/pdns/backends/gsql/gsqlbackend.cc
+++ b/pdns/backends/gsql/gsqlbackend.cc
@@ -1605,6 +1605,8 @@ void GSQLBackend::extractRecord(const SSqlStatement::row_t& row, DNSResourceReco
r.disabled = !row[5].empty() && row[5][0]=='1';
r.domain_id=atoi(row[4].c_str());
+
+ r.prio = atoi(row[2].c_str());
}
void GSQLBackend::extractComment(const SSqlStatement::row_t& row, Comment& comment)
diff --git a/pdns/dns.hh b/pdns/dns.hh
index 0821d05..4b6669d 100644
--- a/pdns/dns.hh
+++ b/pdns/dns.hh
@@ -91,6 +91,7 @@ public:
time_t last_modified; //!< For autocalculating SOA serial numbers - the backend needs to fill this in
uint32_t ttl; //!< Time To Live of this record
+ float prio; //!< Priority for this record
uint32_t signttl; //!< If non-zero, use this TTL as original TTL in the RRSIG
int domain_id; //!< If a backend implements this, the domain_id of the zone this record is in
diff --git a/pdns/packethandler.cc b/pdns/packethandler.cc
old mode 100644
new mode 100755
index b84c48a..279cbff
--- a/pdns/packethandler.cc
+++ b/pdns/packethandler.cc
@@ -1346,13 +1347,41 @@ DNSPacket *PacketHandler::questionOrRecurse(DNSPacket *p, bool *shouldRecurse)
}
else if(weDone) {
bool haveRecords = false;
- for(const auto& rr: rrset) {
- if((p->qtype.getCode() == QType::ANY || rr.qtype == p->qtype) && rr.qtype.getCode() && rr.auth) {
- r->addRecord(rr);
- haveRecords = true;
- }
+ bool priority_used = false;
+ DNSResourceRecord const *proposed_record;
+
+ if(p->qtype.getCode() == QType::A || p->qtype.getCode() == QType::AAAA){ //if we are looking for an A or AAAA record
+ double old_priority = 9999;
+ double current_priority = 0;
+ for(const auto& rr: rrset) {//filter out the other results, and apply he weight
+ if(rr.qtype == p->qtype && rr.qtype.getCode() && rr.auth) {
+ if(rr.prio > 0){
+ current_priority = -log(((double)(rand() % 1000000))/1000000) / rr.prio; //the smaller the higher
+ if(current_priority < old_priority){
+ haveRecords = true;
+ priority_used = true;
+ proposed_record = &rr;
+ old_priority = current_priority;
+ }
+ }else{//IF no priority is used (NULL or 0) than use the standard logic (add all the row)
+ r->addRecord(rr);
+ haveRecords = true;
+ }
+ }
+ }
+ if(priority_used){
+ r->addRecord(*proposed_record);
+ }
+ }else{
+ for(const auto& rr: rrset) {
+ if((p->qtype.getCode() == QType::ANY || rr.qtype == p->qtype) && rr.qtype.getCode() && rr.auth) {
+ r->addRecord(rr);
+ haveRecords = true;
+ }
+ }
}
+
if (haveRecords) {
if(p->qtype.getCode() == QType::ANY)
completeANYRecords(p, r, sd, target);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment