go-zero/core/threading/taskrunner.go

29 lines
482 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package threading
import (
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/rescue"
2020-07-26 17:09:05 +08:00
)
type TaskRunner struct {
limitChan chan lang.PlaceholderType
}
func NewTaskRunner(concurrency int) *TaskRunner {
return &TaskRunner{
limitChan: make(chan lang.PlaceholderType, concurrency),
}
}
func (rp *TaskRunner) Schedule(task func()) {
rp.limitChan <- lang.Placeholder
go func() {
defer rescue.Recover(func() {
<-rp.limitChan
})
task()
}()
}