From dd2e36007d2570fae54d6acc2ad154e0d5f5a9b3 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Fri, 20 Nov 2020 15:53:39 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/mitmproxy/src/lib/choice/index.js | 4 ++- .../src/lib/interceptor/impl/proxy.js | 2 +- .../proxy/mitmproxy/createRequestHandler.js | 25 +++++++++++-------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/mitmproxy/src/lib/choice/index.js b/packages/mitmproxy/src/lib/choice/index.js index 24d9fa6d..3ee73df7 100644 --- a/packages/mitmproxy/src/lib/choice/index.js +++ b/packages/mitmproxy/src/lib/choice/index.js @@ -35,11 +35,13 @@ class DynamicChoice { setBackupList (backupList) { this.value = backupList.shift() this.backup = backupList + let defaultTotal = backupList.length > 6 ? backupList.length : 6 for (const item of backupList) { if (this.count[item]) { continue } - this.count[item] = { value: item, total: 0, error: 0, keepErrorCount: 0, successRate: 1.0 } + this.count[item] = { value: item, total: defaultTotal, error: 0, keepErrorCount: 0, successRate: 1 } + defaultTotal-- } this.doCount(this.value, false) } diff --git a/packages/mitmproxy/src/lib/interceptor/impl/proxy.js b/packages/mitmproxy/src/lib/interceptor/impl/proxy.js index 538e09d9..4dfe7842 100644 --- a/packages/mitmproxy/src/lib/interceptor/impl/proxy.js +++ b/packages/mitmproxy/src/lib/interceptor/impl/proxy.js @@ -49,7 +49,7 @@ module.exports = { if (URL.port == null) { rOptions.port = rOptions.protocol === 'https:' ? 443 : 80 } - log.info('proxy:', rOptions.hostname, rOptions.path, proxyTarget) + log.info('proxy:', rOptions.hostname, proxyTarget) return true }, is (interceptOpt) { diff --git a/packages/mitmproxy/src/lib/proxy/mitmproxy/createRequestHandler.js b/packages/mitmproxy/src/lib/proxy/mitmproxy/createRequestHandler.js index 8831cbdd..94a17021 100644 --- a/packages/mitmproxy/src/lib/proxy/mitmproxy/createRequestHandler.js +++ b/packages/mitmproxy/src/lib/proxy/mitmproxy/createRequestHandler.js @@ -62,16 +62,16 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy, }) } - function countSlow (isDnsIntercept) { + function countSlow (isDnsIntercept, type) { if (isDnsIntercept) { const { dns, ip, hostname } = isDnsIntercept dns.count(hostname, ip, true) - log.error('记录ip失败次数,用于优选ip:', hostname, ip) + log.error('记录ip失败次数,用于优选ip:', hostname, ip, type) } const counter = context.requestCount if (counter != null) { counter.count.doCount(counter.value, true) - log.error('记录prxoy失败次数:', counter.value) + log.error('记录proxy失败次数:', counter.value, type) } } @@ -117,15 +117,16 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy, log.info('代理请求返回:', url, cost + 'ms') } if (cost > MAX_SLOW_TIME) { - countSlow(isDnsIntercept) + countSlow(isDnsIntercept, 'to slow ' + cost + 'ms') } resolve(proxyRes) }) proxyReq.on('timeout', () => { const end = new Date().getTime() - countSlow(isDnsIntercept) - log.error('代理请求超时', rOptions.protocol, rOptions.hostname, rOptions.path, (end - start) + 'ms') + const cost = end - start + log.error('代理请求超时', rOptions.protocol, rOptions.hostname, rOptions.path, cost + 'ms') + countSlow(isDnsIntercept, 'to slow ' + cost + 'ms') proxyReq.end() proxyReq.destroy() const error = new Error(`${rOptions.host}:${rOptions.port}, 代理请求超时`) @@ -135,8 +136,9 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy, proxyReq.on('error', (e) => { const end = new Date().getTime() - countSlow(isDnsIntercept) - log.error('代理请求错误', e.code, e.message, rOptions.hostname, rOptions.path, (end - start) + 'ms') + const cost = end - start + log.error('代理请求错误', e.code, e.message, rOptions.hostname, rOptions.path, cost + 'ms') + countSlow(isDnsIntercept, 'error:' + e.message) reject(e) }) @@ -146,7 +148,7 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy, log.error('代理请求被取消', rOptions.hostname, rOptions.path, cost + 'ms') if (cost > MAX_SLOW_TIME) { - countSlow(isDnsIntercept) + countSlow(isDnsIntercept, 'to slow ' + cost + 'ms') } if (res.writableEnded) { @@ -190,7 +192,7 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy, // // console.log('BODY: ') // }) proxyRes.on('error', (error) => { - countSlow() + countSlow(null, 'error:' + error.message) log.error('proxy res error', error) }) @@ -241,6 +243,9 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy, } }) + if (proxyRes.statusCode >= 400) { + countSlow(null, 'status return :' + proxyRes.statusCode) + } res.writeHead(proxyRes.statusCode) proxyRes.pipe(res) }