Merge pull request #857 from orfins/feat/switch-to-next-instead-of-random

switchInstance returns next instance instead of random
This commit is contained in:
ManeraKai 2023-12-08 20:29:16 +00:00 committed by GitHub
commit 5a105da61e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -618,7 +618,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) {
@ -631,7 +631,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
*/ */
@ -197,6 +214,7 @@ function ping(href) {
export default { export default {
getRandomInstance, getRandomInstance,
getNextInstance,
protocolHost, protocolHost,
getList, getList,
getBlacklist, getBlacklist,