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:
commit
5a105da61e
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
|
Loading…
Reference in New Issue