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 | import * as os from 'os';
|
30 | import { Duration } from './duration';
|
31 | import {
|
32 | LoadBalancingConfig,
|
33 | validateLoadBalancingConfig,
|
34 | } from './load-balancer';
|
35 |
|
36 | export interface MethodConfigName {
|
37 | service: string;
|
38 | method?: string;
|
39 | }
|
40 |
|
41 | export interface MethodConfig {
|
42 | name: MethodConfigName[];
|
43 | waitForReady?: boolean;
|
44 | timeout?: Duration;
|
45 | maxRequestBytes?: number;
|
46 | maxResponseBytes?: number;
|
47 | }
|
48 |
|
49 | export interface ServiceConfig {
|
50 | loadBalancingPolicy?: string;
|
51 | loadBalancingConfig: LoadBalancingConfig[];
|
52 | methodConfig: MethodConfig[];
|
53 | }
|
54 |
|
55 | export interface ServiceConfigCanaryConfig {
|
56 | clientLanguage?: string[];
|
57 | percentage?: number;
|
58 | clientHostname?: string[];
|
59 | serviceConfig: ServiceConfig;
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | const TIMEOUT_REGEX = /^\d+(\.\d{1,9})?s$/;
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | const CLIENT_LANGUAGE_STRING = 'node';
|
73 |
|
74 | function validateName(obj: any): MethodConfigName {
|
75 | if (!('service' in obj) || typeof obj.service !== 'string') {
|
76 | throw new Error('Invalid method config name: invalid service');
|
77 | }
|
78 | const result: MethodConfigName = {
|
79 | service: obj.service,
|
80 | };
|
81 | if ('method' in obj) {
|
82 | if (typeof obj.method === 'string') {
|
83 | result.method = obj.method;
|
84 | } else {
|
85 | throw new Error('Invalid method config name: invalid method');
|
86 | }
|
87 | }
|
88 | return result;
|
89 | }
|
90 |
|
91 | function validateMethodConfig(obj: any): MethodConfig {
|
92 | const result: MethodConfig = {
|
93 | name: [],
|
94 | };
|
95 | if (!('name' in obj) || !Array.isArray(obj.name)) {
|
96 | throw new Error('Invalid method config: invalid name array');
|
97 | }
|
98 | for (const name of obj.name) {
|
99 | result.name.push(validateName(name));
|
100 | }
|
101 | if ('waitForReady' in obj) {
|
102 | if (typeof obj.waitForReady !== 'boolean') {
|
103 | throw new Error('Invalid method config: invalid waitForReady');
|
104 | }
|
105 | result.waitForReady = obj.waitForReady;
|
106 | }
|
107 | if ('timeout' in obj) {
|
108 | if (typeof obj.timeout === 'object') {
|
109 | if (
|
110 | !('seconds' in obj.timeout) ||
|
111 | !(typeof obj.timeout.seconds === 'number')
|
112 | ) {
|
113 | throw new Error('Invalid method config: invalid timeout.seconds');
|
114 | }
|
115 | if (
|
116 | !('nanos' in obj.timeout) ||
|
117 | !(typeof obj.timeout.nanos === 'number')
|
118 | ) {
|
119 | throw new Error('Invalid method config: invalid timeout.nanos');
|
120 | }
|
121 | result.timeout = obj.timeout;
|
122 | } else if (
|
123 | typeof obj.timeout === 'string' &&
|
124 | TIMEOUT_REGEX.test(obj.timeout)
|
125 | ) {
|
126 | const timeoutParts = obj.timeout
|
127 | .substring(0, obj.timeout.length - 1)
|
128 | .split('.');
|
129 | result.timeout = {
|
130 | seconds: timeoutParts[0] | 0,
|
131 | nanos: (timeoutParts[1] ?? 0) | 0,
|
132 | };
|
133 | } else {
|
134 | throw new Error('Invalid method config: invalid timeout');
|
135 | }
|
136 | }
|
137 | if ('maxRequestBytes' in obj) {
|
138 | if (typeof obj.maxRequestBytes !== 'number') {
|
139 | throw new Error('Invalid method config: invalid maxRequestBytes');
|
140 | }
|
141 | result.maxRequestBytes = obj.maxRequestBytes;
|
142 | }
|
143 | if ('maxResponseBytes' in obj) {
|
144 | if (typeof obj.maxResponseBytes !== 'number') {
|
145 | throw new Error('Invalid method config: invalid maxRequestBytes');
|
146 | }
|
147 | result.maxResponseBytes = obj.maxResponseBytes;
|
148 | }
|
149 | return result;
|
150 | }
|
151 |
|
152 | export function validateServiceConfig(obj: any): ServiceConfig {
|
153 | const result: ServiceConfig = {
|
154 | loadBalancingConfig: [],
|
155 | methodConfig: [],
|
156 | };
|
157 | if ('loadBalancingPolicy' in obj) {
|
158 | if (typeof obj.loadBalancingPolicy === 'string') {
|
159 | result.loadBalancingPolicy = obj.loadBalancingPolicy;
|
160 | } else {
|
161 | throw new Error('Invalid service config: invalid loadBalancingPolicy');
|
162 | }
|
163 | }
|
164 | if ('loadBalancingConfig' in obj) {
|
165 | if (Array.isArray(obj.loadBalancingConfig)) {
|
166 | for (const config of obj.loadBalancingConfig) {
|
167 | result.loadBalancingConfig.push(validateLoadBalancingConfig(config));
|
168 | }
|
169 | } else {
|
170 | throw new Error('Invalid service config: invalid loadBalancingConfig');
|
171 | }
|
172 | }
|
173 | if ('methodConfig' in obj) {
|
174 | if (Array.isArray(obj.methodConfig)) {
|
175 | for (const methodConfig of obj.methodConfig) {
|
176 | result.methodConfig.push(validateMethodConfig(methodConfig));
|
177 | }
|
178 | }
|
179 | }
|
180 |
|
181 | const seenMethodNames: MethodConfigName[] = [];
|
182 | for (const methodConfig of result.methodConfig) {
|
183 | for (const name of methodConfig.name) {
|
184 | for (const seenName of seenMethodNames) {
|
185 | if (
|
186 | name.service === seenName.service &&
|
187 | name.method === seenName.method
|
188 | ) {
|
189 | throw new Error(
|
190 | `Invalid service config: duplicate name ${name.service}/${name.method}`
|
191 | );
|
192 | }
|
193 | }
|
194 | seenMethodNames.push(name);
|
195 | }
|
196 | }
|
197 | return result;
|
198 | }
|
199 |
|
200 | function validateCanaryConfig(obj: any): ServiceConfigCanaryConfig {
|
201 | if (!('serviceConfig' in obj)) {
|
202 | throw new Error('Invalid service config choice: missing service config');
|
203 | }
|
204 | const result: ServiceConfigCanaryConfig = {
|
205 | serviceConfig: validateServiceConfig(obj.serviceConfig),
|
206 | };
|
207 | if ('clientLanguage' in obj) {
|
208 | if (Array.isArray(obj.clientLanguage)) {
|
209 | result.clientLanguage = [];
|
210 | for (const lang of obj.clientLanguage) {
|
211 | if (typeof lang === 'string') {
|
212 | result.clientLanguage.push(lang);
|
213 | } else {
|
214 | throw new Error(
|
215 | 'Invalid service config choice: invalid clientLanguage'
|
216 | );
|
217 | }
|
218 | }
|
219 | } else {
|
220 | throw new Error('Invalid service config choice: invalid clientLanguage');
|
221 | }
|
222 | }
|
223 | if ('clientHostname' in obj) {
|
224 | if (Array.isArray(obj.clientHostname)) {
|
225 | result.clientHostname = [];
|
226 | for (const lang of obj.clientHostname) {
|
227 | if (typeof lang === 'string') {
|
228 | result.clientHostname.push(lang);
|
229 | } else {
|
230 | throw new Error(
|
231 | 'Invalid service config choice: invalid clientHostname'
|
232 | );
|
233 | }
|
234 | }
|
235 | } else {
|
236 | throw new Error('Invalid service config choice: invalid clientHostname');
|
237 | }
|
238 | }
|
239 | if ('percentage' in obj) {
|
240 | if (
|
241 | typeof obj.percentage === 'number' &&
|
242 | 0 <= obj.percentage &&
|
243 | obj.percentage <= 100
|
244 | ) {
|
245 | result.percentage = obj.percentage;
|
246 | } else {
|
247 | throw new Error('Invalid service config choice: invalid percentage');
|
248 | }
|
249 | }
|
250 |
|
251 | const allowedFields = [
|
252 | 'clientLanguage',
|
253 | 'percentage',
|
254 | 'clientHostname',
|
255 | 'serviceConfig',
|
256 | ];
|
257 | for (const field in obj) {
|
258 | if (!allowedFields.includes(field)) {
|
259 | throw new Error(
|
260 | `Invalid service config choice: unexpected field ${field}`
|
261 | );
|
262 | }
|
263 | }
|
264 | return result;
|
265 | }
|
266 |
|
267 | function validateAndSelectCanaryConfig(
|
268 | obj: any,
|
269 | percentage: number
|
270 | ): ServiceConfig {
|
271 | if (!Array.isArray(obj)) {
|
272 | throw new Error('Invalid service config list');
|
273 | }
|
274 | for (const config of obj) {
|
275 | const validatedConfig = validateCanaryConfig(config);
|
276 | |
277 |
|
278 | if (
|
279 | typeof validatedConfig.percentage === 'number' &&
|
280 | percentage > validatedConfig.percentage
|
281 | ) {
|
282 | continue;
|
283 | }
|
284 | if (Array.isArray(validatedConfig.clientHostname)) {
|
285 | let hostnameMatched = false;
|
286 | for (const hostname of validatedConfig.clientHostname) {
|
287 | if (hostname === os.hostname()) {
|
288 | hostnameMatched = true;
|
289 | }
|
290 | }
|
291 | if (!hostnameMatched) {
|
292 | continue;
|
293 | }
|
294 | }
|
295 | if (Array.isArray(validatedConfig.clientLanguage)) {
|
296 | let languageMatched = false;
|
297 | for (const language of validatedConfig.clientLanguage) {
|
298 | if (language === CLIENT_LANGUAGE_STRING) {
|
299 | languageMatched = true;
|
300 | }
|
301 | }
|
302 | if (!languageMatched) {
|
303 | continue;
|
304 | }
|
305 | }
|
306 | return validatedConfig.serviceConfig;
|
307 | }
|
308 | throw new Error('No matching service config found');
|
309 | }
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 | export function extractAndSelectServiceConfig(
|
321 | txtRecord: string[][],
|
322 | percentage: number
|
323 | ): ServiceConfig | null {
|
324 | for (const record of txtRecord) {
|
325 | if (record.length > 0 && record[0].startsWith('grpc_config=')) {
|
326 | |
327 |
|
328 | const recordString = record.join('').substring('grpc_config='.length);
|
329 | const recordJson: any = JSON.parse(recordString);
|
330 | return validateAndSelectCanaryConfig(recordJson, percentage);
|
331 | }
|
332 | }
|
333 | return null;
|
334 | }
|