From 70c3e474175ebba65bdf68f354dc679ccc425c44 Mon Sep 17 00:00:00 2001 From: qingzhengli <1204552371@qq.com> Date: Sun, 8 Oct 2023 14:52:47 +0800 Subject: [PATCH 1/3] init --- lib/policies/proxy/proxy.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/policies/proxy/proxy.js b/lib/policies/proxy/proxy.js index abb5254f2..ad237db1f 100644 --- a/lib/policies/proxy/proxy.js +++ b/lib/policies/proxy/proxy.js @@ -61,11 +61,29 @@ module.exports = function (params, config) { proxyOptions.agent = new ProxyAgent(intermediateProxyUrl); } + const retryCountMap = {}; const proxy = httpProxy.createProxyServer(Object.assign(params, proxyOptions)); + //when http request suucess,clear map + proxy.on('end', (req,res)=>{ + delete retryCountMap[req.egContext.requestID]; + }); + + //retry function + const maxAutoRetries = params.maxAutoRetries || 1; proxy.on('error', (err, req, res) => { logger.warn(err); + const requestID = req.egContext.requestID; + let retryCount = retryCountMap[requestID] || 0; + if(retryCount < maxAutoRetries){ + retryCount++; + retryCountMap[requestID] = retryCount; + // console.log(`retry count: ${retryCount} `, req.originalUrl); + proxyHandler(req, res); + return; + } + delete retryCountMap[requestID]; if (!res.headersSent) { res.status(502).send('Bad gateway.'); } else { @@ -100,10 +118,9 @@ module.exports = function (params, config) { } } : () => { }; - return function proxyHandler(req, res) { + function proxyHandler(req, res) { const target = balancer.nextTarget(); const headers = Object.assign({ 'eg-request-id': req.egContext.requestID }, proxyOptions.headers); - stripPathFn(req); logger.debug(`proxying to ${target.href}, ${req.method} ${req.url}`); @@ -115,6 +132,7 @@ module.exports = function (params, config) { agent: !intermediateProxyUrl ? target.protocol === 'https:' ? httpsAgent : httpAgent : proxyOptions.agent }); }; + return proxyHandler; // multiple urls will load balance, defaulting to round-robin }; @@ -137,4 +155,4 @@ function readCertificateDataFromFile({ keyFile, certFile, caFile }) { } return { key, cert, ca }; -} +} \ No newline at end of file From 681f461ee26d1972666653c563da33f9d55b0f2c Mon Sep 17 00:00:00 2001 From: liqingzheng <1204552371@qq.com> Date: Sun, 8 Oct 2023 22:23:32 +0800 Subject: [PATCH 2/3] eslint fix --- lib/policies/proxy/proxy.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/policies/proxy/proxy.js b/lib/policies/proxy/proxy.js index ad237db1f..332c75e2b 100644 --- a/lib/policies/proxy/proxy.js +++ b/lib/policies/proxy/proxy.js @@ -63,20 +63,19 @@ module.exports = function (params, config) { const retryCountMap = {}; const proxy = httpProxy.createProxyServer(Object.assign(params, proxyOptions)); - //when http request suucess,clear map - proxy.on('end', (req,res)=>{ + // when http request suucess,clear map + proxy.on('end', (req, res) => { delete retryCountMap[req.egContext.requestID]; }); - - //retry function + // retry function const maxAutoRetries = params.maxAutoRetries || 1; proxy.on('error', (err, req, res) => { logger.warn(err); const requestID = req.egContext.requestID; let retryCount = retryCountMap[requestID] || 0; - if(retryCount < maxAutoRetries){ + if (retryCount < maxAutoRetries) { retryCount++; retryCountMap[requestID] = retryCount; // console.log(`retry count: ${retryCount} `, req.originalUrl); @@ -155,4 +154,4 @@ function readCertificateDataFromFile({ keyFile, certFile, caFile }) { } return { key, cert, ca }; -} \ No newline at end of file +} From 5e712dec3fa85372781976905dc8a043c731ced3 Mon Sep 17 00:00:00 2001 From: liqingzheng <1204552371@qq.com> Date: Mon, 11 Aug 2025 00:34:19 +0800 Subject: [PATCH 3/3] Delete comments --- lib/policies/proxy/proxy.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/policies/proxy/proxy.js b/lib/policies/proxy/proxy.js index 332c75e2b..ed4abf5d8 100644 --- a/lib/policies/proxy/proxy.js +++ b/lib/policies/proxy/proxy.js @@ -63,7 +63,6 @@ module.exports = function (params, config) { const retryCountMap = {}; const proxy = httpProxy.createProxyServer(Object.assign(params, proxyOptions)); - // when http request suucess,clear map proxy.on('end', (req, res) => { delete retryCountMap[req.egContext.requestID]; }); @@ -78,7 +77,6 @@ module.exports = function (params, config) { if (retryCount < maxAutoRetries) { retryCount++; retryCountMap[requestID] = retryCount; - // console.log(`retry count: ${retryCount} `, req.originalUrl); proxyHandler(req, res); return; }