QPS control that can be used in the field (Rate Limit) Limits execution to n times per second

Example when you want to execute 10 times per second

Use https://godoc.org/golang.org/x/time/rate

func main() {
	limiter := rate.NewLimiter(rate.Limit(10), 1)
	ctx := context.Background()

	for i := 0; i < 1000; i++ {
		if err := limiter.Wait(ctx); err != nil {
			fmt.Println(err)
		}
		fmt.Println(i)
	}
}

https://play.golang.org/p/ByAz9VCkGFk

reference

https://qiita.com/lufia/items/29bf1aeb0a0fe69d16f0 https://blog.lufia.org/entry/2016/08/28/000000

Recommended Posts

QPS control that can be used in the field (Rate Limit) Limits execution to n times per second
Goroutine (parallel control) that can be used in the field
Goroutine that can be used in the field (errgroup.Group edition)
A timer (ticker) that can be used in the field (can be used anywhere)
Functions that can be used in for statements
Basic algorithms that can be used in competition pros
Simple statistics that can be used to analyze the effect of measures on EC sites and codes that can be used in jupyter notebook
[Python3] Code that can be used when you want to resize images in folder units
How to set variables that can be used throughout the Django app-useful for templates, etc.-
Scripts that can be used when using bottle in Python
I tried to expand the database so that it can be used with PES analysis software
Python standard input summary that can be used in competition pro
Python standard module that can be used on the command line
Can the Kalman filter be used to predict stock price trends?
Solution to the problem that Ctrl + z cannot be used in Powershell in Docker for windows environment (provisional)