From d0298ea5773b266a0af6f4fe33275fc3ef047865 Mon Sep 17 00:00:00 2001 From: meiseayoung Date: Tue, 30 Dec 2025 19:23:09 +0800 Subject: [PATCH] fix: In high concurrency (200+), V-Hono experiences an error where the connection is reset problem --- app.v | 9 ++++-- benchmark/quick_test.ps1 | 69 ++++++++++++++++++++++++++++++++++++++++ examples/test_server.v | 24 ++++++++++++++ picoev_server.v | 6 ++-- 4 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 benchmark/quick_test.ps1 create mode 100644 examples/test_server.v diff --git a/app.v b/app.v index 9b3131c..a6360bc 100644 --- a/app.v +++ b/app.v @@ -342,8 +342,13 @@ pub fn (mut app Hono) listen(port string) { return } - // 默认使用 picoev 高性能服务器 - app.listen_picoev(port_num) + // 使用优化配置的 picoev 高性能服务器(支持高并发) + app.listen_picoev_with_config(PicoevConfig{ + port: port_num + timeout_secs: 120 // 高并发场景需要更长超时 + keepalive_timeout: 30 // Keep-Alive 超时 30 秒 + max_keepalive_req: 10000 // 单连接最大请求数 + }) } // 使用传统 http.Server 启动(保留兼容性) diff --git a/benchmark/quick_test.ps1 b/benchmark/quick_test.ps1 new file mode 100644 index 0000000..a71ee9a --- /dev/null +++ b/benchmark/quick_test.ps1 @@ -0,0 +1,69 @@ +# Quick concurrent test using runspaces +$url = "http://127.0.0.1:8888/" +$concurrency = 300 +$requestsEach = 20 + +Write-Host "Testing $url with $concurrency concurrent workers, $requestsEach requests each" +Write-Host "Total: $($concurrency * $requestsEach) requests" + +$runspacePool = [runspacefactory]::CreateRunspacePool(1, $concurrency) +$runspacePool.Open() + +$scriptBlock = { + param($url, $count) + $s = 0; $f = 0 + for ($i = 0; $i -lt $count; $i++) { + try { + $req = [System.Net.WebRequest]::Create($url) + $req.Timeout = 30000 + $resp = $req.GetResponse() + if ($resp.StatusCode -eq "OK") { $s++ } else { $f++ } + $resp.Close() + } catch { + $f++ + } + } + return @{Success=$s; Failed=$f} +} + +$sw = [System.Diagnostics.Stopwatch]::StartNew() +$runspaces = @() + +for ($i = 0; $i -lt $concurrency; $i++) { + $ps = [powershell]::Create() + $ps.RunspacePool = $runspacePool + $ps.AddScript($scriptBlock).AddArgument($url).AddArgument($requestsEach) | Out-Null + $runspaces += @{PS=$ps; Handle=$ps.BeginInvoke()} +} + +$totalSuccess = 0 +$totalFailed = 0 + +foreach ($rs in $runspaces) { + $result = $rs.PS.EndInvoke($rs.Handle) + $totalSuccess += $result.Success + $totalFailed += $result.Failed + $rs.PS.Dispose() +} + +$sw.Stop() +$runspacePool.Close() + +$total = $totalSuccess + $totalFailed +$rps = [math]::Round($total * 1000 / $sw.ElapsedMilliseconds, 2) +$rate = [math]::Round($totalSuccess * 100 / $total, 2) + +Write-Host "" +Write-Host "Results:" +Write-Host " Total: $total" +Write-Host " Success: $totalSuccess" +Write-Host " Failed: $totalFailed" +Write-Host " Duration: $($sw.ElapsedMilliseconds) ms" +Write-Host " RPS: $rps" +Write-Host " Success Rate: $rate%" + +if ($totalFailed -eq 0) { + Write-Host "PASSED - No connection errors!" -ForegroundColor Green +} else { + Write-Host "FAILED - $totalFailed errors occurred" -ForegroundColor Red +} diff --git a/examples/test_server.v b/examples/test_server.v new file mode 100644 index 0000000..6f3e2f8 --- /dev/null +++ b/examples/test_server.v @@ -0,0 +1,24 @@ +// 测试服务器 - 用于高并发测试 +module main + +import hono +import net.http + +fn main() { + mut app := hono.Hono.new() + + // 简单的测试路由 + app.get('/', fn (mut c hono.Context) http.Response { + return c.text('OK') + }) + + app.get('/delay', fn (mut c hono.Context) http.Response { + // 模拟一些处理延迟 + return c.text('OK with delay') + }) + + println('测试服务器启动中...') + println('配置: timeout_secs=120, keepalive_timeout=30, max_keepalive_req=10000') + + app.listen(':8888') +} diff --git a/picoev_server.v b/picoev_server.v index c86562d..02ab0eb 100644 --- a/picoev_server.v +++ b/picoev_server.v @@ -11,12 +11,12 @@ pub: port int = 8080 host string family net.AddrFamily = .ip6 - timeout_secs int = 8 + timeout_secs int = 120 // 高并发场景需要更长超时(原 8 秒太短) max_headers int = 100 max_read int = 8192 max_write int = 65536 - keepalive_timeout int = 5 - max_keepalive_req int = 1000 + keepalive_timeout int = 30 // Keep-Alive 超时延长到 30 秒 + max_keepalive_req int = 10000 // 单连接最大请求数提升到 10000 } // picoev 请求上下文