padlocal-client-ts
Version:
Padlocal ts client
47 lines • 1.68 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.HostResolver = exports.Host = void 0;
class Host {
constructor(host, port, quality) {
this.host = host;
this.port = port;
this.quality = quality;
this.initialQuality = quality;
}
}
exports.Host = Host;
class HostResolver {
/**
* host is initialized with 5 quality gaps, so that each host will get three times to retry while connect error occurs.
* To against
* @param pbHostList
*/
static hostListFromPBHost(pbHostList) {
return pbHostList.map((pbHost, index) => {
return new Host(pbHost.getHost(), pbHost.getPort(), (pbHostList.length - index) * 5);
});
}
static adjustHostQuality(host, connectSuccess) {
// Increase quality while connecting successfully, or decrease.
const nextQuality = host.quality + (connectSuccess ? 2 : -2);
// limit this maximal quality against initialQuality.
// Or the quality number is too large, it will retry too many times after rotating to other host,
// while the host is down suddenly after longtime successful service.
host.quality = Math.min(host.initialQuality + 4, nextQuality);
}
/**
* select host with the highest quality
* @param hostList
*/
static selectBestHostFromList(hostList) {
let ret = hostList[0];
for (let i = 1; i < hostList.length; ++i) {
if (hostList[i].quality > ret.quality) {
ret = hostList[i];
}
}
return ret;
}
}
exports.HostResolver = HostResolver;
//# sourceMappingURL=Host.js.map
;