项目作者: ninedraft

项目描述 :
A simple concurrency limiter
高级语言: Go
项目地址: git://github.com/ninedraft/limiter.git
创建时间: 2017-09-26T19:03:58Z
项目社区:https://github.com/ninedraft/limiter

开源协议:MIT License

下载


limiter

A dead simple goroutine limiter.
Example:

  1. names := []string{"John", "Ada", "Merlin", "Tanya"}
  2. // parameter is a limit
  3. // .Start() method blocks until number
  4. // of running goroutines is reduced.
  5. limit := limiter.New(2)
  6. for _, name := range names {
  7. go func(name string, done func()) {
  8. defer done()
  9. fmt.Printf("Hello, %s!\n", name)
  10. // .Start()) blocks f the number of workers
  11. // approaches the specified limit,
  12. // then waits until the number of active workers decreases.
  13. }(name, limit.Start())
  14. }
  15. // .Wait() blocks until all tasks are completed.
  16. limit.Wait()

To use limiter import it in your project or just copy-paste source code