Skip to content

Instantly share code, notes, and snippets.

@knobunc
Last active April 12, 2017 19:51
Show Gist options
  • Save knobunc/c03ec0c70ec23f79129b8d7f1584aaa1 to your computer and use it in GitHub Desktop.
Save knobunc/c03ec0c70ec23f79129b8d7f1584aaa1 to your computer and use it in GitHub Desktop.
Test program for ratelimiter
$ go run test.go
Started at 2017-04-12 11:49:21.515360854 -0400 EDT
Invoking at 2017-04-12 11:49:21.515439764 -0400 EDT
Invoked at 2017-04-12 11:49:21.515464339 -0400 EDT
Called at 2017-04-12 11:49:21.515469714 -0400 EDT
Invoking at 2017-04-12 11:49:22.515583917 -0400 EDT
Invoking at 2017-04-12 11:49:23.51571878 -0400 EDT
Invoking at 2017-04-12 11:49:24.515809571 -0400 EDT
Invoking at 2017-04-12 11:49:25.515949779 -0400 EDT
Returned at 2017-04-12 11:49:26.515627721 -0400 EDT
Invoked at 2017-04-12 11:49:26.515670754 -0400 EDT
Invoked at 2017-04-12 11:49:26.515689396 -0400 EDT
Invoked at 2017-04-12 11:49:26.515706142 -0400 EDT
Invoked at 2017-04-12 11:49:26.515711263 -0400 EDT
Called at 2017-04-12 11:49:26.515833206 -0400 EDT
Invoking at 2017-04-12 11:49:26.516058019 -0400 EDT
Invoking at 2017-04-12 11:49:27.516254957 -0400 EDT
Invoking at 2017-04-12 11:49:28.516314391 -0400 EDT
Invoking at 2017-04-12 11:49:29.516441726 -0400 EDT
Invoking at 2017-04-12 11:49:30.516592092 -0400 EDT
Returned at 2017-04-12 11:49:31.515928309 -0400 EDT
Invoked at 2017-04-12 11:49:31.515973959 -0400 EDT
Invoked at 2017-04-12 11:49:31.515984402 -0400 EDT
Invoked at 2017-04-12 11:49:31.516001367 -0400 EDT
Invoked at 2017-04-12 11:49:31.51600642 -0400 EDT
Invoked at 2017-04-12 11:49:31.516011045 -0400 EDT
Called at 2017-04-12 11:49:31.516024112 -0400 EDT
Returned at 2017-04-12 11:49:36.516138415 -0400 EDT
Done at 2017-04-12 11:49:51.516849601 -0400 EDT
Called 3 times
package main
import (
"fmt"
"github.com/openshift/origin/pkg/util/ratelimiter"
"sync"
"time"
)
var runFreq = 3 // One every N seconds
var pauseSecs = 1 // Wait N seconds between kicking off tasks
var workSecs = 5 // How long to sleep in the work task
var totalSecs = 20 // Wait this long for all work
func main() {
keyFunc := func(_ interface{}) (string, error) {
return "blah", nil
}
h := &handler{}
quit := make(chan struct{})
rlf := ratelimiter.NewRateLimitedFunction(keyFunc, runFreq, h.handle)
rlf.RunUntil(quit)
fmt.Printf("Started at %s\n", time.Now())
for i := 0; i < 10; i++ {
go func() {
fmt.Printf(" Invoking at %s\n", time.Now())
rlf.Invoke(i)
fmt.Printf(" Invoked at %s\n", time.Now())
}()
time.Sleep(time.Duration(pauseSecs) * time.Second)
}
select {
case <-time.After(time.Duration(totalSecs) * time.Second):
close(quit)
fmt.Printf("Done at %s\n\nCalled %d times\n", time.Now(), h.counter())
}
}
type handler struct {
_counter int
sync.Mutex
}
func (h *handler) handle() error {
h.Lock()
defer h.Unlock()
h._counter += 1
fmt.Printf(" Called at %s\n", time.Now())
time.Sleep(time.Duration(workSecs) * time.Second)
fmt.Printf(" Returned at %s\n", time.Now())
return nil
}
func (h *handler) counter() int {
h.Lock()
defer h.Unlock()
return h._counter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment