Skip to content

Commit

Permalink
Merge pull request #2609 from murgatroid99/grpc-js_resolver_loop_fix
Browse files Browse the repository at this point in the history
grpc-js: Don't repeat fixed resolver results
  • Loading branch information
murgatroid99 committed Oct 30, 2023
2 parents b75a8c9 + 9050ea9 commit 51b0f66
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/grpc-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grpc/grpc-js",
"version": "1.9.8",
"version": "1.9.9",
"description": "gRPC Library for Node - pure JS implementation",
"homepage": "https://grpc.io/",
"repository": "https://github.com/grpc/grpc-node/tree/master/packages/grpc-js",
Expand Down
25 changes: 15 additions & 10 deletions packages/grpc-js/src/resolver-dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class DnsResolver implements Resolver {
private nextResolutionTimer: NodeJS.Timeout;
private isNextResolutionTimerRunning = false;
private isServiceConfigEnabled = true;
private returnedIpResult = false;
constructor(
private target: GrpcUri,
private listener: ResolverListener,
Expand Down Expand Up @@ -163,16 +164,19 @@ class DnsResolver implements Resolver {
*/
private startResolution() {
if (this.ipResult !== null) {
trace('Returning IP address for target ' + uriToString(this.target));
setImmediate(() => {
this.listener.onSuccessfulResolution(
this.ipResult!,
null,
null,
null,
{}
);
});
if (!this.returnedIpResult) {
trace('Returning IP address for target ' + uriToString(this.target));
setImmediate(() => {
this.listener.onSuccessfulResolution(
this.ipResult!,
null,
null,
null,
{}
);
});
this.returnedIpResult = true;
}
this.backoff.stop();
this.backoff.reset();
this.stopNextResolutionTimer();
Expand Down Expand Up @@ -380,6 +384,7 @@ class DnsResolver implements Resolver {
this.latestLookupResult = null;
this.latestServiceConfig = null;
this.latestServiceConfigError = null;
this.returnedIpResult = false;
}

/**
Expand Down
32 changes: 18 additions & 14 deletions packages/grpc-js/src/resolver-ip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const DEFAULT_PORT = 443;
class IpResolver implements Resolver {
private addresses: SubchannelAddress[] = [];
private error: StatusObject | null = null;
private hasReturnedResult = false;
constructor(
target: GrpcUri,
private listener: ResolverListener,
Expand Down Expand Up @@ -87,22 +88,25 @@ class IpResolver implements Resolver {
trace('Parsed ' + target.scheme + ' address list ' + this.addresses);
}
updateResolution(): void {
process.nextTick(() => {
if (this.error) {
this.listener.onError(this.error);
} else {
this.listener.onSuccessfulResolution(
this.addresses,
null,
null,
null,
{}
);
}
});
if (!this.hasReturnedResult) {
this.hasReturnedResult = true;
process.nextTick(() => {
if (this.error) {
this.listener.onError(this.error);
} else {
this.listener.onSuccessfulResolution(
this.addresses,
null,
null,
null,
{}
);
}
});
}
}
destroy(): void {
// This resolver owns no resources, so we do nothing here.
this.hasReturnedResult = false;
}

static getDefaultAuthority(target: GrpcUri): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/grpc-js/test/test-pick-first.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('Shuffler', () => {
});
});

describe.only('pick_first load balancing policy', () => {
describe('pick_first load balancing policy', () => {
const config = new PickFirstLoadBalancingConfig(false);
let subchannels: MockSubchannel[] = [];
const baseChannelControlHelper: ChannelControlHelper = {
Expand Down

0 comments on commit 51b0f66

Please sign in to comment.