Skip to content

Instantly share code, notes, and snippets.

@thiagozs
Created May 8, 2018 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagozs/57adbe576e5dac896d3b8247945fe87d to your computer and use it in GitHub Desktop.
Save thiagozs/57adbe576e5dac896d3b8247945fe87d to your computer and use it in GitHub Desktop.
Test implement limit rate
package main
import (
"fmt"
"log"
"time"
)
func main() {
interval := float64(1000) // start limit interval
ticker := time.NewTicker(time.Duration(interval) * time.Millisecond)
go func() {
gas := 30.0 //total request queue
for {
select {
case <-ticker.C:
//check if gas is over.
if gas <= 0 {
gas = 1.0
interval += interval // add slow to next request
log.Println("ticker slow down to " + fmt.Sprint(interval/gas) + " ms")
} else {
log.Println("ticker accelerate " + fmt.Sprint(interval/gas) + " ms")
}
ticker = time.NewTicker(time.Duration(interval/gas) * time.Millisecond)
gas-- //decrement gas
}
}
log.Println("stopped")
}()
time.Sleep(40 * time.Second)
log.Println("stopping ticker")
ticker.Stop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment