-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcommon.ts
282 lines (239 loc) · 5.49 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { Client } from 'grpc';
import { Options as GRPCProtoLoaderOptions } from '@grpc/proto-loader';
import { Options as NodeRetryOpts } from 'async-retry';
export class GRPCHelperError extends Error {
name: string = 'GRPCHelperError';
detail: any;
constructor(message?: string, detail?: any) {
super(message);
this.detail = detail;
}
}
export interface GRPCHelperSslOpts {
/**
* Whether enable ssl
* Default: false
*/
enable: boolean;
/**
* CA cert in buffer
*/
cacert?: Buffer;
/**
* cert in buffer
*/
cert?: Buffer;
/**
* key in buffer
*/
key?: Buffer;
}
export interface GRPCHelperClient {
address: string;
weight: number;
connected: boolean;
grpcClient: Client;
brake: any;
[method: string]: any;
}
export interface GRPCHelperCheck {
/**
* Whether enable health check
* Default: true
*/
enable: boolean;
/**
* Timeout for health check call
* Default: 5000
*/
timeoutInMS?: number;
}
export interface GRPCOpts {
interceptors?: ((...args) => any)[];
interceptor_providers?: ((...args) => any)[];
[key: string]: any;
}
/**
* From https://github.com/awolden/brakes
*/
export interface BrakeOpts {
/**
* string to use for name of circuit.
* This is mostly used for reporting on stats.
*/
name?: string;
/**
* string to use for group of circuit.
* This is mostly used for reporting on stats.
*/
group?: string;
/**
* time in ms that a specific bucket should remain active
*/
bucketSpan?: number;
/**
* interval in ms that brakes should emit a snapshot event
*/
statInterval?: number;
/**
* array<number> that defines the percentile levels that
* should be calculated on the stats object
* (i.e. 0.9 for 90th percentile)
*/
percentiles?: number[];
/**
* # of buckets to retain in a rolling window
*/
bucketNum?: number;
/**
* time in ms that a circuit should remain broken
*/
circuitDuration?: number;
/**
* number of requests to wait before testing circuit health
*/
waitThreshold?: number;
/**
* % threshold for successful calls.
* If the % of successful calls dips
* below this threshold the circuit will break
*/
threshold?: number;
/**
* time in ms before a service call will timeout
*/
timeout?: number;
/**
* function that returns true if an error should
* be considered a failure (receives the error
* object returned by your command.) This allows
* for non-critical errors to be ignored
* by the circuit breaker
*/
isFailure?: Function;
/**
* time in ms interval between each
* execution of health check function
*/
healthCheckInterval?: number;
/**
* function to call for the health check
* (can be defined also with calling healthCheck function)
*/
healthCheck?: Function;
/**
* function to call for fallback
* (can be defined also with calling fallback function)
*/
fallback?: Function;
/**
* boolean to opt out of check for callback in function.
* This affects the passed in function, health check and fallback
*/
isPromise?: boolean;
/**
* boolean to opt out of check for callback,
* always promisifying in function.
* This affects the passed in function,
* health check and fallback
*/
isFunction?: boolean;
}
interface RetryOpts extends NodeRetryOpts {
/**
* Disabled by default.
*/
enable: boolean;
/**
* Whether ignore some specified errors
*/
bailError?: (e: Error, attempt: number) => boolean;
}
export interface GRPCHelperOpts {
/**
* Service discovery uri
* ex1: `static://1.1.1.1:1234,2.2.2.2:1234`
* ex2: `dns://_grpc._tcp.servicename`
*/
sdUri: string;
/**
* Proto file path, absolute
*/
protoPath: string;
/**
* Package name
*/
packageName: string;
/**
* Service name
*/
serviceName: string;
/**
* Should grpc promised call return full response,
* if true, it will resolve with `status, metadata, peer and message`
* ### Default: false
* Which means only message will be resolved
*/
resolveFullResponse?: boolean;
/**
* grpc options, used in create new instance of grpc client
*/
grpcOpts?: GRPCOpts;
/**
* grpc proto loader options, used in load the proto file
* ### Default:
* ```js
* {
* keepCase: true,
* longs: String,
* enums: String,
* defaults: true,
* oneofs: true
* }
* ```
*/
grpcProtoLoaderOpts?: GRPCProtoLoaderOptions;
/**
* grpc ssl options, used in create new instance of grpc client
*/
sslOpts?: GRPCHelperSslOpts;
/**
* hostname override, override the default hostname,
* by setting following two grpc opts:
* ```
* grpc.ssl_target_name_override
* grpc.default_authority
* ```
*/
hostNameOverride?: string;
/**
* Global timeout is million seconds,
* set 0 to disable
* ### Default: 5000
*/
timeoutInMS?: number;
/**
* Brake opts, passing to the Brakes
*/
brakeOpts?: BrakeOpts;
/**
* Grpc health check func, used in Brakes
*/
healthCheck?: GRPCHelperCheck;
/**
* Whether enable prometheus metrics
* ```
* name: grpc_response_duration_seconds
* type: histogram
* labels: peer,method,code
* ```
*
* ### Default: true
*/
metrics?: boolean;
/**
* Retry options for [async-retry](https://github.com/zeit/async-retry) when error,
* options is actually based on [node-retry](https://github.com/tim-kos/node-retry)
*/
retryOpts?: RetryOpts;
}