You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
895 B

2 years ago
  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. }