From 15c26f193930a2c935659985fd99f35688b948dd Mon Sep 17 00:00:00 2001 From: YGXB_net Date: Tue, 28 Jul 2026 09:17:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor(uploader):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=B9=B6=E5=8F=91=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将循环方式从索引遍历改为range方式 - 添加处理进程创建注释说明 - 在进度更新后添加延时避免过度频繁更新 - 修复空行处理时计数器递增逻辑 - 优化panic恢复处理流程 --- internal/uploader/uploader.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/uploader/uploader.go b/internal/uploader/uploader.go index b9f8f8f..c8d2967 100644 --- a/internal/uploader/uploader.go +++ b/internal/uploader/uploader.go @@ -334,7 +334,7 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil lines := make(chan string, 200) var countLine int32 = 0 // 创建指定个worker同时处理文件上传 - for i := 0; i < config.APPConfig.ThreadCount; i++ { + for i := range config.APPConfig.ThreadCount { select { case <-ctx.Done(): close(lines) @@ -342,6 +342,7 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil default: } + //创建处理进程 go func() { processLines(ctx, logChan, &lines, i, filePath, &countLine) }() @@ -356,6 +357,7 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil fmt.Println("panic:", f+":"+strconv.Itoa(l), r) } }() + for scanner.Scan() { select { case <-ctx.Done(): @@ -384,6 +386,7 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil Percentage: int(float64(countLine) / float64(fileLines) * 100), }, ) + time.Sleep(50 * time.Millisecond) } //上传完成,进度设为100 progress.Store(filepath.Base(filePath), @@ -415,6 +418,7 @@ func processLines(ctx context.Context, logChan *chan string, lines *chan string, // 跳过空行 if strings.TrimSpace(line) == "" { + atomic.AddInt32(countLine, 1) continue } // 上传数据