https://github.com/pythonista7/ratelimited
https://github.com/pythonista7/ratelimited
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/pythonista7/ratelimited
- Owner: Pythonista7
- License: apache-2.0
- Created: 2023-01-14T19:31:19.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-15T03:01:59.000Z (about 3 years ago)
- Last Synced: 2025-04-01T16:55:25.209Z (10 months ago)
- Language: Go
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ratelimited
Just a simple wrapper over [ratelimit](https://github.com/uber-go/ratelimit). :)
## Example
See a working example [here](github.com/pythonista7/ratelimited/example/example.go). But here's the idea:
```go
func main() {
const targetRPM int = 100
queue := make(chan (string), 25)
/*
This will create an instance of the ratelimitedworker.
- id: primary identifier for the ratelimiter Job the work is assosiated to
- targetRPM: the expected maximum rate limit to perform at
- hasty: if true maintains a higher RPS good for work() that takes long, false forces rate below the limit.
- verbose: enable logging
*/
rlw := ratelimitedworker.Create("sampleId", targetRPM, true, true)
go queueLoader(queue)
go doLotsOfWork(rlw,queue)
}
func doLotsOfWork(rlw *ratelimitedworker.RLW, queue chan (string)) {
for range queue {
// this will allow the work() to be called only rlw.targetRPM number of times during a minute
rlw.Track() // if limit is hit for the time period it will block this go routine
go work()
}
}
```