Skip to content

Commit b1fd52d

Browse files
committed
Add retry example
1 parent 34b82cb commit b1fd52d

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed

examples/retry/README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Retry
2+
3+
This example shows how to enable and configure retry on gRPC clients.
4+
5+
## Documentation
6+
7+
[gRFC for client-side retry support](https://github.com/grpc/proposal/blob/master/A6-client-retries.md)
8+
9+
## Try it
10+
11+
This example includes a service implementation that fails requests three times with status code Unavailable, then passes the fourth. The client is configured to make four retry attempts when receiving an Unavailable status code.
12+
13+
First start the server:
14+
15+
```
16+
node server.js
17+
```
18+
19+
Then run the client:
20+
21+
```
22+
node client.js
23+
```
24+
25+
## Usage
26+
27+
### Define your retry policy
28+
29+
Retry is configured via the service config, which can be provided by the name resolver, or as a channel option (described below). In the below example, we set the retry policy for the "grpc.example.echo.Echo" method.
30+
31+
```js
32+
const serviceConfig = {
33+
loadBalancingConfig: [],
34+
methodConfig: [
35+
{
36+
name: [
37+
{
38+
service: 'grpc.examples.echo.Echo',
39+
},
40+
],
41+
retryPolicy: {
42+
maxAttempts: 4,
43+
initialBackoff: '0.01s',
44+
maxBackoff: '0.01s',
45+
backoffMultiplier: 1.0,
46+
retryableStatusCodes: ['UNAVAILABLE'],
47+
},
48+
},
49+
],
50+
};
51+
```
52+
53+
### Providing the retry policy as a channel option
54+
55+
```js
56+
const client = new Echo('localhost:50052', grpc.credentials.createInsecure(), { 'grpc.service_config': JSON.stringify(serviceConfig) });
57+
```

examples/retry/client.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
*
3+
* Copyright 2025 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
const grpc = require('@grpc/grpc-js');
20+
const protoLoader = require('@grpc/proto-loader');
21+
const parseArgs = require('minimist');
22+
23+
const PROTO_PATH = __dirname + '/../protos/echo.proto';
24+
25+
const packageDefinition = protoLoader.loadSync(
26+
PROTO_PATH,
27+
{keepCase: true,
28+
longs: String,
29+
enums: String,
30+
defaults: true,
31+
oneofs: true
32+
});
33+
const echoProto = grpc.loadPackageDefinition(packageDefinition).grpc.examples.echo;
34+
35+
const serviceConfig = {
36+
loadBalancingConfig: [],
37+
methodConfig: [
38+
{
39+
name: [
40+
{
41+
service: 'grpc.examples.echo.Echo',
42+
},
43+
],
44+
retryPolicy: {
45+
maxAttempts: 4,
46+
initialBackoff: '0.01s',
47+
maxBackoff: '0.01s',
48+
backoffMultiplier: 1.0,
49+
retryableStatusCodes: ['UNAVAILABLE'],
50+
},
51+
},
52+
],
53+
};
54+
55+
function main() {
56+
let argv = parseArgs(process.argv.slice(2), {
57+
string: 'target',
58+
default: {target: 'localhost:50052'}
59+
});
60+
61+
// Set up a connection to the server with service config and create the channel.
62+
// However, the recommended approach is to fetch the retry configuration
63+
// (which is part of the service config) from the name resolver rather than
64+
// defining it on the client side.
65+
const client = new echoProto.Echo('localhost:50052', grpc.credentials.createInsecure(), { 'grpc.service_config': JSON.stringify(serviceConfig) });
66+
client.unaryEcho({message: 'Try and Success'}, (error, value) => {
67+
if (error) {
68+
console.log(`Unexpected error from UnaryEcho: ${error}`);
69+
return;
70+
}
71+
console.log(`RPC response: ${JSON.stringify(value)}`);
72+
});
73+
}
74+
75+
main();

examples/retry/server.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
*
3+
* Copyright 2025 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
const grpc = require('@grpc/grpc-js');
20+
const protoLoader = require('@grpc/proto-loader');
21+
const parseArgs = require('minimist');
22+
23+
const PROTO_PATH = __dirname + '/../protos/echo.proto';
24+
25+
const packageDefinition = protoLoader.loadSync(
26+
PROTO_PATH,
27+
{keepCase: true,
28+
longs: String,
29+
enums: String,
30+
defaults: true,
31+
oneofs: true
32+
});
33+
const echoProto = grpc.loadPackageDefinition(packageDefinition).grpc.examples.echo;
34+
35+
const SUCCEED_EVERY = 4
36+
let callCount = 0;
37+
38+
/* This method will succeed every SUCCEED_EVERY calls, and fail all others with status code
39+
* UNAVAILABLE. */
40+
function unaryEcho(call, callback) {
41+
callCount++;
42+
if (callCount % SUCCEED_EVERY === 0) {
43+
console.log(`Request succeeded count: ${callCount}`);
44+
callback(null, call.request);
45+
} else {
46+
console.log(`Request failed count: ${callCount}`);
47+
callback({
48+
code: grpc.status.UNAVAILABLE,
49+
details: 'Request failed by policy'
50+
});
51+
}
52+
}
53+
54+
const serviceImplementation = {
55+
unaryEcho
56+
};
57+
58+
function main() {
59+
const argv = parseArgs(process.argv.slice(2), {
60+
string: 'port',
61+
default: {port: '50052'}
62+
});
63+
const server = new grpc.Server();
64+
server.addService(echoProto.Echo.service, serviceImplementation);
65+
server.bindAsync(`0.0.0.0:${argv.port}`, grpc.ServerCredentials.createInsecure(), (err, port) => {
66+
if (err != null) {
67+
return console.error(err);
68+
}
69+
console.log(`gRPC listening on ${port}`)
70+
});
71+
}
72+
73+
main();

0 commit comments

Comments
 (0)