Skip to content

Commit 0e6bfcc

Browse files
committed
Merge branch 'master' into grpc-js-xds_interop_server
2 parents e1a9f12 + d2462ca commit 0e6bfcc

27 files changed

+621
-172
lines changed

Diff for: doc/environment_variables.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ can be set.
1111
checked in order, and the first one that has a value is used.
1212

1313
* no_grpc_proxy, no_proxy
14-
A comma separated list of hostnames to connect to without using a proxy even
15-
if a proxy is set. These variables are checked in order, and the first one
14+
A comma separated list of hostnames, IP addresses,
15+
or CIDR blocks to connect to without using a proxy even
16+
if a proxy is set, for example: no_proxy=example.com,192.168.0.1,192.168.0.0/16.
17+
These variables are checked in order, and the first one
1618
that has a value is used.
1719

1820
* GRPC_SSL_CIPHER_SUITES
@@ -66,4 +68,4 @@ can be set.
6668
* GRPC_NODE_USE_ALTERNATIVE_RESOLVER
6769
Allows changing dns resolve behavior and parse DNS server authority as described in https://github.com/grpc/grpc/blob/master/doc/naming.md
6870
- true - use alternative resolver
69-
- false - use default resolver (default)
71+
- false - use default resolver (default)

Diff for: 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+
```

Diff for: 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();

Diff for: 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();

Diff for: packages/grpc-js-xds/src/resolver-xds.ts

+79-29
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ const RETRY_CODES: {[key: string]: status} = {
8383
export const XDS_CONFIG_KEY = `${experimental.SUBCHANNEL_ARGS_EXCLUDE_KEY_PREFIX}.xds_config`;
8484
export const XDS_CLIENT_KEY = 'grpc.internal.xds_client';
8585

86+
/**
87+
* Tracks a dynamic subscription to a cluster that is currently or previously
88+
* referenced in a RouteConfiguration.
89+
*/
90+
class ClusterRef {
91+
private refCount = 0;
92+
constructor(private unsubscribe: () => void) {}
93+
94+
ref() {
95+
this.refCount += 1;
96+
}
97+
98+
unref() {
99+
this.refCount -= 1;
100+
if (this.refCount <= 0) {
101+
this.unsubscribe();
102+
}
103+
}
104+
105+
hasRef() {
106+
return this.refCount > 0;
107+
}
108+
}
109+
86110
class XdsResolver implements Resolver {
87111

88112
private listenerResourceName: string | null = null;
@@ -93,6 +117,7 @@ class XdsResolver implements Resolver {
93117

94118
private xdsConfigWatcher: XdsConfigWatcher;
95119
private xdsDependencyManager: XdsDependencyManager | null = null;
120+
private clusterRefs: Map<string, ClusterRef> = new Map();
96121

97122
constructor(
98123
private target: GrpcUri,
@@ -123,11 +148,20 @@ class XdsResolver implements Resolver {
123148
}
124149
}
125150

151+
private pruneUnusedClusters() {
152+
for (const [cluster, clusterRef] of this.clusterRefs) {
153+
if (!clusterRef.hasRef()) {
154+
this.clusterRefs.delete(cluster);
155+
}
156+
}
157+
}
158+
126159
private async handleXdsConfig(xdsConfig: XdsConfig) {
127160
/* We need to load the xxhash API before this function finishes, because
128161
* it is invoked in the config selector, which can be called immediately
129162
* after this function returns. */
130163
await loadXxhashApi();
164+
this.pruneUnusedClusters();
131165
const httpConnectionManager = decodeSingleResource(HTTP_CONNECTION_MANGER_TYPE_URL, xdsConfig.listener.api_listener!.api_listener!.value);
132166
const configDefaultTimeout = httpConnectionManager.common_http_protocol_options?.idle_timeout;
133167
let defaultTimeout: Duration | undefined = undefined;
@@ -312,44 +346,60 @@ class XdsResolver implements Resolver {
312346
const routeMatcher = getPredicateForMatcher(route.match!);
313347
matchList.push({matcher: routeMatcher, action: routeAction});
314348
}
315-
const configSelector: ConfigSelector = (methodName, metadata, channelId) => {
316-
for (const {matcher, action} of matchList) {
317-
if (matcher.apply(methodName, metadata)) {
318-
const clusterResult = action.getCluster();
319-
const unrefCluster = this.xdsDependencyManager!.addClusterSubscription(clusterResult.name);
320-
const onCommitted = () => {
321-
unrefCluster();
322-
}
323-
let hash: string;
324-
if (EXPERIMENTAL_RING_HASH) {
325-
hash = `${action.getHash(metadata, channelId)}`;
326-
} else {
327-
hash = '';
349+
for (const cluster of allConfigClusters) {
350+
let clusterRef = this.clusterRefs.get(cluster);
351+
if (!clusterRef) {
352+
clusterRef = new ClusterRef(this.xdsDependencyManager!.addClusterSubscription(cluster));
353+
this.clusterRefs.set(cluster, clusterRef);
354+
}
355+
clusterRef.ref();
356+
}
357+
const configSelector: ConfigSelector = {
358+
invoke: (methodName, metadata, channelId) => {
359+
for (const {matcher, action} of matchList) {
360+
if (matcher.apply(methodName, metadata)) {
361+
const clusterResult = action.getCluster();
362+
const clusterRef = this.clusterRefs.get(clusterResult.name)!;
363+
clusterRef.ref();
364+
const onCommitted = () => {
365+
clusterRef.unref();
366+
}
367+
let hash: string;
368+
if (EXPERIMENTAL_RING_HASH) {
369+
hash = `${action.getHash(metadata, channelId)}`;
370+
} else {
371+
hash = '';
372+
}
373+
return {
374+
methodConfig: clusterResult.methodConfig,
375+
onCommitted: onCommitted,
376+
pickInformation: {cluster: clusterResult.name, hash: hash},
377+
status: status.OK,
378+
dynamicFilterFactories: clusterResult.dynamicFilterFactories
379+
};
328380
}
329-
return {
330-
methodConfig: clusterResult.methodConfig,
331-
onCommitted: onCommitted,
332-
pickInformation: {cluster: clusterResult.name, hash: hash},
333-
status: status.OK,
334-
dynamicFilterFactories: clusterResult.dynamicFilterFactories
335-
};
381+
}
382+
return {
383+
methodConfig: {name: []},
384+
// These fields won't be used here, but they're set because of some TypeScript weirdness
385+
pickInformation: {cluster: '', hash: ''},
386+
status: status.UNAVAILABLE,
387+
dynamicFilterFactories: []
388+
};
389+
},
390+
unref: () => {
391+
for (const cluster of allConfigClusters) {
392+
this.clusterRefs.get(cluster)?.unref();
336393
}
337394
}
338-
return {
339-
methodConfig: {name: []},
340-
// These fields won't be used here, but they're set because of some TypeScript weirdness
341-
pickInformation: {cluster: '', hash: ''},
342-
status: status.UNAVAILABLE,
343-
dynamicFilterFactories: []
344-
};
345-
};
395+
}
346396
trace('Created ConfigSelector with configuration:');
347397
for (const {matcher, action} of matchList) {
348398
trace(matcher.toString());
349399
trace('=> ' + action.toString());
350400
}
351401
const clusterConfigMap: {[key: string]: {child_policy: LoadBalancingConfig[]}} = {};
352-
for (const clusterName of allConfigClusters) {
402+
for (const clusterName of this.clusterRefs.keys()) {
353403
clusterConfigMap[clusterName] = {child_policy: [{cds: {cluster: clusterName}}]};
354404
}
355405
const lbPolicyConfig = {xds_cluster_manager: {children: clusterConfigMap}};

0 commit comments

Comments
 (0)