Skip to content

Commit 50759b5

Browse files
committed
Add grpc health list endpoint
1 parent a89238d commit 50759b5

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

packages/grpc-health-check/src/health.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { loadSync, ServiceDefinition } from '@grpc/proto-loader';
2121
import { HealthCheckRequest__Output } from './generated/grpc/health/v1/HealthCheckRequest';
2222
import { HealthCheckResponse } from './generated/grpc/health/v1/HealthCheckResponse';
2323
import { sendUnaryData, Server, ServerUnaryCall, ServerWritableStream } from './server-type';
24+
import { HealthListRequest } from './generated/grpc/health/v1/HealthListRequest';
25+
import { HealthListResponse } from './generated/grpc/health/v1/HealthListResponse';
2426

2527
const loadedProto = loadSync('health/v1/health.proto', {
2628
keepCase: true,
@@ -34,6 +36,8 @@ const loadedProto = loadSync('health/v1/health.proto', {
3436
export const service = loadedProto['grpc.health.v1.Health'] as ServiceDefinition;
3537

3638
const GRPC_STATUS_NOT_FOUND = 5;
39+
const GRPC_STATUS_RESOURCE_EXHAUSTED = 8;
40+
const RESOURCE_EXHAUSTION_LIMIT = 100;
3741

3842
export type ServingStatus = 'UNKNOWN' | 'SERVING' | 'NOT_SERVING';
3943

@@ -104,7 +108,25 @@ export class HealthImplementation {
104108
} else {
105109
call.write({status: 'SERVICE_UNKNOWN'});
106110
}
107-
}
111+
},
112+
list: (_call: ServerUnaryCall<HealthListRequest, HealthListResponse>, callback: sendUnaryData<HealthListResponse>) => {
113+
const statuses: { [key: string]: HealthCheckResponse } = {};
114+
let serviceCount = 0;
115+
116+
for (const [serviceName, status] of this.statusMap.entries()) {
117+
if (serviceCount >= RESOURCE_EXHAUSTION_LIMIT) {
118+
const error = {
119+
code: GRPC_STATUS_RESOURCE_EXHAUSTED,
120+
details: 'Too many services to list.',
121+
};
122+
callback(error, null);
123+
return;
124+
}
125+
statuses[serviceName] = { status };
126+
serviceCount++;
127+
}
128+
callback(null, { statuses });
129+
},
108130
});
109131
}
110132
}

0 commit comments

Comments
 (0)