Created
May 8, 2018 19:10
-
-
Save thiagozs/57adbe576e5dac896d3b8247945fe87d to your computer and use it in GitHub Desktop.
Test implement limit rate
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" | |
"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