Skip to content
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

Memory leak in grpc health indicator #2329

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/health-indicator/microservice/grpc.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ export class GRPCHealthIndicator extends HealthIndicator {
this.checkDependantPackages();
}

/**
* A cache of open channels for the health indicator
* This is used to prevent opening new channels for every health check
*/
private readonly openChannels = new Map<string, GRPCHealthService>();

/**
* Checks if the dependant packages are present
*/
Expand Down Expand Up @@ -185,13 +191,19 @@ export class GRPCHealthIndicator extends HealthIndicator {

const settings = { ...defaultOptions, ...options };

const client = this.createClient<GrpcOptions>(settings);

let healthService: GRPCHealthService;
try {
healthService = client.getService<GRPCHealthService | any>(
settings.healthServiceName as string,
);
if (this.openChannels.has(service)) {
healthService = this.openChannels.get(service)!;
} else {
const client = this.createClient<GrpcOptions>(settings);

healthService = client.getService<GRPCHealthService>(
settings.healthServiceName as string,
);

this.openChannels.set(service, healthService);
}
} catch (err) {
if (err instanceof TypeError) throw err;
if (isError(err)) {
Expand Down