Skip to content

Commit

Permalink
deterministic ip selection for r sync
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTrunk committed Dec 13, 2023
1 parent d29790c commit e741cfc
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/services/haproxyTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ ${forbiddenBackend}
return config;
}

function selectIPforR(ips) {
// choose the ip address whose sum of digits is the lowest
let chosenIp = ips[0];
let chosenIpSum = ips[0].split(':')[0].split('.').reduce((a, b) => parseInt(a, 10) + parseInt(b, 10), 0);
for (const ip of ips) {
const sum = ip.split(':')[0].split('.').reduce((a, b) => parseInt(a, 10) + parseInt(b, 10), 0);
if (sum < chosenIpSum) {
chosenIp = ip;
chosenIpSum = sum;
}
}
return chosenIp;
}

function generateDomainBackend(app, mode) {
let domainUsed = app.domain.split('.').join('');
if (mode === 'tcp') {
Expand Down Expand Up @@ -231,7 +245,7 @@ backend ${domainUsed}backend
domainBackend += ' backup';
}
} else { // set new IP as main
mapOfNamesIps[app.name] = ip;
mapOfNamesIps[app.name] = selectIPforR(app.ips);
domainBackend += ' inter 10s fall 3 rise 99999999';
}
}
Expand Down

0 comments on commit e741cfc

Please sign in to comment.