-
Notifications
You must be signed in to change notification settings - Fork 654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add keepalive example #2688
Merged
Merged
Add keepalive example #2688
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Keepalive | ||
|
||
This example illustrates how to set up client-side keepalive pings and | ||
server-side keepalive pings and connection age and idleness settings. | ||
|
||
## Start the server | ||
|
||
``` | ||
node server.js | ||
``` | ||
|
||
## Start the client | ||
|
||
``` | ||
GRPC_TRACE=transport,keepalive GRPC_VERBOSITY=DEBUG node client.js | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* | ||
* Copyright 2024 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
const grpc = require('@grpc/grpc-js'); | ||
const protoLoader = require('@grpc/proto-loader'); | ||
const parseArgs = require('minimist'); | ||
|
||
const PROTO_PATH = __dirname + '/../protos/echo.proto'; | ||
|
||
const packageDefinition = protoLoader.loadSync( | ||
PROTO_PATH, | ||
{keepCase: true, | ||
longs: String, | ||
enums: String, | ||
defaults: true, | ||
oneofs: true | ||
}); | ||
const echoProto = grpc.loadPackageDefinition(packageDefinition).grpc.examples.echo; | ||
|
||
const keepaliveOptions = { | ||
// Ping the server every 10 seconds to ensure the connection is still active | ||
'grpc.keepalive_time_ms': 10_000, | ||
// Wait 1 second for the ping ack before assuming the connection is dead | ||
'grpc.keepalive_timeout_ms': 1_000, | ||
// send pings even without active streams | ||
'grpc.keepalive_permit_without_calls': 1 | ||
} | ||
|
||
function main() { | ||
let argv = parseArgs(process.argv.slice(2), { | ||
string: 'target', | ||
default: {target: 'localhost:50052'} | ||
}); | ||
const client = new echoProto.Echo(argv.target, grpc.credentials.createInsecure(), keepaliveOptions); | ||
client.unaryEcho({message: 'keepalive demo'}, (error, value) => { | ||
if (error) { | ||
console.log(`Unexpected error from UnaryEcho: ${error}`); | ||
return; | ||
} | ||
console.log(`RPC response: ${JSON.stringify(value)}`); | ||
}); | ||
// Keep process alive forever; run with GRPC_TRACE=transport,keepalive GRPC_VERBOSITY=DEBUG to observe ping frames and GOAWAYs due to idleness. | ||
setInterval(() => {}, 1000); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* | ||
* Copyright 2024 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
const grpc = require('@grpc/grpc-js'); | ||
const protoLoader = require('@grpc/proto-loader'); | ||
const parseArgs = require('minimist'); | ||
|
||
const PROTO_PATH = __dirname + '/../protos/echo.proto'; | ||
|
||
const packageDefinition = protoLoader.loadSync( | ||
PROTO_PATH, | ||
{keepCase: true, | ||
longs: String, | ||
enums: String, | ||
defaults: true, | ||
oneofs: true | ||
}); | ||
const echoProto = grpc.loadPackageDefinition(packageDefinition).grpc.examples.echo; | ||
|
||
const keepaliveOptions = { | ||
// If a client is idle for 15 seconds, send a GOAWAY | ||
'grpc.max_connection_idle_ms': 15_000, | ||
// If any connection is alive for more than 30 seconds, send a GOAWAY | ||
'grpc.max_connection_age_ms': 30_000, | ||
// Allow 5 seconds for pending RPCs to complete before forcibly closing connections | ||
'grpc.max_connection_age_grace_ms': 5_000, | ||
// Ping the client every 5 seconds to ensure the connection is still active | ||
'grpc.keepalive_time_ms': 5_000, | ||
// Wait 1 second for the ping ack before assuming the connection is dead | ||
'grpc.keepalive_timeout_ms': 1_000 | ||
} | ||
|
||
function unaryEcho(call, callback) { | ||
callback(null, call.request); | ||
} | ||
|
||
const serviceImplementation = { | ||
unaryEcho | ||
}; | ||
|
||
function main() { | ||
const argv = parseArgs(process.argv.slice(2), { | ||
string: 'port', | ||
default: {port: '50052'} | ||
}); | ||
const server = new grpc.Server(keepaliveOptions); | ||
server.addService(echoProto.Echo.service, serviceImplementation); | ||
server.bindAsync(`0.0.0.0:${argv.port}`, grpc.ServerCredentials.createInsecure(), (err, port) => { | ||
if (err != null) { | ||
return console.error(err); | ||
} | ||
console.log(`gRPC listening on ${port}`) | ||
}); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readability nit: "...set up client and server keepalive pings as well as connection..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I'm not sure. I'm trying to convey that the keepalive ping settings are on both the client and the server, but the connection age and idleness settings are only on the server. The Go example uses this wording:
Which is similar, but also omits the fact that it has server-side keepalive pings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see, on initial read this distinction did not come through, but makes sense now that you mentioned it. If you wanted to be super clear you go with:
"...how to set up keepalive pings on the client-side and keepalive pings, connection age and idleness setting on the server-side"
But I'll leave it up to you.