@gravity-ui/gateway 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/build/components/grpc.js +21 -12
- package/build/index.js +3 -1
- package/build/utils/grpc-reflection.d.ts +4 -3
- package/build/utils/grpc-reflection.js +20 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -116,7 +116,7 @@ interface GatewayConfig {
|
|
|
116
116
|
validationSchema?: object;
|
|
117
117
|
// Enables encoding of REST path arguments.
|
|
118
118
|
encodePathArgs?: boolean;
|
|
119
|
-
// Configuration for automatic connection re-establishment upon connection error through L3 load balancer (default is
|
|
119
|
+
// Configuration for automatic connection re-establishment upon connection error through L3 load balancer (default is true).
|
|
120
120
|
grpcRecreateService?: boolean;
|
|
121
121
|
}
|
|
122
122
|
```
|
package/build/components/grpc.js
CHANGED
|
@@ -218,12 +218,17 @@ async function refreshCache(actionEndpoint, config, endpointData, grpcOptions, c
|
|
|
218
218
|
return;
|
|
219
219
|
}
|
|
220
220
|
lodash_1.default.set(reflectionCacheStatusMap, [config.protoKey, actionEndpoint], Object.assign(Object.assign({}, cacheStatus), { isRefresh: true }));
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
221
|
+
try {
|
|
222
|
+
const res = await getServiceInstanceReflect(config, endpointData, grpcOptions, credentials, true);
|
|
223
|
+
lodash_1.default.set(reflectionServiceInstancesMap, [config.protoKey, actionEndpoint], Promise.resolve(res));
|
|
224
|
+
lodash_1.default.set(reflectionCacheStatusMap, [config.protoKey, actionEndpoint], {
|
|
225
|
+
time: Date.now() / 1000,
|
|
226
|
+
isRefresh: false,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
catch (e) {
|
|
230
|
+
lodash_1.default.set(reflectionCacheStatusMap, [config.protoKey, actionEndpoint], Object.assign(Object.assign({}, cacheStatus), { isRefresh: false }));
|
|
231
|
+
}
|
|
227
232
|
}
|
|
228
233
|
function getServiceInstanceReflectCached(config, endpointData, grpcOptions, credentials) {
|
|
229
234
|
if (config.reflection === common_1.GrpcReflection.OnEveryRequest) {
|
|
@@ -241,6 +246,9 @@ function getServiceInstanceReflectCached(config, endpointData, grpcOptions, cred
|
|
|
241
246
|
}
|
|
242
247
|
const service = getServiceInstanceReflect(config, endpointData, grpcOptions, credentials);
|
|
243
248
|
lodash_1.default.set(reflectionServiceInstancesMap, cacheKey, service);
|
|
249
|
+
service.catch(() => {
|
|
250
|
+
lodash_1.default.set(reflectionServiceInstancesMap, cacheKey, undefined);
|
|
251
|
+
});
|
|
244
252
|
return service;
|
|
245
253
|
}
|
|
246
254
|
async function getServiceInstanceReflect(config, endpointData, grpcOptions, credentials, isRefreshCache) {
|
|
@@ -250,21 +258,22 @@ async function getServiceInstanceReflect(config, endpointData, grpcOptions, cred
|
|
|
250
258
|
: undefined;
|
|
251
259
|
const isInsecure = config.insecure || endpointInsecure;
|
|
252
260
|
const creds = isInsecure ? credentials.insecure : credentials.secure;
|
|
261
|
+
const endpointGrpcOptions = (0, common_2.isExtendedGrpcActionEndpoint)(endpointData)
|
|
262
|
+
? endpointData.grpcOptions || {}
|
|
263
|
+
: {};
|
|
264
|
+
const combinedGrpcOptions = Object.assign(Object.assign(Object.assign({}, constants_1.DEFAULT_GRPC_OPTIONS), grpcOptions), endpointGrpcOptions);
|
|
253
265
|
let loadedRoot;
|
|
254
266
|
if (config.reflection === common_1.GrpcReflection.OnEveryRequest || isRefreshCache) {
|
|
255
|
-
loadedRoot = await (0, grpc_reflection_1.getReflectionRoot)(actionEndpoint, config.protoKey, creds, isRefreshCache);
|
|
267
|
+
loadedRoot = await (0, grpc_reflection_1.getReflectionRoot)(actionEndpoint, config.protoKey, creds, combinedGrpcOptions, isRefreshCache);
|
|
256
268
|
}
|
|
257
269
|
else {
|
|
258
|
-
loadedRoot = await (0, grpc_reflection_1.getCachedReflectionRoot)(actionEndpoint, config.protoKey, creds);
|
|
270
|
+
loadedRoot = await (0, grpc_reflection_1.getCachedReflectionRoot)(actionEndpoint, config.protoKey, creds, combinedGrpcOptions);
|
|
259
271
|
}
|
|
260
272
|
const descriptor = loadedRoot.toDescriptor('proto3');
|
|
261
273
|
const definition = protoLoader.loadFileDescriptorSetFromObject(descriptor, reflectLoaderOptions);
|
|
262
274
|
const packageObject = grpc.loadPackageDefinition(definition);
|
|
263
275
|
const Service = lodash_1.default.get(packageObject, config.protoKey);
|
|
264
|
-
const
|
|
265
|
-
? endpointData.grpcOptions || {}
|
|
266
|
-
: {};
|
|
267
|
-
const serviceInstance = new Service(actionEndpoint, creds, Object.assign(Object.assign(Object.assign({}, constants_1.DEFAULT_GRPC_OPTIONS), grpcOptions), endpointGrpcOptions));
|
|
276
|
+
const serviceInstance = new Service(actionEndpoint, creds, combinedGrpcOptions);
|
|
268
277
|
return serviceInstance;
|
|
269
278
|
}
|
|
270
279
|
function loadAndCachePackageObject(root, protoPath) {
|
package/build/index.js
CHANGED
|
@@ -48,6 +48,7 @@ function isRestActionConfig(actionConfig) {
|
|
|
48
48
|
return Boolean(actionConfig.method);
|
|
49
49
|
}
|
|
50
50
|
function createApiAction(schema, config, serviceKey, actionName, api, grpcContext) {
|
|
51
|
+
var _a;
|
|
51
52
|
const serviceSchema = schema[serviceKey];
|
|
52
53
|
if (!serviceSchema) {
|
|
53
54
|
throw new config.ErrorConstructor(`Gateway config error. Service "${serviceKey}" have been not found.`, {
|
|
@@ -80,13 +81,14 @@ function createApiAction(schema, config, serviceKey, actionName, api, grpcContex
|
|
|
80
81
|
getAuthHeaders: config.getAuthHeaders,
|
|
81
82
|
}, config.ErrorConstructor);
|
|
82
83
|
}
|
|
84
|
+
const grpcRecreateService = (_a = config.grpcRecreateService) !== null && _a !== void 0 ? _a : true;
|
|
83
85
|
return (0, grpc_1.default)(grpcContext, endpointsConfig, action, serviceKey, actionName, {
|
|
84
86
|
serviceName,
|
|
85
87
|
timeout: config.timeout,
|
|
86
88
|
sendStats: config.sendStats,
|
|
87
89
|
proxyHeaders: config.proxyHeaders,
|
|
88
90
|
grpcOptions: config.grpcOptions,
|
|
89
|
-
grpcRecreateService
|
|
91
|
+
grpcRecreateService,
|
|
90
92
|
getAuthHeaders: config.getAuthHeaders,
|
|
91
93
|
}, config.ErrorConstructor);
|
|
92
94
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { ChannelCredentials } from '@grpc/grpc-js';
|
|
2
|
-
import * as protobufjs from 'protobufjs';
|
|
3
2
|
type DescriptorExtensionProto = string[] | {
|
|
4
3
|
includeProtoRoots: string[];
|
|
5
4
|
filenames: string[];
|
|
@@ -8,20 +7,22 @@ type DescriptorExtensionProto = string[] | {
|
|
|
8
7
|
* @param actionEndpoint
|
|
9
8
|
* @param protoKey
|
|
10
9
|
* @param credentials
|
|
10
|
+
* @param grpcOptions
|
|
11
11
|
* @param descriptorExtensionProto
|
|
12
12
|
* @returns Promise<protobufjs.Root>.
|
|
13
13
|
* use toDescriptor for use with protoLoader.
|
|
14
14
|
* use toJSON for get a JSON descriptor.
|
|
15
15
|
*/
|
|
16
|
-
export declare function getCachedReflectionRoot(actionEndpoint: string, protoKey: string, credentials: ChannelCredentials, descriptorExtensionProto?: DescriptorExtensionProto): Promise<
|
|
16
|
+
export declare function getCachedReflectionRoot(actionEndpoint: string, protoKey: string, credentials: ChannelCredentials, grpcOptions?: object, descriptorExtensionProto?: DescriptorExtensionProto): Promise<any>;
|
|
17
17
|
/**
|
|
18
18
|
* @param actionEndpoint
|
|
19
19
|
* @param protoKey
|
|
20
20
|
* @param credentials
|
|
21
|
+
* @param grpcOptions
|
|
21
22
|
* @param addToCache
|
|
22
23
|
* @returns Promise<protobufjs.Root>.
|
|
23
24
|
* use toDescriptor for use with protoLoader.
|
|
24
25
|
* use toJSON for get a JSON descriptor.
|
|
25
26
|
*/
|
|
26
|
-
export declare function getReflectionRoot(actionEndpoint: string, protoKey: string, credentials: ChannelCredentials, addToCache?: boolean): Promise<
|
|
27
|
+
export declare function getReflectionRoot(actionEndpoint: string, protoKey: string, credentials: ChannelCredentials, grpcOptions?: object, addToCache?: boolean): Promise<any>;
|
|
27
28
|
export {};
|
|
@@ -31,10 +31,10 @@ const lodash_1 = __importDefault(require("lodash"));
|
|
|
31
31
|
const protobufjs = __importStar(require("protobufjs"));
|
|
32
32
|
const proto_path_resolver_1 = require("./proto-path-resolver");
|
|
33
33
|
const reflectionClientsMap = {};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
let
|
|
37
|
-
if (!
|
|
34
|
+
function getCachedClient(actionEndpoint, credentials, grpcOptions, descriptorExtensionProto) {
|
|
35
|
+
const cacheKey = [actionEndpoint, JSON.stringify([grpcOptions, descriptorExtensionProto])];
|
|
36
|
+
let clientWithCache = lodash_1.default.get(reflectionClientsMap, cacheKey);
|
|
37
|
+
if (!clientWithCache) {
|
|
38
38
|
const grpcReflection = require('grpc-reflection-js');
|
|
39
39
|
let descriptorRoot;
|
|
40
40
|
if (descriptorExtensionProto) {
|
|
@@ -47,26 +47,32 @@ function getCachedClient(actionEndpoint, credentials, descriptorExtensionProto)
|
|
|
47
47
|
descriptorRoot.loadSync(descriptorExtensionProto.filenames);
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
client = new grpcReflection.Client(actionEndpoint, credentials,
|
|
51
|
-
|
|
50
|
+
const client = new grpcReflection.Client(actionEndpoint, credentials, grpcOptions, undefined, descriptorRoot);
|
|
51
|
+
clientWithCache = { client, reflectionRootPromiseMap: {} };
|
|
52
|
+
lodash_1.default.set(reflectionClientsMap, cacheKey, clientWithCache);
|
|
52
53
|
}
|
|
53
|
-
return
|
|
54
|
+
return clientWithCache;
|
|
54
55
|
}
|
|
55
56
|
/**
|
|
56
57
|
* @param actionEndpoint
|
|
57
58
|
* @param protoKey
|
|
58
59
|
* @param credentials
|
|
60
|
+
* @param grpcOptions
|
|
59
61
|
* @param descriptorExtensionProto
|
|
60
62
|
* @returns Promise<protobufjs.Root>.
|
|
61
63
|
* use toDescriptor for use with protoLoader.
|
|
62
64
|
* use toJSON for get a JSON descriptor.
|
|
63
65
|
*/
|
|
64
|
-
async function getCachedReflectionRoot(actionEndpoint, protoKey, credentials, descriptorExtensionProto) {
|
|
65
|
-
const client = getCachedClient(actionEndpoint, credentials, descriptorExtensionProto);
|
|
66
|
-
|
|
66
|
+
async function getCachedReflectionRoot(actionEndpoint, protoKey, credentials, grpcOptions, descriptorExtensionProto) {
|
|
67
|
+
const { client, reflectionRootPromiseMap } = getCachedClient(actionEndpoint, credentials, grpcOptions, descriptorExtensionProto);
|
|
68
|
+
const cacheKey = [actionEndpoint, protoKey];
|
|
69
|
+
let cachedRootPromise = lodash_1.default.get(reflectionRootPromiseMap, cacheKey);
|
|
67
70
|
if (!cachedRootPromise) {
|
|
68
71
|
cachedRootPromise = client.fileContainingSymbol(protoKey);
|
|
69
|
-
lodash_1.default.set(reflectionRootPromiseMap,
|
|
72
|
+
lodash_1.default.set(reflectionRootPromiseMap, cacheKey, cachedRootPromise);
|
|
73
|
+
cachedRootPromise.catch(() => {
|
|
74
|
+
lodash_1.default.set(reflectionRootPromiseMap, cacheKey, undefined);
|
|
75
|
+
});
|
|
70
76
|
}
|
|
71
77
|
const loadedRoot = await cachedRootPromise;
|
|
72
78
|
return loadedRoot;
|
|
@@ -76,13 +82,14 @@ exports.getCachedReflectionRoot = getCachedReflectionRoot;
|
|
|
76
82
|
* @param actionEndpoint
|
|
77
83
|
* @param protoKey
|
|
78
84
|
* @param credentials
|
|
85
|
+
* @param grpcOptions
|
|
79
86
|
* @param addToCache
|
|
80
87
|
* @returns Promise<protobufjs.Root>.
|
|
81
88
|
* use toDescriptor for use with protoLoader.
|
|
82
89
|
* use toJSON for get a JSON descriptor.
|
|
83
90
|
*/
|
|
84
|
-
async function getReflectionRoot(actionEndpoint, protoKey, credentials, addToCache) {
|
|
85
|
-
const client = getCachedClient(actionEndpoint, credentials);
|
|
91
|
+
async function getReflectionRoot(actionEndpoint, protoKey, credentials, grpcOptions, addToCache) {
|
|
92
|
+
const { client, reflectionRootPromiseMap } = getCachedClient(actionEndpoint, credentials, grpcOptions);
|
|
86
93
|
const loadedRoot = await client.fileContainingSymbol(protoKey);
|
|
87
94
|
if (addToCache) {
|
|
88
95
|
lodash_1.default.set(reflectionRootPromiseMap, [actionEndpoint, protoKey], Promise.resolve(loadedRoot));
|