perf: 并发worker数可配置(默认4)与MIME检测并行化
This commit is contained in:
@@ -5,6 +5,10 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/magiconair/properties"
|
||||
)
|
||||
|
||||
@@ -14,12 +18,14 @@ import (
|
||||
// - AutoOpen:启动后要不要自动打开浏览器
|
||||
// - FFmpegPath:ffmpeg 程序路径(用于把带 EXIF 旋转标记的图片转正);留空自动在 PATH 里找
|
||||
// - RotateImages:整理时是否自动转正带 EXIF 旋转标记的图片(默认 true)
|
||||
// - Workers:探测/MIME/移动阶段的并发 worker 数(默认 4;设 0 表示用满全部 CPU 核数)
|
||||
type Config struct {
|
||||
Port int
|
||||
Dsn string
|
||||
AutoOpen bool
|
||||
FFmpegPath string
|
||||
RotateImages bool
|
||||
Workers int
|
||||
}
|
||||
|
||||
// appConfig 当前生效的配置(程序运行期间一直不变)。
|
||||
@@ -33,7 +39,7 @@ func GetAppConfig() Config {
|
||||
// LoadAppConfig 从 application.properties 读配置;文件不存在就用默认值。
|
||||
func LoadAppConfig() {
|
||||
// 先给一套默认值
|
||||
appConfig = Config{Port: 8080, Dsn: "media-info.db", AutoOpen: true, RotateImages: true}
|
||||
appConfig = Config{Port: 8080, Dsn: "media-info.db", AutoOpen: true, RotateImages: true, Workers: 4}
|
||||
|
||||
p, err := properties.LoadFile("application.properties", properties.UTF8)
|
||||
if err != nil {
|
||||
@@ -52,4 +58,24 @@ func LoadAppConfig() {
|
||||
appConfig.FFmpegPath = v
|
||||
}
|
||||
appConfig.RotateImages = p.GetBool("organize.rotateImages", true)
|
||||
// 并发 worker 数:只认非负整数,填了别的就保持默认
|
||||
if v, ok := p.Get("organize.workers"); ok && strings.TrimSpace(v) != "" {
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(v)); err == nil && n >= 0 {
|
||||
appConfig.Workers = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WorkerCount 返回探测/MIME/移动阶段使用的并发 worker 数:
|
||||
// - 配置里 organize.workers > 0 时用配置值(默认 4);
|
||||
// - 配置为 0 时用满全部 CPU 核数。
|
||||
func WorkerCount() int {
|
||||
if appConfig.Workers > 0 {
|
||||
return appConfig.Workers
|
||||
}
|
||||
n := runtime.NumCPU()
|
||||
if n < 1 {
|
||||
return 1
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user