Skip to content

Commit 86debcd

Browse files
committed
Add cancellation example
1 parent 71936fa commit 86debcd

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

examples/cancellation/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Cancellation
2+
3+
This example shows how clients can cancel in-flight RPCs by cancelling the
4+
call object returned by the method invocation. The client will receive a status
5+
with code `CANCELLED` and the server handler's call object will emit a
6+
`'cancelled'` event.
7+
8+
## Start the server
9+
10+
```
11+
node server.js
12+
```
13+
14+
## Run the client
15+
16+
```
17+
node client.js
18+
```

examples/cancellation/client.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
*
3+
* Copyright 2023 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+
function main() {
36+
let argv = parseArgs(process.argv.slice(2), {
37+
string: 'target',
38+
default: {target: 'localhost:50052'}
39+
});
40+
const client = new echoProto.Echo(argv.target, grpc.credentials.createInsecure());
41+
const call = client.bidirectionalStreamingEcho();
42+
const EXPECTED_MESSAGES = 2;
43+
let receivedMessages = 0;
44+
call.on('data', value => {
45+
console.log(`received message "${value.message}"`)
46+
receivedMessages += 1;
47+
if (receivedMessages >= EXPECTED_MESSAGES) {
48+
console.log('cancelling call');
49+
call.cancel();
50+
}
51+
});
52+
call.on('status', statusObject => {
53+
console.log(`received call status with code ${grpc.status[statusObject.code]}`);
54+
});
55+
call.on('error', error => {
56+
console.log(`received error ${error}`);
57+
})
58+
console.log('sending message "hello"');
59+
call.write({message: 'hello'});
60+
console.log('sending message "world"')
61+
call.write({message: 'world'});
62+
}
63+
64+
main();

examples/cancellation/server.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
*
3+
* Copyright 2023 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+
function bidirectionalStreamingEcho(call) {
36+
call.on('data', value => {
37+
const message = value.message;
38+
console.log(`echoing message "${message}"`);
39+
call.write({message: message});
40+
});
41+
call.on('end', () => {
42+
call.end();
43+
});
44+
call.on('cancelled', () => {
45+
console.log('received cancelled event');
46+
});
47+
}
48+
49+
const serviceImplementation = {
50+
bidirectionalStreamingEcho
51+
}
52+
53+
function main() {
54+
const argv = parseArgs(process.argv.slice(2), {
55+
string: 'port',
56+
default: {port: '50052'}
57+
});
58+
const server = new grpc.Server();
59+
server.addService(echoProto.Echo.service, serviceImplementation);
60+
server.bindAsync(`0.0.0.0:${argv.port}`, grpc.ServerCredentials.createInsecure(), () => {
61+
server.start();
62+
});
63+
client = new echoProto.Echo(`localhost:${argv.port}`, grpc.credentials.createInsecure());
64+
}
65+
66+
main();

0 commit comments

Comments
 (0)