limited_executor.go 895 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package util
  2. // initial version comes from https://github.com/korovkin/limiter/blob/master/limiter.go
  3. // LimitedConcurrentExecutor object
  4. type LimitedConcurrentExecutor struct {
  5. limit int
  6. tokenChan chan int
  7. }
  8. func NewLimitedConcurrentExecutor(limit int) *LimitedConcurrentExecutor {
  9. // allocate a limiter instance
  10. c := &LimitedConcurrentExecutor{
  11. limit: limit,
  12. tokenChan: make(chan int, limit),
  13. }
  14. // allocate the tokenChan:
  15. for i := 0; i < c.limit; i++ {
  16. c.tokenChan <- i
  17. }
  18. return c
  19. }
  20. // Execute adds a function to the execution queue.
  21. // if num of go routines allocated by this instance is < limit
  22. // launch a new go routine to execute job
  23. // else wait until a go routine becomes available
  24. func (c *LimitedConcurrentExecutor) Execute(job func()) {
  25. token := <-c.tokenChan
  26. go func() {
  27. defer func() {
  28. c.tokenChan <- token
  29. }()
  30. // run the job
  31. job()
  32. }()
  33. }