mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-08 15:39:59 +08:00
29 lines
436 B
Go
29 lines
436 B
Go
|
|
package threading
|
||
|
|
|
||
|
|
import (
|
||
|
|
"zero/core/lang"
|
||
|
|
"zero/core/rescue"
|
||
|
|
)
|
||
|
|
|
||
|
|
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()
|
||
|
|
}()
|
||
|
|
}
|