perf: 优化初始值
parent
4d79a98588
commit
dd2e36007d
|
@ -35,11 +35,13 @@ class DynamicChoice {
|
||||||
setBackupList (backupList) {
|
setBackupList (backupList) {
|
||||||
this.value = backupList.shift()
|
this.value = backupList.shift()
|
||||||
this.backup = backupList
|
this.backup = backupList
|
||||||
|
let defaultTotal = backupList.length > 6 ? backupList.length : 6
|
||||||
for (const item of backupList) {
|
for (const item of backupList) {
|
||||||
if (this.count[item]) {
|
if (this.count[item]) {
|
||||||
continue
|
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)
|
this.doCount(this.value, false)
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ module.exports = {
|
||||||
if (URL.port == null) {
|
if (URL.port == null) {
|
||||||
rOptions.port = rOptions.protocol === 'https:' ? 443 : 80
|
rOptions.port = rOptions.protocol === 'https:' ? 443 : 80
|
||||||
}
|
}
|
||||||
log.info('proxy:', rOptions.hostname, rOptions.path, proxyTarget)
|
log.info('proxy:', rOptions.hostname, proxyTarget)
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
is (interceptOpt) {
|
is (interceptOpt) {
|
||||||
|
|
|
@ -62,16 +62,16 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function countSlow (isDnsIntercept) {
|
function countSlow (isDnsIntercept, type) {
|
||||||
if (isDnsIntercept) {
|
if (isDnsIntercept) {
|
||||||
const { dns, ip, hostname } = isDnsIntercept
|
const { dns, ip, hostname } = isDnsIntercept
|
||||||
dns.count(hostname, ip, true)
|
dns.count(hostname, ip, true)
|
||||||
log.error('记录ip失败次数,用于优选ip:', hostname, ip)
|
log.error('记录ip失败次数,用于优选ip:', hostname, ip, type)
|
||||||
}
|
}
|
||||||
const counter = context.requestCount
|
const counter = context.requestCount
|
||||||
if (counter != null) {
|
if (counter != null) {
|
||||||
counter.count.doCount(counter.value, true)
|
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')
|
log.info('代理请求返回:', url, cost + 'ms')
|
||||||
}
|
}
|
||||||
if (cost > MAX_SLOW_TIME) {
|
if (cost > MAX_SLOW_TIME) {
|
||||||
countSlow(isDnsIntercept)
|
countSlow(isDnsIntercept, 'to slow ' + cost + 'ms')
|
||||||
}
|
}
|
||||||
resolve(proxyRes)
|
resolve(proxyRes)
|
||||||
})
|
})
|
||||||
|
|
||||||
proxyReq.on('timeout', () => {
|
proxyReq.on('timeout', () => {
|
||||||
const end = new Date().getTime()
|
const end = new Date().getTime()
|
||||||
countSlow(isDnsIntercept)
|
const cost = end - start
|
||||||
log.error('代理请求超时', rOptions.protocol, rOptions.hostname, rOptions.path, (end - start) + 'ms')
|
log.error('代理请求超时', rOptions.protocol, rOptions.hostname, rOptions.path, cost + 'ms')
|
||||||
|
countSlow(isDnsIntercept, 'to slow ' + cost + 'ms')
|
||||||
proxyReq.end()
|
proxyReq.end()
|
||||||
proxyReq.destroy()
|
proxyReq.destroy()
|
||||||
const error = new Error(`${rOptions.host}:${rOptions.port}, 代理请求超时`)
|
const error = new Error(`${rOptions.host}:${rOptions.port}, 代理请求超时`)
|
||||||
|
@ -135,8 +136,9 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy,
|
||||||
|
|
||||||
proxyReq.on('error', (e) => {
|
proxyReq.on('error', (e) => {
|
||||||
const end = new Date().getTime()
|
const end = new Date().getTime()
|
||||||
countSlow(isDnsIntercept)
|
const cost = end - start
|
||||||
log.error('代理请求错误', e.code, e.message, rOptions.hostname, rOptions.path, (end - start) + 'ms')
|
log.error('代理请求错误', e.code, e.message, rOptions.hostname, rOptions.path, cost + 'ms')
|
||||||
|
countSlow(isDnsIntercept, 'error:' + e.message)
|
||||||
reject(e)
|
reject(e)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -146,7 +148,7 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy,
|
||||||
log.error('代理请求被取消', rOptions.hostname, rOptions.path, cost + 'ms')
|
log.error('代理请求被取消', rOptions.hostname, rOptions.path, cost + 'ms')
|
||||||
|
|
||||||
if (cost > MAX_SLOW_TIME) {
|
if (cost > MAX_SLOW_TIME) {
|
||||||
countSlow(isDnsIntercept)
|
countSlow(isDnsIntercept, 'to slow ' + cost + 'ms')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res.writableEnded) {
|
if (res.writableEnded) {
|
||||||
|
@ -190,7 +192,7 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy,
|
||||||
// // console.log('BODY: ')
|
// // console.log('BODY: ')
|
||||||
// })
|
// })
|
||||||
proxyRes.on('error', (error) => {
|
proxyRes.on('error', (error) => {
|
||||||
countSlow()
|
countSlow(null, 'error:' + error.message)
|
||||||
log.error('proxy res error', error)
|
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)
|
res.writeHead(proxyRes.statusCode)
|
||||||
proxyRes.pipe(res)
|
proxyRes.pipe(res)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue