From e741cfc0aa9095624b1acf9e5b2f4ddaad66a665 Mon Sep 17 00:00:00 2001 From: TheTrunk Date: Wed, 13 Dec 2023 20:49:38 +0700 Subject: [PATCH] deterministic ip selection for r sync --- src/services/haproxyTemplate.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/services/haproxyTemplate.js b/src/services/haproxyTemplate.js index 8a684a0..37aa832 100644 --- a/src/services/haproxyTemplate.js +++ b/src/services/haproxyTemplate.js @@ -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') { @@ -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'; } }