(proxyUris: string[], proxyObj: Config['proxy'])
| 23 | } |
| 24 | |
| 25 | const createMultiProxy = (proxyUris: string[], proxyObj: Config['proxy']): MultiProxyResult => { |
| 26 | const proxies: ProxyState[] = []; |
| 27 | let currentProxyIndex = 0; |
| 28 | |
| 29 | for (const uri of proxyUris) { |
| 30 | const unifiedProxy = unifyProxy(uri, proxyObj); |
| 31 | if (unifiedProxy.proxyUri) { |
| 32 | proxies.push({ |
| 33 | uri: unifiedProxy.proxyUri, |
| 34 | isActive: true, |
| 35 | failureCount: 0, |
| 36 | urlHandler: unifiedProxy.proxyUrlHandler, |
| 37 | }); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | if (proxies.length === 0) { |
| 42 | logger.warn('No valid proxies found in the provided list'); |
| 43 | return { |
| 44 | allProxies: [], |
| 45 | proxyObj: proxyObj || {}, |
| 46 | getNextProxy: () => null, |
| 47 | markProxyFailed: () => {}, |
| 48 | resetProxy: () => {}, |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | const healthCheckInterval = proxyObj?.healthCheckInterval || 60000; |
| 53 | const maxFailures = 3; |
| 54 | |
| 55 | const healthCheck = () => { |
| 56 | const now = Date.now(); |
| 57 | for (const proxy of proxies) { |
| 58 | if (!(!proxy.isActive && proxy.lastFailureTime && now - proxy.lastFailureTime > healthCheckInterval)) { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | proxy.isActive = true; |
| 63 | proxy.failureCount = 0; |
| 64 | delete proxy.lastFailureTime; |
| 65 | logger.info(`Proxy ${proxy.uri} marked as active again after health check`); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | setInterval(healthCheck, healthCheckInterval); |
| 70 | |
| 71 | const getNextProxy = (): ProxyState | null => { |
| 72 | const activeProxies = proxies.filter((p) => p.isActive); |
| 73 | if (activeProxies.length === 0) { |
| 74 | logger.warn('No active proxies available'); |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | let nextProxy = activeProxies[currentProxyIndex % activeProxies.length]; |
| 79 | let attempts = 0; |
| 80 | |
| 81 | while (!nextProxy.isActive && attempts < activeProxies.length) { |
| 82 | currentProxyIndex = (currentProxyIndex + 1) % activeProxies.length; |
no test coverage detected