Skip to content

Instantly share code, notes, and snippets.

@liggitt
Created June 30, 2015 14:20
Show Gist options
  • Save liggitt/0bbf37f71ffb21ac169d to your computer and use it in GitHub Desktop.
Save liggitt/0bbf37f71ffb21ac169d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
// "github.com/juju/ratelimit"
)
func main() {
limiter := util.NewTokenBucketRateLimiter(150, 300)
// limiter := ratelimit.NewBucketWithRate(150, 300)
// Keep track of our initial offset
startNano := time.Now().Nanosecond()
// Track how requests are spread across a 1 second interval
spread := [100]int{}
accepted := func() {
offsetNano := (time.Now().Nanosecond() + int(time.Second) - startNano) % int(time.Second)
bucket := offsetNano / 10000000
spread[bucket] = spread[bucket] + 1
}
go func() {
for {
limiter.Accept()
// limiter.Wait(1)
accepted()
}
}()
for range time.Tick(time.Second) {
fmt.Printf("%v\n", spread)
for i := range spread {
spread[i] = 0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment