Created
June 30, 2015 14:20
-
-
Save liggitt/0bbf37f71ffb21ac169d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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