Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

Commit

Permalink
support custom delay test concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Sep 23, 2024
1 parent 21eed81 commit 09847fe
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/renderer/src/components/settings/mihomo-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const MihomoConfig: React.FC = () => {
const {
controlDns = true,
controlSniff = true,
delayTestConcurrency,
delayTestTimeout,
githubToken = '',
autoCloseConnection = true,
Expand Down Expand Up @@ -56,6 +57,18 @@ const MihomoConfig: React.FC = () => {
}}
></Input>
</SettingItem>
<SettingItem title="延迟测试并发数量" divider>
<Input
type="number"
size="sm"
className="w-[60%]"
value={delayTestConcurrency?.toString()}
placeholder="默认 50"
onValueChange={(v) => {
patchAppConfig({ delayTestConcurrency: parseInt(v) })
}}
/>
</SettingItem>
<SettingItem title="延迟测试超时时间" divider>
<Input
type="number"
Expand Down
42 changes: 33 additions & 9 deletions src/renderer/src/pages/proxies.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Avatar, Button, Card, CardBody, Chip } from '@nextui-org/react'
import BasePage from '@renderer/components/base/base-page'
import { useAppConfig } from '@renderer/hooks/use-app-config'
import {
mihomoChangeProxy,
mihomoCloseAllConnections,
mihomoGroupDelay,
mihomoProxyDelay
} from '@renderer/utils/ipc'
import { mihomoChangeProxy, mihomoCloseAllConnections, mihomoProxyDelay } from '@renderer/utils/ipc'
import { CgDetailsLess, CgDetailsMore } from 'react-icons/cg'
import { TbCircleLetterD } from 'react-icons/tb'
import { FaLocationCrosshairs } from 'react-icons/fa6'
Expand All @@ -27,7 +22,8 @@ const Proxies: React.FC = () => {
proxyDisplayMode = 'simple',
proxyDisplayOrder = 'default',
autoCloseConnection = true,
proxyCols = 'auto'
proxyCols = 'auto',
delayTestConcurrency = 50
} = appConfig || {}
const [cols, setCols] = useState(1)
const [isOpen, setIsOpen] = useState(Array(groups.length).fill(false))
Expand Down Expand Up @@ -79,18 +75,46 @@ const Proxies: React.FC = () => {
}

const onGroupDelay = async (index: number): Promise<void> => {
if (allProxies[index].length === 0) {
setIsOpen((prev) => {
const newOpen = [...prev]
newOpen[index] = true
return newOpen
})
}
setDelaying((prev) => {
const newDelaying = [...prev]
newDelaying[index] = true
return newDelaying
})
await mihomoGroupDelay(groups[index].name, groups[index].testUrl)
// 限制并发数量
const result: Promise<void>[] = []
const runningList: Promise<void>[] = []
for (const proxy of allProxies[index]) {
const promise = Promise.resolve().then(async () => {
try {
await mihomoProxyDelay(proxy.name, groups[index].testUrl)
} catch {
// ignore
} finally {
mutate()
}
})
result.push(promise)
const running = promise.then(() => {
runningList.splice(runningList.indexOf(running), 1)
})
runningList.push(running)
if (runningList.length >= (delayTestConcurrency || 50)) {
await Promise.race(runningList)
}
}
await Promise.all(result)
setDelaying((prev) => {
const newDelaying = [...prev]
newDelaying[index] = false
return newDelaying
})
mutate()
}

const calcCols = (): number => {
Expand Down
1 change: 1 addition & 0 deletions src/shared/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ interface IAppConfig {
sysProxy: ISysProxyConfig
maxLogDays: number
userAgent?: string
delayTestConcurrency?: number
delayTestUrl?: string
delayTestTimeout?: number
encryptedPassword?: number[]
Expand Down

0 comments on commit 09847fe

Please sign in to comment.