25 lines
1.1 KiB
JavaScript
25 lines
1.1 KiB
JavaScript
|
import { RETRY_MODES } from "./config";
|
||
|
import { DefaultRateLimiter } from "./DefaultRateLimiter";
|
||
|
import { StandardRetryStrategy } from "./StandardRetryStrategy";
|
||
|
export class AdaptiveRetryStrategy {
|
||
|
constructor(maxAttemptsProvider, options) {
|
||
|
this.maxAttemptsProvider = maxAttemptsProvider;
|
||
|
this.mode = RETRY_MODES.ADAPTIVE;
|
||
|
const { rateLimiter } = options ?? {};
|
||
|
this.rateLimiter = rateLimiter ?? new DefaultRateLimiter();
|
||
|
this.standardRetryStrategy = new StandardRetryStrategy(maxAttemptsProvider);
|
||
|
}
|
||
|
async acquireInitialRetryToken(retryTokenScope) {
|
||
|
await this.rateLimiter.getSendToken();
|
||
|
return this.standardRetryStrategy.acquireInitialRetryToken(retryTokenScope);
|
||
|
}
|
||
|
async refreshRetryTokenForRetry(tokenToRenew, errorInfo) {
|
||
|
this.rateLimiter.updateClientSendingRate(errorInfo);
|
||
|
return this.standardRetryStrategy.refreshRetryTokenForRetry(tokenToRenew, errorInfo);
|
||
|
}
|
||
|
recordSuccess(token) {
|
||
|
this.rateLimiter.updateClientSendingRate({});
|
||
|
this.standardRetryStrategy.recordSuccess(token);
|
||
|
}
|
||
|
}
|