refactor(proxies): use connection msg instead of active connections for better performance

This commit is contained in:
Zephyruso 2023-09-08 15:22:07 +08:00
parent a8cb75f29d
commit a7213df135
2 changed files with 23 additions and 9 deletions

View File

@ -69,7 +69,7 @@ export const useConnections = () => {
} }
} }
function restructRawMsgToConnection( export function restructRawMsgToConnection(
connections: ConnectionRawMessage[], connections: ConnectionRawMessage[],
prevActiveConnections: Connection[], prevActiveConnections: Connection[],
): Connection[] { ): Connection[] {
@ -95,7 +95,7 @@ function restructRawMsgToConnection(
}) })
} }
function mergeAllConnections(activeConns: Connection[]) { export function mergeAllConnections(activeConns: Connection[]) {
return unionWith(allConnections(), activeConns, (a, b) => a.id === b.id) return unionWith(allConnections(), activeConns, (a, b) => a.id === b.id)
} }

View File

@ -6,7 +6,11 @@ import {
useRequest, useRequest,
} from '~/signals' } from '~/signals'
import type { Proxy, ProxyNode, ProxyProvider } from '~/types' import type { Proxy, ProxyNode, ProxyProvider } from '~/types'
import { useConnections } from './connections' import {
latestConnectionMsg,
mergeAllConnections,
restructRawMsgToConnection,
} from './connections'
type ProxyInfo = { type ProxyInfo = {
name: string name: string
@ -47,7 +51,6 @@ const setProxiesInfo = (proxies: (Proxy | ProxyNode)[]) => {
export const useProxies = () => { export const useProxies = () => {
const request = useRequest() const request = useRequest()
const { activeConnections } = useConnections()
const updateProxies = async () => { const updateProxies = async () => {
const [{ providers }, { proxies }] = await Promise.all([ const [{ providers }, { proxies }] = await Promise.all([
@ -89,11 +92,22 @@ export const useProxies = () => {
}) })
if (autoCloseConns()) { if (autoCloseConns()) {
activeConnections().forEach(({ id, chains }) => { // we dont use activeConns from useConnection here for better performance
if (chains.includes(proxy.name)) { // and we use empty array to restruct msg because they are closed and they won't have speed anyway
request.delete(`connections/${id}`)
} const activeConns = restructRawMsgToConnection(
}) latestConnectionMsg()?.connections ?? [],
[],
)
if (activeConns.length > 0) {
activeConns.forEach(({ id, chains }) => {
if (chains.includes(proxy.name)) {
request.delete(`connections/${id}`)
}
})
mergeAllConnections(activeConns)
}
} }
proxyGroup.now = proxyName proxyGroup.now = proxyName