@be-link/ecommerce-promotion-service-node-sdk 0.1.15 → 0.1.17

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-promotion-service-node-sdk",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "EcommercePromotionService Node.js SDK",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/utils/http.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  declare module '@fastify/request-context' {
2
2
  interface RequestContextData {
3
- requestId: string;
3
+ requestId?: string;
4
4
  traceMessageId?: string;
5
5
  pandoraUserId?: string;
6
6
  beLinkUserId?: string;
@@ -9,11 +9,3 @@ declare module '@fastify/request-context' {
9
9
  }
10
10
  }
11
11
  export declare function callApi<T extends (...args: any[]) => Promise<any>>(url: string, request?: Parameters<T>[0]): Promise<Awaited<ReturnType<T>>>;
12
- export declare function getHttpStats(): {
13
- activeRequests: number;
14
- totalRequests: number;
15
- errorCount: number;
16
- timeoutCount: number;
17
- errorRate: string;
18
- timeoutRate: string;
19
- };
package/utils/http.js CHANGED
@@ -4,22 +4,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.callApi = callApi;
7
- exports.getHttpStats = getHttpStats;
8
7
  const axios_1 = __importDefault(require("axios"));
9
8
  const uuid_1 = require("uuid");
10
9
  const axios_retry_1 = __importDefault(require("axios-retry"));
11
10
  const request_context_1 = require("@fastify/request-context");
12
11
  const http_1 = __importDefault(require("http"));
13
12
  const https_1 = __importDefault(require("https"));
14
- const safe_stable_stringify_1 = __importDefault(require("safe-stable-stringify"));
15
- // 针对高并发优化配置 - 极限性能模式
13
+ // HTTP 连接池配置 - 支持 2000 QPS(响应时间 2秒)
16
14
  const HTTP_CONFIG = {
17
- maxSockets: Infinity, // 不限制每个主机的socket连接数(极限并发模式)
18
- maxFreeSockets: 2000, // 保留2000个空闲连接(减少连接重建开销)
19
- maxTotalSockets: Infinity, // 不限制总socket数(避免连接池成为瓶颈)
15
+ maxSockets: 3000, // 每个 host 最大并发连接数
16
+ maxFreeSockets: 1000, // 保持空闲连接数(减少连接重建开销)
17
+ maxTotalSockets: 10000, // 所有 host 总连接数上限(支持多个服务同时调用)
20
18
  keepAliveMsecs: 60000, // 保持连接60秒
21
- timeout: 6000, // socket超时6秒(给予充足时间)
22
- requestTimeout: 6000, // 请求超时6秒(避免正常请求超时)
19
+ timeout: 5000, // socket超时5秒
20
+ requestTimeout: 5000, // 请求超时5秒
23
21
  retries: 0, // 不重试(失败直接返回)
24
22
  retryBaseDelay: 200, // 基础重试延迟200ms
25
23
  };
@@ -83,11 +81,6 @@ axios_1.default.defaults.maxContentLength = 50 * 1024 * 1024; // 50MB 最大响
83
81
  },
84
82
  shouldResetTimeout: true, // 重试时重置超时
85
83
  });
86
- // 性能监控变量
87
- let activeRequests = 0;
88
- let totalRequests = 0;
89
- let timeoutCount = 0;
90
- let errorCount = 0;
91
84
  async function callApi(url, request) {
92
85
  // 获取请求上下文
93
86
  const ctx = request_context_1.requestContext.get('requestId') ? request_context_1.requestContext : null;
@@ -96,103 +89,20 @@ async function callApi(url, request) {
96
89
  const beLinkUserId = ctx?.get('beLinkUserId') || '';
97
90
  const pandoraRoleId = ctx?.get('pandoraRoleId') || '';
98
91
  const realIp = ctx?.get('realIp') || '';
99
- // 性能监控
100
- const startTime = Date.now();
101
- activeRequests++;
102
- totalRequests++;
103
- const currentRequestId = totalRequests;
104
- try {
105
- const response = await axios_1.default.post(url, request, {
106
- headers: {
107
- 'x-request-id': requestId,
108
- 'x-belink-pandora-userid': pandoraUserId,
109
- 'x-belink-userid': beLinkUserId,
110
- 'x-belink-pandora-roleid': pandoraRoleId,
111
- 'x-real-ip': realIp,
112
- Connection: 'keep-alive',
113
- },
114
- timeout: HTTP_CONFIG.requestTimeout,
115
- httpAgent,
116
- httpsAgent,
117
- });
118
- const duration = Date.now() - startTime;
119
- // 记录慢请求(超过3秒)
120
- if (duration > 3000) {
121
- console.log((0, safe_stable_stringify_1.default)({
122
- message: '慢请求警告',
123
- url,
124
- duration,
125
- activeRequests,
126
- requestId: currentRequestId,
127
- type: 'slow_request',
128
- }));
129
- }
130
- const responseData = response.data;
131
- return responseData.data;
132
- }
133
- catch (error) {
134
- const axiosError = error;
135
- const duration = Date.now() - startTime;
136
- errorCount++;
137
- // 记录详细的错误信息用于诊断
138
- const errorInfo = {
139
- message: '请求失败',
140
- url,
141
- duration,
142
- activeRequests,
143
- totalRequests: currentRequestId,
144
- errorMessage: axiosError.message,
145
- errorCode: axiosError.code,
146
- type: 'http_request_failed',
147
- };
148
- // 添加超时相关信息
149
- if (axiosError.code === 'ECONNABORTED' || axiosError.message?.includes('timeout')) {
150
- errorInfo.isTimeout = true;
151
- errorInfo.timeoutValue = HTTP_CONFIG.requestTimeout;
152
- timeoutCount++;
153
- }
154
- // 添加重试信息
155
- if (axiosError.config && 'axios-retry' in axiosError.config) {
156
- errorInfo.retryCount = axiosError.config['axios-retry']?.retryCount || 0;
157
- }
158
- console.log((0, safe_stable_stringify_1.default)(errorInfo));
159
- if (axiosError.response) {
160
- const response = axiosError.response;
161
- const data = response.data;
162
- console.log((0, safe_stable_stringify_1.default)({
163
- message: '响应异常',
164
- status: response.status,
165
- errorMessage: data?.message || axiosError.message,
166
- type: 'http_response_error',
167
- }));
168
- }
169
- throw error;
170
- }
171
- finally {
172
- activeRequests--;
173
- // 每1000个请求输出一次统计信息
174
- if (totalRequests % 1000 === 0) {
175
- console.log((0, safe_stable_stringify_1.default)({
176
- message: 'HTTP客户端统计',
177
- totalRequests,
178
- activeRequests,
179
- errorCount,
180
- timeoutCount,
181
- errorRate: ((errorCount / totalRequests) * 100).toFixed(2) + '%',
182
- timeoutRate: ((timeoutCount / totalRequests) * 100).toFixed(2) + '%',
183
- type: 'http_stats',
184
- }));
185
- }
186
- }
187
- }
188
- // 获取当前统计信息
189
- function getHttpStats() {
190
- return {
191
- activeRequests,
192
- totalRequests,
193
- errorCount,
194
- timeoutCount,
195
- errorRate: totalRequests > 0 ? ((errorCount / totalRequests) * 100).toFixed(2) + '%' : '0%',
196
- timeoutRate: totalRequests > 0 ? ((timeoutCount / totalRequests) * 100).toFixed(2) + '%' : '0%',
197
- };
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
+ 'Content-Type': 'application/json;charset=utf-8',
101
+ },
102
+ timeout: HTTP_CONFIG.requestTimeout,
103
+ httpAgent,
104
+ httpsAgent,
105
+ });
106
+ const responseData = response.data;
107
+ return responseData.data;
198
108
  }