|
1 | 1 | import PQueue from 'p-queue'; |
2 | 2 | import Queue from 'queue'; |
3 | | -import http from 'http'; |
4 | | -import https from 'https'; |
5 | | -import axios from 'axios'; |
| 3 | +import createAxiosInstance from '../utils/createAxiosAgent.js'; |
| 4 | + |
| 5 | +const maxSockets = 16; |
| 6 | +const _axios = createAxiosInstance({maxSockets:maxSockets}); |
6 | 7 |
|
7 | | -const batchSockets = 16; |
8 | 8 |
|
9 | 9 | export const batchFetch1 = async (items, maxWorkers = 5, timeoutConfig = 5000) => { |
10 | 10 | let t1 = (new Date()).getTime(); |
11 | | - const AgentOption = {keepAlive: true, maxSockets: batchSockets, timeout: 30000}; // 最大连接数64,30秒定期清理空闲连接 |
12 | | -// const AgentOption = {keepAlive: true}; |
13 | | - const httpAgent = new http.Agent(AgentOption); |
14 | | - const httpsAgent = new https.Agent({rejectUnauthorized: false, ...AgentOption}); |
15 | | - |
16 | | -// 配置 axios 使用代理 |
17 | | - const _axios = axios.create({ |
18 | | - httpAgent, // 用于 HTTP 请求的代理 |
19 | | - httpsAgent, // 用于 HTTPS 请求的代理 |
20 | | - }); |
21 | 11 | const queue = new PQueue({concurrency: maxWorkers}); |
22 | 12 |
|
23 | 13 | // 获取全局 timeout 设置 |
@@ -51,16 +41,6 @@ export const batchFetch1 = async (items, maxWorkers = 5, timeoutConfig = 5000) = |
51 | 41 |
|
52 | 42 | export const batchFetch2 = async (items, maxWorkers = 5, timeoutConfig = 5000) => { |
53 | 43 | let t1 = (new Date()).getTime(); |
54 | | - const AgentOption = {keepAlive: true, maxSockets: batchSockets, timeout: 30000}; // 最大连接数64,30秒定期清理空闲连接 |
55 | | -// const AgentOption = {keepAlive: true}; |
56 | | - const httpAgent = new http.Agent(AgentOption); |
57 | | - const httpsAgent = new https.Agent({rejectUnauthorized: false, ...AgentOption}); |
58 | | - |
59 | | -// 配置 axios 使用代理 |
60 | | - const _axios = axios.create({ |
61 | | - httpAgent, // 用于 HTTP 请求的代理 |
62 | | - httpsAgent, // 用于 HTTPS 请求的代理 |
63 | | - }); |
64 | 44 | const queue = new Queue({concurrency: maxWorkers, autostart: true}); |
65 | 45 |
|
66 | 46 | // 获取全局 timeout 设置 |
|
0 commit comments