https://github.com/huttotw/warm-up
A implementation of a rate limiter that will allow the rate to increase over the course of time according to some function.
https://github.com/huttotw/warm-up
channels golang rate time up warm
Last synced: 9 months ago
JSON representation
A implementation of a rate limiter that will allow the rate to increase over the course of time according to some function.
- Host: GitHub
- URL: https://github.com/huttotw/warm-up
- Owner: huttotw
- License: apache-2.0
- Created: 2019-03-15T17:36:29.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-18T15:08:36.000Z (about 7 years ago)
- Last Synced: 2025-08-14T13:52:59.782Z (10 months ago)
- Topics: channels, golang, rate, time, up, warm
- Language: Go
- Size: 7.81 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# warm-up
Warm up is a pseudo implementation of `golang.org/x/time/rate` with the biggest difference being that you can supply a
custom function with which to calculate the rate at every token. Please see the [Golang documentation](https://godoc.org/golang.org/x/time/rate) for details on the
terminology used here, especially _Limiter_, _tokens_, and _bucket_.
### Use cases:
- Sending lots of requests to a system, but you want to give the system a chance to auto-scale.
- Warming up a load balancer to be ready to handle a lot of requests.
- Gradually increasing load to a system.
## Example
```go
func main() {
fmt.Println("starting...")
// Define our custom function
f := func(x float64) float64 {
return math.Pow(x/10, 2)
}
// Create a new limiter that will increase in throughput according to (x/10)^2,
// with a burst up to 0 tokens.
l := warmup.NewLimiter(f, 0)
defer l.Stop()
i := 0
for {
ctx := context.Background()
l.WaitN(ctx, 1)
fmt.Println("i", i)
i++
}
}
```
This example will print out at each iteration, slowly increasing over time.
## To Do
- [ ] Implement `AllowN`
- [ ] Implement `ReserveN`
- [ ] Handle cancellations and deadlines properly.
## License
Copyright © 2019 Trevor Hutto
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.