initial import
This commit is contained in:
28
core/threading/taskrunner.go
Normal file
28
core/threading/taskrunner.go
Normal file
@@ -0,0 +1,28 @@
|
||||
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()
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user