@be-link/ecommerce-product-service-node-sdk 0.1.15 → 0.1.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@be-link/ecommerce-product-service-node-sdk",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "description": "EcommerceProductService Node.js SDK",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/utils/http.d.ts CHANGED
@@ -5,6 +5,7 @@ declare module '@fastify/request-context' {
5
5
  pandoraUserId?: string;
6
6
  beLinkUserId?: string;
7
7
  pandoraRoleId?: string;
8
+ realIp?: string;
8
9
  }
9
10
  }
10
11
  export declare function callApi<T extends (...args: any[]) => Promise<any>>(url: string, request?: Parameters<T>[0]): Promise<Awaited<ReturnType<T>>>;
package/utils/http.js CHANGED
@@ -1,37 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
36
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
4
  };
@@ -41,88 +8,100 @@ const axios_1 = __importDefault(require("axios"));
41
8
  const uuid_1 = require("uuid");
42
9
  const axios_retry_1 = __importDefault(require("axios-retry"));
43
10
  const request_context_1 = require("@fastify/request-context");
44
- const safe_stable_stringify_1 = __importDefault(require("safe-stable-stringify"));
45
11
  const http_1 = __importDefault(require("http"));
46
12
  const https_1 = __importDefault(require("https"));
47
- // 配置连接池
13
+ // HTTP 连接池配置 - 支持 2000 QPS(响应时间 2秒)
14
+ const HTTP_CONFIG = {
15
+ maxSockets: 3000, // 每个 host 最大并发连接数
16
+ maxFreeSockets: 1000, // 保持空闲连接数(减少连接重建开销)
17
+ maxTotalSockets: 10000, // 所有 host 总连接数上限(支持多个服务同时调用)
18
+ keepAliveMsecs: 60000, // 保持连接60秒
19
+ timeout: 5000, // socket超时5秒
20
+ requestTimeout: 5000, // 请求超时5秒
21
+ retries: 0, // 不重试(失败直接返回)
22
+ retryBaseDelay: 200, // 基础重试延迟200ms
23
+ };
24
+ // 配置 HTTP/HTTPS Agent 以支持高并发连接池和 keepAlive
48
25
  const httpAgent = new http_1.default.Agent({
49
26
  keepAlive: true,
50
- keepAliveMsecs: 30000, // 保持连接30秒
51
- maxSockets: 100, // 每个主机最大并发连接数
52
- maxFreeSockets: 10, // 空闲连接池大小
53
- timeout: 60000, // socket 超时时间
27
+ keepAliveMsecs: HTTP_CONFIG.keepAliveMsecs,
28
+ maxSockets: HTTP_CONFIG.maxSockets,
29
+ maxFreeSockets: HTTP_CONFIG.maxFreeSockets,
30
+ maxTotalSockets: HTTP_CONFIG.maxTotalSockets,
31
+ timeout: HTTP_CONFIG.timeout,
32
+ scheduling: 'lifo', // 后进先出调度(优先复用热连接,提高连接复用效率)
54
33
  });
55
34
  const httpsAgent = new https_1.default.Agent({
56
35
  keepAlive: true,
57
- keepAliveMsecs: 30000,
58
- maxSockets: 100,
59
- maxFreeSockets: 10,
60
- timeout: 60000,
36
+ keepAliveMsecs: HTTP_CONFIG.keepAliveMsecs,
37
+ maxSockets: HTTP_CONFIG.maxSockets,
38
+ maxFreeSockets: HTTP_CONFIG.maxFreeSockets,
39
+ maxTotalSockets: HTTP_CONFIG.maxTotalSockets,
40
+ timeout: HTTP_CONFIG.timeout,
41
+ scheduling: 'lifo', // 后进先出调度(优先复用热连接,提高连接复用效率)
61
42
  });
43
+ // 配置 axios 默认使用这些 agent
44
+ axios_1.default.defaults.httpAgent = httpAgent;
45
+ axios_1.default.defaults.httpsAgent = httpsAgent;
46
+ axios_1.default.defaults.timeout = HTTP_CONFIG.requestTimeout;
47
+ axios_1.default.defaults.maxRedirects = 3; // 限制重定向次数
48
+ axios_1.default.defaults.maxContentLength = 50 * 1024 * 1024; // 50MB 最大响应大小
49
+ // 高并发场景下的智能重试配置
62
50
  (0, axios_retry_1.default)(axios_1.default, {
63
- retries: 1,
51
+ retries: HTTP_CONFIG.retries,
64
52
  retryCondition(error) {
65
- return error.response?.status === 502;
53
+ // 重试临时错误和特定的网络错误
54
+ const status = error.response?.status;
55
+ const isNetworkError = error.code === 'ECONNRESET' ||
56
+ error.code === 'ETIMEDOUT' ||
57
+ error.code === 'ECONNREFUSED' ||
58
+ error.code === 'ENOTFOUND' ||
59
+ error.code === 'ENETUNREACH' ||
60
+ error.code === 'EAI_AGAIN';
61
+ const isServerError = status === 502 || status === 503 || status === 504;
62
+ const isRateLimited = status === 429;
63
+ const isTimeout = error.code === 'ECONNABORTED' || error.message?.includes('timeout');
64
+ return isNetworkError || isServerError || isRateLimited || isTimeout;
66
65
  },
67
- retryDelay: (retryCount) => {
68
- console.info(`retryCount: ${retryCount}, retryDelay: ${retryCount * 500}`);
69
- return retryCount * 500;
70
- },
71
- onRetry(retryCount, error, requestConfig) {
72
- console.info(`retryCount: ${retryCount}, onRetry: ${error.message}, requestHeader: ${(0, safe_stable_stringify_1.default)(requestConfig.headers)}`);
66
+ retryDelay: (retryCount, error) => {
67
+ // 指数退避 + 抖动
68
+ const status = error.response?.status;
69
+ // 如果是限流错误,延迟更长
70
+ if (status === 429) {
71
+ const retryAfter = error.response?.headers['retry-after'];
72
+ if (retryAfter) {
73
+ return parseInt(retryAfter) * 1000;
74
+ }
75
+ return 1000 + Math.random() * 1000; // 1-2秒
76
+ }
77
+ // 指数退避:200ms, 400ms, 800ms...
78
+ const exponentialDelay = HTTP_CONFIG.retryBaseDelay * Math.pow(2, retryCount - 1);
79
+ const jitter = Math.random() * 100; // 0-100ms 抖动
80
+ return Math.min(exponentialDelay + jitter, 3000); // 最多延迟3秒
73
81
  },
82
+ shouldResetTimeout: true, // 重试时重置超时
74
83
  });
75
84
  async function callApi(url, request) {
76
- const requestId = request_context_1.requestContext.get('requestId') || request_context_1.requestContext.get('traceMessageId') || (0, uuid_1.v4)();
77
- const pandoraUserId = request_context_1.requestContext.get('pandoraUserId') || '';
78
- const beLinkUserId = request_context_1.requestContext.get('beLinkUserId') || '';
79
- const pandoraRoleId = request_context_1.requestContext.get('pandoraRoleId') || '';
80
- try {
81
- console.info(`准备发起ecommerce-product-service请求[${requestId}]: ${url}, 参数: ${(0, safe_stable_stringify_1.default)(request)}`);
82
- const response = await axios_1.default.post(url, request || {}, {
83
- headers: {
84
- 'x-request-id': requestId,
85
- 'x-belink-pandora-userid': pandoraUserId,
86
- 'x-belink-userid': beLinkUserId,
87
- 'x-belink-pandora-roleid': pandoraRoleId,
88
- },
89
- timeout: 3000,
90
- httpAgent,
91
- httpsAgent,
92
- });
93
- const responseData = response.data;
94
- return responseData.data;
95
- }
96
- catch (error) {
97
- const axiosError = error;
98
- if (axiosError.response) {
99
- const response = axiosError.response;
100
- const data = response.data;
101
- console.error(`ecommerce-product-service 异常: ${axiosError.message},requestId: ${requestId}`);
102
- console.info('响应信息', data.message);
103
- console.error('异常堆栈', (0, safe_stable_stringify_1.default)(error.stack));
104
- throw error;
105
- }
106
- // 调用dns模块解析url
107
- const dns = await Promise.resolve().then(() => __importStar(require('dns')));
108
- const dnsPromise = new Promise((resolve, reject) => {
109
- const lookupRes = dns.lookup(url, (err, address) => {
110
- if (err) {
111
- console.error(err.message);
112
- reject(err);
113
- }
114
- console.info(`lookup: ${(0, safe_stable_stringify_1.default)(lookupRes)}`);
115
- resolve(address);
116
- });
117
- });
118
- try {
119
- const address = await dnsPromise;
120
- console.info(`address: ${(0, safe_stable_stringify_1.default)(address)}`);
121
- }
122
- catch (error) {
123
- console.info(`error: ${(0, safe_stable_stringify_1.default)(error)}`);
124
- }
125
- console.error(`ecommerce-product-service 未知异常: ${axiosError.message}`, error.stack);
126
- throw error;
127
- }
85
+ // 获取请求上下文
86
+ const ctx = request_context_1.requestContext.get('requestId') ? request_context_1.requestContext : null;
87
+ const requestId = ctx?.get('requestId') || ctx?.get('traceMessageId') || (0, uuid_1.v4)();
88
+ const pandoraUserId = ctx?.get('pandoraUserId') || '';
89
+ const beLinkUserId = ctx?.get('beLinkUserId') || '';
90
+ const pandoraRoleId = ctx?.get('pandoraRoleId') || '';
91
+ const realIp = ctx?.get('realIp') || '';
92
+ const response = await axios_1.default.post(url, request, {
93
+ headers: {
94
+ 'x-request-id': requestId,
95
+ 'x-belink-pandora-userid': pandoraUserId,
96
+ 'x-belink-userid': beLinkUserId,
97
+ 'x-belink-pandora-roleid': pandoraRoleId,
98
+ 'x-real-ip': realIp,
99
+ Connection: 'keep-alive',
100
+ },
101
+ timeout: HTTP_CONFIG.requestTimeout,
102
+ httpAgent,
103
+ httpsAgent,
104
+ });
105
+ const responseData = response.data;
106
+ return responseData.data;
128
107
  }