Skip to content

Commit fa21e13

Browse files
committed
Limit maxAttempts to 5 for retries and hedging
1 parent f1f351f commit fa21e13

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

packages/grpc-js/src/retrying-call.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export class RetryingCall implements Call {
296296
return;
297297
}
298298
const retryPolicy = this.callConfig!.methodConfig.retryPolicy!;
299-
if (this.attempts >= retryPolicy.maxAttempts) {
299+
if (this.attempts >= Math.min(retryPolicy.maxAttempts, 5)) {
300300
callback(false);
301301
return;
302302
}
@@ -446,7 +446,7 @@ export class RetryingCall implements Call {
446446
return;
447447
}
448448
const hedgingPolicy = this.callConfig.methodConfig.hedgingPolicy;
449-
if (this.attempts >= hedgingPolicy.maxAttempts) {
449+
if (this.attempts >= Math.min(hedgingPolicy.maxAttempts, 5)) {
450450
return;
451451
}
452452
this.attempts += 1;
@@ -465,7 +465,7 @@ export class RetryingCall implements Call {
465465
return;
466466
}
467467
const hedgingPolicy = this.callConfig.methodConfig.hedgingPolicy;
468-
if (this.attempts >= hedgingPolicy.maxAttempts) {
468+
if (this.attempts >= Math.min(hedgingPolicy.maxAttempts, 5)) {
469469
return;
470470
}
471471
const hedgingDelayString = hedgingPolicy.hedgingDelay ?? '0s';

packages/grpc-js/test/test-retry.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,39 @@ describe('Retries', () => {
216216
}
217217
);
218218
});
219+
220+
it('Should not be able to make more than 5 attempts', (done) => {
221+
const serviceConfig = {
222+
loadBalancingConfig: [],
223+
methodConfig: [
224+
{
225+
name: [{
226+
service: 'EchoService'
227+
}],
228+
retryPolicy: {
229+
maxAttempts: 10,
230+
initialBackoff: '0.1s',
231+
maxBackoff: '10s',
232+
backoffMultiplier: 1.2,
233+
retryableStatusCodes: [14, 'RESOURCE_EXHAUSTED']
234+
}
235+
}
236+
]
237+
}
238+
const client2 = new EchoService(`localhost:${port}`, grpc.credentials.createInsecure(), {'grpc.service_config': JSON.stringify(serviceConfig)});
239+
const metadata = new grpc.Metadata();
240+
metadata.set('succeed-on-retry-attempt', '6');
241+
metadata.set('respond-with-status', `${grpc.status.RESOURCE_EXHAUSTED}`);
242+
client2.echo(
243+
{ value: 'test value', value2: 3 },
244+
metadata,
245+
(error: grpc.ServiceError, response: any) => {
246+
assert(error);
247+
assert.strictEqual(error.details, 'Failed on retry 4');
248+
done();
249+
}
250+
);
251+
})
219252
});
220253

221254
describe('Client with hedging configured', () => {
@@ -297,5 +330,35 @@ describe('Retries', () => {
297330
}
298331
);
299332
});
333+
334+
it('Should not be able to make more than 5 attempts', (done) => {
335+
const serviceConfig = {
336+
loadBalancingConfig: [],
337+
methodConfig: [
338+
{
339+
name: [{
340+
service: 'EchoService'
341+
}],
342+
hedgingPolicy: {
343+
maxAttempts: 10,
344+
nonFatalStatusCodes: [14, 'RESOURCE_EXHAUSTED']
345+
}
346+
}
347+
]
348+
}
349+
const client2 = new EchoService(`localhost:${port}`, grpc.credentials.createInsecure(), {'grpc.service_config': JSON.stringify(serviceConfig)});
350+
const metadata = new grpc.Metadata();
351+
metadata.set('succeed-on-retry-attempt', '6');
352+
metadata.set('respond-with-status', `${grpc.status.RESOURCE_EXHAUSTED}`);
353+
client2.echo(
354+
{ value: 'test value', value2: 3 },
355+
metadata,
356+
(error: grpc.ServiceError, response: any) => {
357+
assert(error);
358+
assert(error.details.startsWith('Failed on retry'));
359+
done();
360+
}
361+
);
362+
})
300363
});
301364
});

0 commit comments

Comments
 (0)