@@ -21,6 +21,8 @@ import { loadSync, ServiceDefinition } from '@grpc/proto-loader';
21
21
import { HealthCheckRequest__Output } from './generated/grpc/health/v1/HealthCheckRequest' ;
22
22
import { HealthCheckResponse } from './generated/grpc/health/v1/HealthCheckResponse' ;
23
23
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' ;
24
26
25
27
const loadedProto = loadSync ( 'health/v1/health.proto' , {
26
28
keepCase : true ,
@@ -34,6 +36,8 @@ const loadedProto = loadSync('health/v1/health.proto', {
34
36
export const service = loadedProto [ 'grpc.health.v1.Health' ] as ServiceDefinition ;
35
37
36
38
const GRPC_STATUS_NOT_FOUND = 5 ;
39
+ const GRPC_STATUS_RESOURCE_EXHAUSTED = 8 ;
40
+ const RESOURCE_EXHAUSTION_LIMIT = 100 ;
37
41
38
42
export type ServingStatus = 'UNKNOWN' | 'SERVING' | 'NOT_SERVING' ;
39
43
@@ -104,7 +108,25 @@ export class HealthImplementation {
104
108
} else {
105
109
call . write ( { status : 'SERVICE_UNKNOWN' } ) ;
106
110
}
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
+ } ,
108
130
} ) ;
109
131
}
110
132
}
0 commit comments