perf: 优化初始值

pull/180/head
xiaojunnuo 2020-11-20 15:53:39 +08:00
parent 4d79a98588
commit dd2e36007d
3 changed files with 19 additions and 12 deletions

View File

@ -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)
}

View File

@ -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) {

View File

@ -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)
}