限制并发的goroutine数量

This commit is contained in:
Yun
2025-10-14 11:13:12 +08:00
parent 1b9e9b757b
commit de3568de42
4 changed files with 38 additions and 11 deletions
+12 -1
View File
@@ -14,7 +14,8 @@ type Options struct {
usePriority bool
priorityVal int64
batchSize int
maxRunCount int // 单个任务最大运行次数 0 代表不限
maxRunCount int // 单个任务最大运行次数 0代表不限
maxWorkers int // 最大工作协程数
cronParser *cron.Parser // cron表达式解析器
}
@@ -31,6 +32,7 @@ func defaultOptions() Options {
priorityVal: 0,
batchSize: 100,
maxRunCount: 0,
maxWorkers: 100,
cronParser: &parser,
}
}
@@ -102,6 +104,15 @@ func WithMaxRetryCount(count int) Option {
}
}
func WithMaxWorkers(count int) Option {
return func(o *Options) {
if count < 0 {
count = 10
}
o.maxWorkers = count
}
}
// 添加cron表达式解析器
func WithCronParser(parser cron.Parser) Option {
return func(o *Options) {