perf: 并发worker数可配置(默认4)与MIME检测并行化
This commit is contained in:
@@ -11,8 +11,11 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"kenaito-media-picker/internal/config"
|
||||
"kenaito-media-picker/internal/probe"
|
||||
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
@@ -106,12 +109,35 @@ func Scan(opts Options) (*Result, error) {
|
||||
if opts.OnProgress != nil {
|
||||
opts.OnProgress(total, total)
|
||||
}
|
||||
for i := range res.Files {
|
||||
res.Files[i].Mime = detectMime(res.Files[i].Path)
|
||||
if opts.OnProgress != nil {
|
||||
opts.OnProgress(i+1, total)
|
||||
}
|
||||
// 并行认类型:每个 worker 只写自己名下那个元素(res.Files[i]),并发安全;
|
||||
// 认类型要读文件内容,数量多时串行太慢,并行能明显提速。
|
||||
workers := config.WorkerCount()
|
||||
if workers > total {
|
||||
workers = total
|
||||
}
|
||||
if workers < 1 {
|
||||
workers = 1
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
var done int32
|
||||
jobs := make(chan int)
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := range jobs {
|
||||
res.Files[i].Mime = detectMime(res.Files[i].Path)
|
||||
if opts.OnProgress != nil {
|
||||
opts.OnProgress(int(atomic.AddInt32(&done, 1)), total)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
for i := range res.Files {
|
||||
jobs <- i
|
||||
}
|
||||
close(jobs)
|
||||
wg.Wait()
|
||||
return res, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user