ks3
Version:
本代码库为`金山云存储KS3`服务.主要提供`KS3 nodejs SDK`和`命令行工具`.
81 lines (70 loc) • 1.98 kB
JavaScript
class BaseWait {
constructor() {}
invoke(retryState) {
throw new Error("invoke() must be implemented by subclasses");
}
}
// 指数等待策略
class ExponentialWait extends BaseWait {
constructor(multiplier = 200, minInterval = 1, maxInterval = 10000) {
super();
this.multiplier = multiplier;
this.minInterval = minInterval;
this.maxInterval = maxInterval;
}
invoke(retryState) {
let waitTime = this.multiplier * Math.pow(2, retryState.attemptNumber - 1);
waitTime = Math.max(this.minInterval, Math.min(waitTime, this.maxInterval));
return waitTime;
}
}
// 固定等待策略
class FixedWait extends BaseWait {
constructor(fixedInterval = 200) {
super();
this.fixedInterval = fixedInterval;
}
invoke(retryState) {
return this.fixedInterval;
}
}
// 线性等待策略
class LinearWait extends BaseWait {
constructor(start = 200, increment = 100, maxInterval = 10000) {
super();
this.start = start;
this.increment = increment;
this.maxInterval = maxInterval;
}
invoke(retryState) {
let waitTime = this.start + (this.increment * (retryState.attemptNumber - 1));
return Math.max(0, Math.min(waitTime, this.maxInterval));
}
}
// 抖动时间策略
class JitterWait extends BaseWait {
constructor(initial = 200, jitter = 500, maxInterval = 10000) {
super();
this.initial = initial;
this.jitter = jitter;
this.maxInterval = maxInterval;
}
invoke(retryState) {
let waitTime = this.initial + (Math.random() * this.jitter);
return Math.min(waitTime, this.maxInterval);
}
}
class RetryState {
constructor(attemptNumber) {
this.attemptNumber = attemptNumber;
}
}
// 导出所有类
module.exports = {
BaseWait,
ExponentialWait,
FixedWait,
LinearWait,
JitterWait,
RetryState,
};