Compare commits

..

2 Commits

Author SHA1 Message Date
ygxbnet 73a7d26816 refactor(uploader): 优化文件上传并发处理逻辑
构建上传工具 / build-tool (push) Successful in 1m46s
- 将初始化连接池的并发方式改为waitgroup控制的goroutine池
- 调整文件处理时的channel缓冲区大小从100增加到200
- 移除不必要的sync.WaitGroup变量声明
- 修改进度计算逻辑,确保上传完成时进度显示为100%
- 添加对processLines函数的功能注释
- 优化上下文取消时的资源清理流程,及时关闭channel
2026-04-28 15:43:02 +08:00
ygxbnet 1cac9e9013 fix(build): 修复版本号链接参数格式问题
- 修正了 ldflags 中版本号格式导致的编译错误
- 移除了版本字符串中的意外空格
- 确保版本号正确传递给主程序包变量
2026-04-28 15:08:21 +08:00
3 changed files with 43 additions and 24 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ jobs:
build_date=$(TZ=Asia/Shanghai date +"%Y%m%d%H%M") build_date=$(TZ=Asia/Shanghai date +"%Y%m%d%H%M")
wails build \ wails build \
-platform windows/amd64 \ -platform windows/amd64 \
-ldflags "-X 'main.version=$build_date-$git_hash'" \ -ldflags "-X 'main.version= $build_date - $git_hash'" \
-o 上传工具.exe -o 上传工具.exe
- name: 上传构建文件 - name: 上传构建文件
+19 -10
View File
@@ -7,6 +7,7 @@ import (
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"sync"
"time" "time"
) )
@@ -28,17 +29,25 @@ func init() {
// InitConn 创建连接池 // InitConn 创建连接池
func InitConn() { func InitConn() {
for i := 0; i < 200; i++ { wg := &sync.WaitGroup{}
resp, err := httpClient.Get(config.APPConfig.Url + "/api/test")
if err != nil { for i := 0; i < 10; i++ {
fmt.Println(err) wg.Go(func() {
return for i := 0; i < 50; i++ {
} resp, err := httpClient.Get(config.APPConfig.Url + "/api/test")
defer func() { if err != nil {
io.Copy(io.Discard, resp.Body) fmt.Println(err)
resp.Body.Close() return
}() }
defer func() {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()
}
})
} }
wg.Wait()
} }
func UploadDataToServer(ctx context.Context, data string) error { func UploadDataToServer(ctx context.Context, data string) error {
+23 -13
View File
@@ -211,7 +211,6 @@ func getTxtFiles(dir string) (txtFiles []string, err error) {
} }
func processFile(ctx context.Context, logChan *chan string, filePath string, fileLines int) { func processFile(ctx context.Context, logChan *chan string, filePath string, fileLines int) {
var wg sync.WaitGroup
// 打开文件 // 打开文件
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
@@ -221,8 +220,7 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
defer file.Close() defer file.Close()
// 创建行通道 // 创建行通道
lines := make(chan string, 100) lines := make(chan string, 200)
defer close(lines)
var countLine int32 = 0 var countLine int32 = 0
// 创建指定个worker同时处理文件上传 // 创建指定个worker同时处理文件上传
for i := 0; i < config.APPConfig.ThreadCount; i++ { for i := 0; i < config.APPConfig.ThreadCount; i++ {
@@ -230,9 +228,9 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
case <-ctx.Done(): case <-ctx.Done():
return return
default: default:
wg.Go(func() { go func() {
processLines(ctx, logChan, &lines, i, filePath, &countLine) processLines(ctx, logChan, &lines, i, filePath, &countLine)
}) }()
} }
} }
@@ -255,22 +253,33 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
} }
}() }()
// 等待所有行处理完成并推送进度
for int(countLine) != fileLines { for int(countLine) != fileLines {
select { select {
case <-ctx.Done(): case <-ctx.Done():
wg.Wait() close(lines) //关闭processLines中的上传线程
return return
default: default:
progress.Store(filepath.Base(filePath), progress.Store(filepath.Base(filePath),
Progress{FileName: filepath.Base(filePath), Progress{
Total: fileLines, Uploaded: int(countLine), FileName: filepath.Base(filePath),
Percentage: int(float64(countLine)/float64(fileLines)*100) + 1, Total: fileLines,
}) Uploaded: int(countLine),
time.Sleep(500 * time.Millisecond) Percentage: int(float64(countLine) / float64(fileLines) * 100),
},
)
} }
} }
//上传完成,进度设为100
wg.Wait() progress.Store(filepath.Base(filePath),
Progress{
FileName: filepath.Base(filePath),
Total: fileLines,
Uploaded: int(countLine),
Percentage: 100,
},
)
close(lines) //关闭processLines中的上传线程
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
AddLog(logChan, fmt.Sprintf("读取文件 %s 错误: %v", filePath, err)) AddLog(logChan, fmt.Sprintf("读取文件 %s 错误: %v", filePath, err))
@@ -280,6 +289,7 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
AddLog(logChan, fmt.Sprintf("文件【%s】处理完成,共处理 %d 行数据", filepath.Base(filePath), countLine)) AddLog(logChan, fmt.Sprintf("文件【%s】处理完成,共处理 %d 行数据", filepath.Base(filePath), countLine))
} }
// processLines 处理接受到的每一行数据并上传(chan 管道接受数据)
func processLines(ctx context.Context, logChan *chan string, lines *chan string, workerID int, filePath string, countLine *int32) { func processLines(ctx context.Context, logChan *chan string, lines *chan string, workerID int, filePath string, countLine *int32) {
for line := range *lines { for line := range *lines {
select { select {