service-config.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. "use strict";
  2. /*
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.extractAndSelectServiceConfig = exports.validateServiceConfig = exports.validateRetryThrottling = void 0;
  20. /* This file implements gRFC A2 and the service config spec:
  21. * https://github.com/grpc/proposal/blob/master/A2-service-configs-in-dns.md
  22. * https://github.com/grpc/grpc/blob/master/doc/service_config.md. Each
  23. * function here takes an object with unknown structure and returns its
  24. * specific object type if the input has the right structure, and throws an
  25. * error otherwise. */
  26. /* The any type is purposely used here. All functions validate their input at
  27. * runtime */
  28. /* eslint-disable @typescript-eslint/no-explicit-any */
  29. const os = require("os");
  30. const constants_1 = require("./constants");
  31. /**
  32. * Recognizes a number with up to 9 digits after the decimal point, followed by
  33. * an "s", representing a number of seconds.
  34. */
  35. const DURATION_REGEX = /^\d+(\.\d{1,9})?s$/;
  36. /**
  37. * Client language name used for determining whether this client matches a
  38. * `ServiceConfigCanaryConfig`'s `clientLanguage` list.
  39. */
  40. const CLIENT_LANGUAGE_STRING = 'node';
  41. function validateName(obj) {
  42. // In this context, and unset field and '' are considered the same
  43. if ('service' in obj && obj.service !== '') {
  44. if (typeof obj.service !== 'string') {
  45. throw new Error(`Invalid method config name: invalid service: expected type string, got ${typeof obj.service}`);
  46. }
  47. if ('method' in obj && obj.method !== '') {
  48. if (typeof obj.method !== 'string') {
  49. throw new Error(`Invalid method config name: invalid method: expected type string, got ${typeof obj.service}`);
  50. }
  51. return {
  52. service: obj.service,
  53. method: obj.method,
  54. };
  55. }
  56. else {
  57. return {
  58. service: obj.service,
  59. };
  60. }
  61. }
  62. else {
  63. if ('method' in obj && obj.method !== undefined) {
  64. throw new Error(`Invalid method config name: method set with empty or unset service`);
  65. }
  66. return {};
  67. }
  68. }
  69. function validateRetryPolicy(obj) {
  70. if (!('maxAttempts' in obj) ||
  71. !Number.isInteger(obj.maxAttempts) ||
  72. obj.maxAttempts < 2) {
  73. throw new Error('Invalid method config retry policy: maxAttempts must be an integer at least 2');
  74. }
  75. if (!('initialBackoff' in obj) ||
  76. typeof obj.initialBackoff !== 'string' ||
  77. !DURATION_REGEX.test(obj.initialBackoff)) {
  78. throw new Error('Invalid method config retry policy: initialBackoff must be a string consisting of a positive integer or decimal followed by s');
  79. }
  80. if (!('maxBackoff' in obj) ||
  81. typeof obj.maxBackoff !== 'string' ||
  82. !DURATION_REGEX.test(obj.maxBackoff)) {
  83. throw new Error('Invalid method config retry policy: maxBackoff must be a string consisting of a positive integer or decimal followed by s');
  84. }
  85. if (!('backoffMultiplier' in obj) ||
  86. typeof obj.backoffMultiplier !== 'number' ||
  87. obj.backoffMultiplier <= 0) {
  88. throw new Error('Invalid method config retry policy: backoffMultiplier must be a number greater than 0');
  89. }
  90. if (!('retryableStatusCodes' in obj && Array.isArray(obj.retryableStatusCodes))) {
  91. throw new Error('Invalid method config retry policy: retryableStatusCodes is required');
  92. }
  93. if (obj.retryableStatusCodes.length === 0) {
  94. throw new Error('Invalid method config retry policy: retryableStatusCodes must be non-empty');
  95. }
  96. for (const value of obj.retryableStatusCodes) {
  97. if (typeof value === 'number') {
  98. if (!Object.values(constants_1.Status).includes(value)) {
  99. throw new Error('Invalid method config retry policy: retryableStatusCodes value not in status code range');
  100. }
  101. }
  102. else if (typeof value === 'string') {
  103. if (!Object.values(constants_1.Status).includes(value.toUpperCase())) {
  104. throw new Error('Invalid method config retry policy: retryableStatusCodes value not a status code name');
  105. }
  106. }
  107. else {
  108. throw new Error('Invalid method config retry policy: retryableStatusCodes value must be a string or number');
  109. }
  110. }
  111. return {
  112. maxAttempts: obj.maxAttempts,
  113. initialBackoff: obj.initialBackoff,
  114. maxBackoff: obj.maxBackoff,
  115. backoffMultiplier: obj.backoffMultiplier,
  116. retryableStatusCodes: obj.retryableStatusCodes,
  117. };
  118. }
  119. function validateHedgingPolicy(obj) {
  120. if (!('maxAttempts' in obj) ||
  121. !Number.isInteger(obj.maxAttempts) ||
  122. obj.maxAttempts < 2) {
  123. throw new Error('Invalid method config hedging policy: maxAttempts must be an integer at least 2');
  124. }
  125. if ('hedgingDelay' in obj &&
  126. (typeof obj.hedgingDelay !== 'string' ||
  127. !DURATION_REGEX.test(obj.hedgingDelay))) {
  128. throw new Error('Invalid method config hedging policy: hedgingDelay must be a string consisting of a positive integer followed by s');
  129. }
  130. if ('nonFatalStatusCodes' in obj && Array.isArray(obj.nonFatalStatusCodes)) {
  131. for (const value of obj.nonFatalStatusCodes) {
  132. if (typeof value === 'number') {
  133. if (!Object.values(constants_1.Status).includes(value)) {
  134. throw new Error('Invalid method config hedging policy: nonFatalStatusCodes value not in status code range');
  135. }
  136. }
  137. else if (typeof value === 'string') {
  138. if (!Object.values(constants_1.Status).includes(value.toUpperCase())) {
  139. throw new Error('Invalid method config hedging policy: nonFatalStatusCodes value not a status code name');
  140. }
  141. }
  142. else {
  143. throw new Error('Invalid method config hedging policy: nonFatalStatusCodes value must be a string or number');
  144. }
  145. }
  146. }
  147. const result = {
  148. maxAttempts: obj.maxAttempts,
  149. };
  150. if (obj.hedgingDelay) {
  151. result.hedgingDelay = obj.hedgingDelay;
  152. }
  153. if (obj.nonFatalStatusCodes) {
  154. result.nonFatalStatusCodes = obj.nonFatalStatusCodes;
  155. }
  156. return result;
  157. }
  158. function validateMethodConfig(obj) {
  159. var _a;
  160. const result = {
  161. name: [],
  162. };
  163. if (!('name' in obj) || !Array.isArray(obj.name)) {
  164. throw new Error('Invalid method config: invalid name array');
  165. }
  166. for (const name of obj.name) {
  167. result.name.push(validateName(name));
  168. }
  169. if ('waitForReady' in obj) {
  170. if (typeof obj.waitForReady !== 'boolean') {
  171. throw new Error('Invalid method config: invalid waitForReady');
  172. }
  173. result.waitForReady = obj.waitForReady;
  174. }
  175. if ('timeout' in obj) {
  176. if (typeof obj.timeout === 'object') {
  177. if (!('seconds' in obj.timeout) ||
  178. !(typeof obj.timeout.seconds === 'number')) {
  179. throw new Error('Invalid method config: invalid timeout.seconds');
  180. }
  181. if (!('nanos' in obj.timeout) ||
  182. !(typeof obj.timeout.nanos === 'number')) {
  183. throw new Error('Invalid method config: invalid timeout.nanos');
  184. }
  185. result.timeout = obj.timeout;
  186. }
  187. else if (typeof obj.timeout === 'string' &&
  188. DURATION_REGEX.test(obj.timeout)) {
  189. const timeoutParts = obj.timeout
  190. .substring(0, obj.timeout.length - 1)
  191. .split('.');
  192. result.timeout = {
  193. seconds: timeoutParts[0] | 0,
  194. nanos: ((_a = timeoutParts[1]) !== null && _a !== void 0 ? _a : 0) | 0,
  195. };
  196. }
  197. else {
  198. throw new Error('Invalid method config: invalid timeout');
  199. }
  200. }
  201. if ('maxRequestBytes' in obj) {
  202. if (typeof obj.maxRequestBytes !== 'number') {
  203. throw new Error('Invalid method config: invalid maxRequestBytes');
  204. }
  205. result.maxRequestBytes = obj.maxRequestBytes;
  206. }
  207. if ('maxResponseBytes' in obj) {
  208. if (typeof obj.maxResponseBytes !== 'number') {
  209. throw new Error('Invalid method config: invalid maxRequestBytes');
  210. }
  211. result.maxResponseBytes = obj.maxResponseBytes;
  212. }
  213. if ('retryPolicy' in obj) {
  214. if ('hedgingPolicy' in obj) {
  215. throw new Error('Invalid method config: retryPolicy and hedgingPolicy cannot both be specified');
  216. }
  217. else {
  218. result.retryPolicy = validateRetryPolicy(obj.retryPolicy);
  219. }
  220. }
  221. else if ('hedgingPolicy' in obj) {
  222. result.hedgingPolicy = validateHedgingPolicy(obj.hedgingPolicy);
  223. }
  224. return result;
  225. }
  226. function validateRetryThrottling(obj) {
  227. if (!('maxTokens' in obj) ||
  228. typeof obj.maxTokens !== 'number' ||
  229. obj.maxTokens <= 0 ||
  230. obj.maxTokens > 1000) {
  231. throw new Error('Invalid retryThrottling: maxTokens must be a number in (0, 1000]');
  232. }
  233. if (!('tokenRatio' in obj) ||
  234. typeof obj.tokenRatio !== 'number' ||
  235. obj.tokenRatio <= 0) {
  236. throw new Error('Invalid retryThrottling: tokenRatio must be a number greater than 0');
  237. }
  238. return {
  239. maxTokens: +obj.maxTokens.toFixed(3),
  240. tokenRatio: +obj.tokenRatio.toFixed(3),
  241. };
  242. }
  243. exports.validateRetryThrottling = validateRetryThrottling;
  244. function validateLoadBalancingConfig(obj) {
  245. if (!(typeof obj === 'object' && obj !== null)) {
  246. throw new Error(`Invalid loadBalancingConfig: unexpected type ${typeof obj}`);
  247. }
  248. const keys = Object.keys(obj);
  249. if (keys.length > 1) {
  250. throw new Error(`Invalid loadBalancingConfig: unexpected multiple keys ${keys}`);
  251. }
  252. if (keys.length === 0) {
  253. throw new Error('Invalid loadBalancingConfig: load balancing policy name required');
  254. }
  255. return {
  256. [keys[0]]: obj[keys[0]],
  257. };
  258. }
  259. function validateServiceConfig(obj) {
  260. const result = {
  261. loadBalancingConfig: [],
  262. methodConfig: [],
  263. };
  264. if ('loadBalancingPolicy' in obj) {
  265. if (typeof obj.loadBalancingPolicy === 'string') {
  266. result.loadBalancingPolicy = obj.loadBalancingPolicy;
  267. }
  268. else {
  269. throw new Error('Invalid service config: invalid loadBalancingPolicy');
  270. }
  271. }
  272. if ('loadBalancingConfig' in obj) {
  273. if (Array.isArray(obj.loadBalancingConfig)) {
  274. for (const config of obj.loadBalancingConfig) {
  275. result.loadBalancingConfig.push(validateLoadBalancingConfig(config));
  276. }
  277. }
  278. else {
  279. throw new Error('Invalid service config: invalid loadBalancingConfig');
  280. }
  281. }
  282. if ('methodConfig' in obj) {
  283. if (Array.isArray(obj.methodConfig)) {
  284. for (const methodConfig of obj.methodConfig) {
  285. result.methodConfig.push(validateMethodConfig(methodConfig));
  286. }
  287. }
  288. }
  289. if ('retryThrottling' in obj) {
  290. result.retryThrottling = validateRetryThrottling(obj.retryThrottling);
  291. }
  292. // Validate method name uniqueness
  293. const seenMethodNames = [];
  294. for (const methodConfig of result.methodConfig) {
  295. for (const name of methodConfig.name) {
  296. for (const seenName of seenMethodNames) {
  297. if (name.service === seenName.service &&
  298. name.method === seenName.method) {
  299. throw new Error(`Invalid service config: duplicate name ${name.service}/${name.method}`);
  300. }
  301. }
  302. seenMethodNames.push(name);
  303. }
  304. }
  305. return result;
  306. }
  307. exports.validateServiceConfig = validateServiceConfig;
  308. function validateCanaryConfig(obj) {
  309. if (!('serviceConfig' in obj)) {
  310. throw new Error('Invalid service config choice: missing service config');
  311. }
  312. const result = {
  313. serviceConfig: validateServiceConfig(obj.serviceConfig),
  314. };
  315. if ('clientLanguage' in obj) {
  316. if (Array.isArray(obj.clientLanguage)) {
  317. result.clientLanguage = [];
  318. for (const lang of obj.clientLanguage) {
  319. if (typeof lang === 'string') {
  320. result.clientLanguage.push(lang);
  321. }
  322. else {
  323. throw new Error('Invalid service config choice: invalid clientLanguage');
  324. }
  325. }
  326. }
  327. else {
  328. throw new Error('Invalid service config choice: invalid clientLanguage');
  329. }
  330. }
  331. if ('clientHostname' in obj) {
  332. if (Array.isArray(obj.clientHostname)) {
  333. result.clientHostname = [];
  334. for (const lang of obj.clientHostname) {
  335. if (typeof lang === 'string') {
  336. result.clientHostname.push(lang);
  337. }
  338. else {
  339. throw new Error('Invalid service config choice: invalid clientHostname');
  340. }
  341. }
  342. }
  343. else {
  344. throw new Error('Invalid service config choice: invalid clientHostname');
  345. }
  346. }
  347. if ('percentage' in obj) {
  348. if (typeof obj.percentage === 'number' &&
  349. 0 <= obj.percentage &&
  350. obj.percentage <= 100) {
  351. result.percentage = obj.percentage;
  352. }
  353. else {
  354. throw new Error('Invalid service config choice: invalid percentage');
  355. }
  356. }
  357. // Validate that no unexpected fields are present
  358. const allowedFields = [
  359. 'clientLanguage',
  360. 'percentage',
  361. 'clientHostname',
  362. 'serviceConfig',
  363. ];
  364. for (const field in obj) {
  365. if (!allowedFields.includes(field)) {
  366. throw new Error(`Invalid service config choice: unexpected field ${field}`);
  367. }
  368. }
  369. return result;
  370. }
  371. function validateAndSelectCanaryConfig(obj, percentage) {
  372. if (!Array.isArray(obj)) {
  373. throw new Error('Invalid service config list');
  374. }
  375. for (const config of obj) {
  376. const validatedConfig = validateCanaryConfig(config);
  377. /* For each field, we check if it is present, then only discard the
  378. * config if the field value does not match the current client */
  379. if (typeof validatedConfig.percentage === 'number' &&
  380. percentage > validatedConfig.percentage) {
  381. continue;
  382. }
  383. if (Array.isArray(validatedConfig.clientHostname)) {
  384. let hostnameMatched = false;
  385. for (const hostname of validatedConfig.clientHostname) {
  386. if (hostname === os.hostname()) {
  387. hostnameMatched = true;
  388. }
  389. }
  390. if (!hostnameMatched) {
  391. continue;
  392. }
  393. }
  394. if (Array.isArray(validatedConfig.clientLanguage)) {
  395. let languageMatched = false;
  396. for (const language of validatedConfig.clientLanguage) {
  397. if (language === CLIENT_LANGUAGE_STRING) {
  398. languageMatched = true;
  399. }
  400. }
  401. if (!languageMatched) {
  402. continue;
  403. }
  404. }
  405. return validatedConfig.serviceConfig;
  406. }
  407. throw new Error('No matching service config found');
  408. }
  409. /**
  410. * Find the "grpc_config" record among the TXT records, parse its value as JSON, validate its contents,
  411. * and select a service config with selection fields that all match this client. Most of these steps
  412. * can fail with an error; the caller must handle any errors thrown this way.
  413. * @param txtRecord The TXT record array that is output from a successful call to dns.resolveTxt
  414. * @param percentage A number chosen from the range [0, 100) that is used to select which config to use
  415. * @return The service configuration to use, given the percentage value, or null if the service config
  416. * data has a valid format but none of the options match the current client.
  417. */
  418. function extractAndSelectServiceConfig(txtRecord, percentage) {
  419. for (const record of txtRecord) {
  420. if (record.length > 0 && record[0].startsWith('grpc_config=')) {
  421. /* Treat the list of strings in this record as a single string and remove
  422. * "grpc_config=" from the beginning. The rest should be a JSON string */
  423. const recordString = record.join('').substring('grpc_config='.length);
  424. const recordJson = JSON.parse(recordString);
  425. return validateAndSelectCanaryConfig(recordJson, percentage);
  426. }
  427. }
  428. return null;
  429. }
  430. exports.extractAndSelectServiceConfig = extractAndSelectServiceConfig;
  431. //# sourceMappingURL=service-config.js.map