grpc-js: Fix min/max switch in retry throttler

This commit is contained in:
Michael Lumish 2025-03-20 17:47:10 -07:00
parent 5572bbd432
commit 26d26d7b0a
2 changed files with 14 additions and 7 deletions

View File

@ -57,15 +57,15 @@ export class RetryThrottler {
} }
addCallSucceeded() { addCallSucceeded() {
this.tokens = Math.max(this.tokens + this.tokenRatio, this.maxTokens); this.tokens = Math.min(this.tokens + this.tokenRatio, this.maxTokens);
} }
addCallFailed() { addCallFailed() {
this.tokens = Math.min(this.tokens - 1, 0); this.tokens = Math.max(this.tokens - 1, 0);
} }
canRetryCall() { canRetryCall() {
return this.tokens > this.maxTokens / 2; return this.tokens > (this.maxTokens / 2);
} }
} }
@ -217,7 +217,10 @@ export class RetryingCall implements Call, DeadlineInfoProvider {
private readonly retryThrottler?: RetryThrottler private readonly retryThrottler?: RetryThrottler
) { ) {
const maxAttemptsLimit = channel.getOptions()['grpc-node.retry_max_attempts_limit'] ?? DEFAULT_MAX_ATTEMPTS_LIMIT; const maxAttemptsLimit = channel.getOptions()['grpc-node.retry_max_attempts_limit'] ?? DEFAULT_MAX_ATTEMPTS_LIMIT;
if (callConfig.methodConfig.retryPolicy) { if (channel.getOptions()['grpc.enable_retries'] === 0) {
this.state = 'NO_RETRY';
this.maxAttempts = 1;
} else if (callConfig.methodConfig.retryPolicy) {
this.state = 'RETRY'; this.state = 'RETRY';
const retryPolicy = callConfig.methodConfig.retryPolicy; const retryPolicy = callConfig.methodConfig.retryPolicy;
this.nextRetryBackoffSec = this.initialRetryBackoffSec = Number( this.nextRetryBackoffSec = this.initialRetryBackoffSec = Number(
@ -230,9 +233,6 @@ export class RetryingCall implements Call, DeadlineInfoProvider {
} else if (callConfig.methodConfig.hedgingPolicy) { } else if (callConfig.methodConfig.hedgingPolicy) {
this.state = 'HEDGING'; this.state = 'HEDGING';
this.maxAttempts = Math.min(callConfig.methodConfig.hedgingPolicy.maxAttempts, maxAttemptsLimit); this.maxAttempts = Math.min(callConfig.methodConfig.hedgingPolicy.maxAttempts, maxAttemptsLimit);
} else if (channel.getOptions()['grpc.enable_retries'] === 0) {
this.state = 'NO_RETRY';
this.maxAttempts = 1;
} else { } else {
this.state = 'TRANSPARENT_ONLY'; this.state = 'TRANSPARENT_ONLY';
this.maxAttempts = 1; this.maxAttempts = 1;
@ -459,6 +459,9 @@ export class RetryingCall implements Call, DeadlineInfoProvider {
callback(true); callback(true);
this.attempts += 1; this.attempts += 1;
this.startNewAttempt(); this.startNewAttempt();
} else {
this.trace('Retry attempt denied by throttling policy');
callback(false);
} }
}, retryDelayMs); }, retryDelayMs);
} }

View File

@ -175,6 +175,10 @@ describe('Retries', () => {
}, },
}, },
], ],
retryThrottling: {
maxTokens: 1000,
tokenRatio: 0.1,
},
}; };
client = new EchoService( client = new EchoService(
`localhost:${port}`, `localhost:${port}`,