fix: latency not correct when proxy group has custom test url (#1199)

This commit is contained in:
njzy 2024-11-30 00:30:27 +08:00 committed by GitHub
parent 19f595919d
commit 23e56cfde8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 24 deletions

View File

@ -8,7 +8,7 @@ import {
formatProxyType, formatProxyType,
getLatencyClassName, getLatencyClassName,
} from '~/helpers' } from '~/helpers'
import { rootElement, urlForLatencyTest, useProxies } from '~/signals' import { rootElement, useProxies } from '~/signals'
export const ProxyNodeCard = (props: { export const ProxyNodeCard = (props: {
proxyName: string proxyName: string
@ -18,8 +18,12 @@ export const ProxyNodeCard = (props: {
onClick?: () => void onClick?: () => void
}) => { }) => {
const { proxyName, isSelected, onClick } = props const { proxyName, isSelected, onClick } = props
const { proxyNodeMap, proxyLatencyTest, proxyLatencyTestingMap } = const {
useProxies() proxyNodeMap,
proxyLatencyTest,
proxyLatencyTestingMap,
getLatencyHistoryByName,
} = useProxies()
const proxyNode = createMemo(() => proxyNodeMap()[proxyName]) const proxyNode = createMemo(() => proxyNodeMap()[proxyName])
const specialTypes = createMemo(() => { const specialTypes = createMemo(() => {
@ -38,8 +42,10 @@ export const ProxyNodeCard = (props: {
[proxyName, specialTypes()].filter(Boolean).join(' - '), [proxyName, specialTypes()].filter(Boolean).join(' - '),
) )
const latencyTestHistory = const latencyTestHistory = getLatencyHistoryByName(
proxyNode().latencyTestHistory[props.testUrl || urlForLatencyTest()] || [] props.proxyName,
props.testUrl,
)
return ( return (
<Tooltip <Tooltip

View File

@ -70,7 +70,7 @@ type AllTestUrlLatencyInfo = {
} }
const getLatencyFromProxy = ( const getLatencyFromProxy = (
proxy: Pick<Proxy, 'extra' | 'history'>, proxy: Pick<Proxy, 'extra' | 'history' | 'testUrl'>,
fallbackDefault = true, fallbackDefault = true,
): AllTestUrlLatencyInfo => { ): AllTestUrlLatencyInfo => {
const extra = (proxy.extra || {}) as Record< const extra = (proxy.extra || {}) as Record<
@ -80,22 +80,9 @@ const getLatencyFromProxy = (
} }
> >
if (!Object.keys(extra).length && fallbackDefault) { const { allTestUrlLatency, allTestUrlLatencyHistory } = Object.keys(
const testUrl = urlForLatencyTest() extra,
).reduce(
const delay =
proxy.history?.at(-1)?.delay ?? latencyQualityMap().NOT_CONNECTED
const allTestUrlLatency = { [testUrl]: delay }
const allTestUrlLatencyHistory = { [testUrl]: proxy.history }
return {
allTestUrlLatency,
allTestUrlLatencyHistory,
} as AllTestUrlLatencyInfo
}
return Object.keys(extra).reduce(
(acc, testUrl) => { (acc, testUrl) => {
const data = extra[testUrl] const data = extra[testUrl]
const delay = const delay =
@ -111,6 +98,23 @@ const getLatencyFromProxy = (
allTestUrlLatencyHistory: {}, allTestUrlLatencyHistory: {},
} as AllTestUrlLatencyInfo, } as AllTestUrlLatencyInfo,
) )
if (fallbackDefault) {
// Since the proxy here could be a proxy group, prioritize using the group's own testUrl
const defaultTestUrl = proxy.testUrl || urlForLatencyTest()
const isDefaultTestUrlLatencyExists = defaultTestUrl in allTestUrlLatency
// If the defaultTtestUrlLatency is not exist, use the fault latency history
// If the current proxy is a proxy group with its own testUrl, then this history refers to the current proxy group's history
if (!isDefaultTestUrlLatencyExists) {
const delay =
proxy.history?.at(-1)?.delay ?? latencyQualityMap().NOT_CONNECTED
allTestUrlLatency[defaultTestUrl] = delay
allTestUrlLatencyHistory[defaultTestUrl] = proxy.history
}
}
return { allTestUrlLatency, allTestUrlLatencyHistory }
} }
const setProxiesInfo = ( const setProxiesInfo = (
@ -153,7 +157,7 @@ export const useProxies = () => {
]) ])
const proxiesWithTestUrl = Object.values(proxies).map((proxy) => { const proxiesWithTestUrl = Object.values(proxies).map((proxy) => {
if (proxy.all?.length) { if (proxy.all?.length && !proxy.testUrl) {
const { testUrl, timeout } = providers?.[proxy.name] || {} const { testUrl, timeout } = providers?.[proxy.name] || {}
return { ...proxy, testUrl, timeout } return { ...proxy, testUrl, timeout }
@ -321,8 +325,30 @@ export const useProxies = () => {
const getLatencyByName = (name: string, testUrl: string | null) => { const getLatencyByName = (name: string, testUrl: string | null) => {
const finalTestUrl = testUrl || urlForLatencyTest() const finalTestUrl = testUrl || urlForLatencyTest()
const latencyMapValue = latencyMap()
return latencyMap()[getNowProxyNodeName(name)]?.[finalTestUrl] || 0 // First recursively search for proxy node latency by name for the current testUrl.
// If not found, the current name may be a proxy group - in that case, use that proxy group's latency
return (
latencyMapValue[getNowProxyNodeName(name)]?.[finalTestUrl] ||
latencyMapValue[name]?.[finalTestUrl] ||
latencyQualityMap().NOT_CONNECTED
)
}
const getLatencyHistoryByName = (name: string, testUrl: string | null) => {
const proxyNode = proxyNodeMap()[name]
const nowProxyNodeName = getNowProxyNodeName(name)
const nowProxyNode = proxyNodeMap()[nowProxyNodeName]
const finalTestUrl = testUrl || urlForLatencyTest()
return (
nowProxyNode.latencyTestHistory[finalTestUrl] ||
proxyNode.latencyTestHistory[finalTestUrl] ||
[]
)
} }
const isProxyGroup = (name: string) => { const isProxyGroup = (name: string) => {
@ -358,6 +384,7 @@ export const useProxies = () => {
proxyProviderLatencyTest, proxyProviderLatencyTest,
getNowProxyNodeName, getNowProxyNodeName,
getLatencyByName, getLatencyByName,
getLatencyHistoryByName,
isProxyGroup, isProxyGroup,
} }
} }