Basic idea: define a chan, the size of chan is the qps size that needs to be limited, and a coroutine starts a tick, writes a numerical value in the tick every 1000/qps time, starts another coroutine, reads the value in chan, and if there is a value in chan, sends a request to the down-level interface.
The code is as follows:
package main import ( "fmt" "time" "httpclient" ) var LEN int = 10 func tickStoreCh(arrlen int, ch chan int) { len := 1000/arrlen (len) tickTime := ((len)*) var i int for { (len) i++ <- ch<- i } } func OrganReq(org string, qps int) { ch := make(chan int, qps) go tickStoreCh(qps, ch) (1000*) for { //Check customer requests and send http requests to RE client := ((1000)*, (2000)*) header := make(map[string]string) header["Content-Type"] = "application/json;charset=utf-8" code, err := ("http://127.0.0.1:19988", header, "llltest") value := <- ch (code, value, err, "lenchan:", len(ch)) //() } }
This is the end of this article about the implementation code of golang simple token bucket algorithm. For more related golang token bucket algorithm content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!