routinetoken.go 529 B

1234567891011121314151617181920212223242526272829303132
  1. package coroutine
  2. import (
  3. "leafstalk/log"
  4. "leafstalk/otherutils"
  5. )
  6. type LimitNRoutine struct {
  7. routineTokens otherutils.RoutineTokens
  8. }
  9. func NewLimitNRoutine(routineNum int) *LimitNRoutine {
  10. lr := new(LimitNRoutine)
  11. lr.routineTokens.Init(routineNum)
  12. return lr
  13. }
  14. // 协程加载
  15. func (r *LimitNRoutine) Go(fprocess func()) {
  16. go func() {
  17. r.routineTokens.Get()
  18. defer func() {
  19. r.routineTokens.Release()
  20. err := recover()
  21. if err != nil {
  22. log.Errorf("Go run error: %s", err)
  23. }
  24. }()
  25. fprocess()
  26. }()
  27. }