From 48b0b56ce7d500a8e4d8e347a1b47243cb6edb37 Mon Sep 17 00:00:00 2001 From: bourdon Date: Sat, 4 Oct 2025 01:43:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=9A=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=20webapp=20=E5=9F=BA=E4=BA=8E=E7=B4=A2=E5=BC=95=E7=9A=84?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=AF=BC=E8=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 主要改进: - 添加索引导航支持:前进/后退按钮现在基于记录索引加载数据 - 后端 API 支持可选的 index 参数,返回包含 centerIndex 的响应 - 前端追踪 currentCenterIndex,实现精确的页面跳转 - 在状态徽章中显示记录索引号 [#索引] - 修复日志显示逻辑:从追加模式改为完全重新渲染 代码优化: - concurrent: 使用 Go 1.25 range 语法和 min 函数 - concurrent: 使用 WaitGroup.Go 方法简化 goroutine 启动 - topic_processor: 修正格式化输出 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- example/concurrent/main.go | 22 ++--- example/topic_processor/main.go | 2 +- example/webapp/main.go | 157 +++++++++++++++++++++----------- 3 files changed, 115 insertions(+), 66 deletions(-) diff --git a/example/concurrent/main.go b/example/concurrent/main.go index 1a91e4d..4dc6906 100644 --- a/example/concurrent/main.go +++ b/example/concurrent/main.go @@ -114,12 +114,12 @@ func main() { var totalQueries atomic.Int64 var queryErrors atomic.Int64 - for i := 0; i < queryCount; i++ { + for i := range queryCount { queryWg.Add(1) go func(queryID int) { defer queryWg.Done() - for j := 0; j < queriesPerGoroutine; j++ { + for j := range queriesPerGoroutine { // 随机选择一个 topic 进行查询 topic := topics[j%len(topics)] @@ -136,10 +136,7 @@ func main() { } // 查询最新的 10 条记录 - querySize := 10 - if count < querySize { - querySize = count - } + querySize := min(count, 10) _, err = processor.QueryNewest(count-1, querySize) if err != nil { @@ -180,7 +177,7 @@ func main() { go func(topicName string) { defer mixWg.Done() - for j := 0; j < 1000; j++ { + for j := range 1000 { data := fmt.Sprintf("mix-%s-msg-%d", topicName, j) if _, err := seq.Write(topicName, []byte(data)); err == nil { @@ -194,12 +191,12 @@ func main() { } // 启动查询 goroutine - for i := 0; i < 10; i++ { + for i := range 10 { mixWg.Add(1) go func(queryID int) { defer mixWg.Done() - for j := 0; j < 200; j++ { + for j := range 200 { topic := topics[j%len(topics)] processor, err := seq.GetProcessor(topic) @@ -267,7 +264,7 @@ func main() { } // 持续查询 goroutine - for i := 0; i < 5; i++ { + for i := range 5 { stressWg.Add(1) go func(queryID int) { defer stressWg.Done() @@ -296,8 +293,7 @@ func main() { } // 进度显示 goroutine - stressWg.Add(1) - go func() { + stressWg.Go(func() { defer stressWg.Done() ticker := time.NewTicker(10 * time.Second) defer ticker.Stop() @@ -314,7 +310,7 @@ func main() { stressQueryCount.Load()) } } - }() + }) stressWg.Wait() stressDuration := time.Since(stressTestStart) diff --git a/example/topic_processor/main.go b/example/topic_processor/main.go index 253faa1..6671ce1 100644 --- a/example/topic_processor/main.go +++ b/example/topic_processor/main.go @@ -10,7 +10,7 @@ import ( func main() { // ===== TopicProcessor 作为聚合器使用 ===== - fmt.Println("=== TopicProcessor 聚合器示例 ===\n") + fmt.Println("=== TopicProcessor 聚合器示例 ===") // 创建 TopicProcessor(提供空 handler) logger := slog.Default() diff --git a/example/webapp/main.go b/example/webapp/main.go index d9825ae..ffb9830 100644 --- a/example/webapp/main.go +++ b/example/webapp/main.go @@ -347,7 +347,7 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {