You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
992 B
Go

package routine
type T any
type options struct {
workers int
capacity int
taskFn func(T)
panicHandler func(any)
}
// Option function
type Option func(*options)
// loadOptions 设置默认值
func loadOptions(opt ...Option) options {
opts := options{
workers: 1,
capacity: 1,
}
for _, o := range opt {
o(&opts)
}
return opts
}
// WithWorkers 设置协程数
func WithWorkers(workers int) Option {
if workers <= 0 {
workers = 1
}
return func(o *options) {
o.workers = workers
}
}
// WithCapacity 设置任务队列容量
func WithCapacity(capacity int) Option {
if capacity <= 0 {
capacity = 1
}
return func(o *options) {
o.capacity = capacity
}
}
// WithTaskFn 设置任务函数
func WithTaskFn(taskFn func(T)) Option {
return func(o *options) {
o.taskFn = taskFn
}
}
// WithPanicHandler 设置panic处理函数
func WithPanicHandler(panicHandler func(any)) Option {
return func(o *options) {
o.panicHandler = panicHandler
}
}