Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yudppp/throttle
lodash throttle like Go library
https://github.com/yudppp/throttle
Last synced: about 1 month ago
JSON representation
lodash throttle like Go library
- Host: GitHub
- URL: https://github.com/yudppp/throttle
- Owner: yudppp
- License: mit
- Created: 2019-10-25T14:30:38.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-08-24T15:15:43.000Z (over 3 years ago)
- Last Synced: 2024-06-19T18:07:59.171Z (6 months ago)
- Language: Go
- Size: 19.5 KB
- Stars: 37
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-extra - throttle - 10-25T14:30:38Z|2021-08-24T15:15:43Z| (Utilities / Fail injection)
README
# Throttle
![test workflow](https://github.com/yudppp/throttle/actions/workflows/test.yml/badge.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/yudppp/throttle)](https://goreportcard.com/report/github.com/yudppp/throttle)
[![Coverage Status](https://coveralls.io/repos/github/yudppp/throttle/badge.svg?branch=master)](https://coveralls.io/github/yudppp/throttle?branch=master)Throttle is an object that will perform exactly one action per duration.
Do call the function f if a specified duration has passed since the last function f was called for this instance of Throttle.## Examples
### single thread
```go
package mainimport (
"fmt"
"time""github.com/yudppp/throttle"
)func main() {
throttler := throttle.New(time.Second)
throttler.Do(func() {
fmt.Println("first call")
})
throttler.Do(func() {
// this function called never.
fmt.Println("second call")
})
time.Sleep(time.Second)
throttler.Do(func() {
fmt.Println("third call")
})
time.Sleep(time.Second)
}
``````
$ go run -race main.go
first call
third call
```### multiple threads
```go
package mainimport (
"fmt"
"time""github.com/yudppp/throttle"
)func main() {
throttler := throttle.New(time.Second)
var wg sync.WaitGroup
for i := 0; i < 64; i++ {
wg.Add(1)
go func(i int) {
throttler.Do(func() {
fmt.Println("called")
})
wg.Done()
}(i)
}
wg.Wait()
}
``````
$ go run -race main.go
called
```## License
[The MIT License (MIT)](https://github.com/yudppp/throttle/blob/master/LICENSE)