Skip to content

Instantly share code, notes, and snippets.

@miss-invincible
Created August 1, 2019 09:27
Show Gist options
  • Save miss-invincible/bc1bf64d2f0c6406b0c8671cb7751f0a to your computer and use it in GitHub Desktop.
Save miss-invincible/bc1bf64d2f0c6406b0c8671cb7751f0a to your computer and use it in GitHub Desktop.
This is a simple test to explore the functionality of go rate limiter. Here, I am trying to test it's behaviour in parallel and synchronous calling.
package main
import (
"sync"
"fmt"
"time"
"https://github.com/uber-go/ratelimit"
)
var prev time.Time
func testrateLimit() {
now := time.Now()
time.Sleep(300*time.Millisecond)
fmt.Println("heelo", now.Sub(prev))
prev = now
}
func main(){
call_parallel()
//call_sync()
}
func call_parallel(){
ratelimiter := ratelimit.New(5)
var wg sync.WaitGroup
prev = time.Now()
for i:=0;i<20;i++{
ratelimiter.Take()
wg.Add(1)
go func() {
defer wg.Done()
testrateLimit()
}()
}
wg.Wait()
}
func call_sync(){
ratelimiter := ratelimit.New(5)
prev = time.Now()
for i:=0;i<20;i++{
ratelimiter.Take()
testrateLimit()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment