taskrunner.go 482 B

12345678910111213141516171819202122232425262728
  1. package threading
  2. import (
  3. "github.com/tal-tech/go-zero/core/lang"
  4. "github.com/tal-tech/go-zero/core/rescue"
  5. )
  6. type TaskRunner struct {
  7. limitChan chan lang.PlaceholderType
  8. }
  9. func NewTaskRunner(concurrency int) *TaskRunner {
  10. return &TaskRunner{
  11. limitChan: make(chan lang.PlaceholderType, concurrency),
  12. }
  13. }
  14. func (rp *TaskRunner) Schedule(task func()) {
  15. rp.limitChan <- lang.Placeholder
  16. go func() {
  17. defer rescue.Recover(func() {
  18. <-rp.limitChan
  19. })
  20. task()
  21. }()
  22. }