Skip to content

Commit bcea4b4

Browse files
authored
Merge pull request #2688 from murgatroid99/example_keepalive
Add keepalive example
2 parents cfa8072 + f794b77 commit bcea4b4

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

Diff for: examples/keepalive/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Keepalive
2+
3+
This example illustrates how to set up client-side keepalive pings and
4+
server-side keepalive pings and connection age and idleness settings.
5+
6+
## Start the server
7+
8+
```
9+
node server.js
10+
```
11+
12+
## Start the client
13+
14+
```
15+
GRPC_TRACE=transport,keepalive GRPC_VERBOSITY=DEBUG node client.js
16+
```

Diff for: examples/keepalive/client.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
*
3+
* Copyright 2024 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 keepaliveOptions = {
36+
// Ping the server every 10 seconds to ensure the connection is still active
37+
'grpc.keepalive_time_ms': 10_000,
38+
// Wait 1 second for the ping ack before assuming the connection is dead
39+
'grpc.keepalive_timeout_ms': 1_000,
40+
// send pings even without active streams
41+
'grpc.keepalive_permit_without_calls': 1
42+
}
43+
44+
function main() {
45+
let argv = parseArgs(process.argv.slice(2), {
46+
string: 'target',
47+
default: {target: 'localhost:50052'}
48+
});
49+
const client = new echoProto.Echo(argv.target, grpc.credentials.createInsecure(), keepaliveOptions);
50+
client.unaryEcho({message: 'keepalive demo'}, (error, value) => {
51+
if (error) {
52+
console.log(`Unexpected error from UnaryEcho: ${error}`);
53+
return;
54+
}
55+
console.log(`RPC response: ${JSON.stringify(value)}`);
56+
});
57+
// Keep process alive forever; run with GRPC_TRACE=transport,keepalive GRPC_VERBOSITY=DEBUG to observe ping frames and GOAWAYs due to idleness.
58+
setInterval(() => {}, 1000);
59+
}
60+
61+
main();

Diff for: examples/keepalive/server.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
*
3+
* Copyright 2024 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 keepaliveOptions = {
36+
// If a client is idle for 15 seconds, send a GOAWAY
37+
'grpc.max_connection_idle_ms': 15_000,
38+
// If any connection is alive for more than 30 seconds, send a GOAWAY
39+
'grpc.max_connection_age_ms': 30_000,
40+
// Allow 5 seconds for pending RPCs to complete before forcibly closing connections
41+
'grpc.max_connection_age_grace_ms': 5_000,
42+
// Ping the client every 5 seconds to ensure the connection is still active
43+
'grpc.keepalive_time_ms': 5_000,
44+
// Wait 1 second for the ping ack before assuming the connection is dead
45+
'grpc.keepalive_timeout_ms': 1_000
46+
}
47+
48+
function unaryEcho(call, callback) {
49+
callback(null, call.request);
50+
}
51+
52+
const serviceImplementation = {
53+
unaryEcho
54+
};
55+
56+
function main() {
57+
const argv = parseArgs(process.argv.slice(2), {
58+
string: 'port',
59+
default: {port: '50052'}
60+
});
61+
const server = new grpc.Server(keepaliveOptions);
62+
server.addService(echoProto.Echo.service, serviceImplementation);
63+
server.bindAsync(`0.0.0.0:${argv.port}`, grpc.ServerCredentials.createInsecure(), (err, port) => {
64+
if (err != null) {
65+
return console.error(err);
66+
}
67+
console.log(`gRPC listening on ${port}`)
68+
});
69+
}
70+
71+
main();

Diff for: examples/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"@grpc/proto-loader": "^0.6.0",
66
"async": "^1.5.2",
77
"google-protobuf": "^3.0.0",
8-
"@grpc/grpc-js": "^1.8.0",
9-
"@grpc/grpc-js-xds": "^1.8.0",
8+
"@grpc/grpc-js": "^1.10.2",
9+
"@grpc/grpc-js-xds": "^1.10.0",
1010
"@grpc/reflection": "^1.0.0",
1111
"lodash": "^4.6.1",
1212
"minimist": "^1.2.0"

0 commit comments

Comments
 (0)