switchInstance returns next instance instead of random

This commit is contained in:
Or 2023-11-18 17:40:35 +02:00
parent cac79c6959
commit d9121aa5d8
2 changed files with 20 additions and 2 deletions

View File

@ -605,7 +605,7 @@ function switchInstance(url, customService) {
if (customService) { if (customService) {
const instancesList = options[options[customService].frontend] const instancesList = options[options[customService].frontend]
if (instancesList !== undefined) { if (instancesList !== undefined) {
resolve(`${utils.getRandomInstance(instancesList)}${url.pathname}${url.search}`) resolve(`${utils.getNextInstance(url.origin, instancesList)}${url.pathname}${url.search}`)
} }
} else { } else {
for (const service in config.services) { for (const service in config.services) {
@ -618,7 +618,7 @@ function switchInstance(url, customService) {
resolve() resolve()
return return
} }
resolve(`${utils.getRandomInstance(instancesList)}${url.pathname}${url.search}`) resolve(`${utils.getNextInstance(url.origin, instancesList)}${url.pathname}${url.search}`)
return return
} }
} }

View File

@ -8,6 +8,23 @@ function getRandomInstance(instances) {
return instances[~~(instances.length * Math.random())] return instances[~~(instances.length * Math.random())]
} }
/**
* @param {string} currentInstanceUrl
* @param {Array.<T>} instances
* @returns {T}
*/
function getNextInstance(currentInstanceUrl, instances) {
const currentInstanceIndex = instances.indexOf(currentInstanceUrl);
if (currentInstanceIndex === -1){
return getRandomInstance(instances);
}
const nextInstanceIndex = (currentInstanceIndex + 1) % instances.length;
return instances[nextInstanceIndex];
}
/** /**
* @param {string} str * @param {string} str
*/ */
@ -195,6 +212,7 @@ function ping(href) {
export default { export default {
getRandomInstance, getRandomInstance,
getNextInstance,
protocolHost, protocolHost,
getList, getList,
getBlacklist, getBlacklist,