https://github.com/korovkin/limiter
go lang concurrency limiter.
https://github.com/korovkin/limiter
go golang goroutine goroutine-pool
Last synced: 28 days ago
JSON representation
go lang concurrency limiter.
- Host: GitHub
- URL: https://github.com/korovkin/limiter
- Owner: korovkin
- License: mit
- Created: 2017-05-13T17:15:41.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-04-07T19:32:09.000Z (about 2 years ago)
- Last Synced: 2024-08-02T06:23:11.437Z (9 months ago)
- Topics: go, golang, goroutine, goroutine-pool
- Language: Go
- Size: 25.4 KB
- Stars: 274
- Watchers: 9
- Forks: 36
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go lang goroutine concurrency limiter
## builds
[](https://travis-ci.org/korovkin/limiter)
## Example
limit the number of concurrent go routines to 10:
```
import "github.com/korovkin/limiter"...
limit := limiter.NewConcurrencyLimiter(10)
defer limit.WaitAndClose()for i := 0; i < 1000; i++ {
limit.Execute(func() {
// do some work
})
}
```## Real World Example:
```
import "github.com/korovkin/limiter"...
limiter := limiter.NewConcurrencyLimiter(10)
httpGoogle := int(0)
limiter.Execute(func() {
resp, err := http.Get("https://www.google.com/")
Expect(err).To(BeNil())
defer resp.Body.Close()
httpGoogle = resp.StatusCode
})httpApple := int(0)
limiter.Execute(func() {
resp, err := http.Get("https://www.apple.com/")
Expect(err).To(BeNil())
defer resp.Body.Close()
httpApple = resp.StatusCode
})limiter.WaitAndClose()
log.Println("httpGoogle:", httpGoogle)
log.Println("httpApple:", httpApple)
```## Concurrent IO with Error tracking:
```
import "github.com/korovkin/limiter"
...
a := errors.New("error a")
b := errors.New("error b")concurrently := limiter.NewConcurrencyLimiterForIO(limiter.DefaultConcurrencyLimitIO)
concurrently.Execute(func() {
// Do some really slow IO ...
// keep the error:
concurrently.FirstErrorStore(a)
})
concurrently.Execute(func() {
// Do some really slow IO ...
// keep the error:
concurrently.FirstErrorStore(b)
})
concurrently.WaitAndClose()firstErr := concurrently.FirstErrorGet()
Expect(firstErr == a || firstErr == b).To(BeTrue())```