Golang speed limit, time. Tick (time. SECOND*2) channel, continue after 2 seconds

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("FMT"    " Time") Func main () {requests:= Make (chanint,5)     forI: =1; I <=2; i++{Requests<-i} close (requests) limiter:= time. Tick (Time.millisecond * $)     forReq: =Range Requests {//Will loop two times and two values are sent in front of the requests channel<-limiter//execution here, it takes 200 milliseconds to continue execution, time. The FMT is defined above the Tick (timer). Println ("Request", req, time. Now ())} Burstylimiter:= Make (chan time. Time,3)     forI: =0; I <3; i++{Burstylimiter<-Time . Now ()//Here Burstylimiter Channel continuously sends three values} go func () { forT: = range time. Tick (time. Second *2) {Burstylimiter<-t//here toward the Burstylimiter channel loop to send time. Tick}} () Burstyrequests:= Make (chanint,5)     forI: =1; I <=5; i++{burstyrequests<-I<-Burstylimiter//First three times no speed limit, will print directly out the contents of the println later FMT. Println ("Request", I, time. Now ())} close (burstyrequests) forReq: =Range Burstyrequests {<-Burstylimiter//continues to receive burstylimiter values, except for the first three times, followed by time. Tick carries a speed limit of 2 seconds until the end of the FMT cycle. Println ("Request", req, time. Now ())}}

Note, put time. Tick (time. Second) will make it easier to see the effect by changing the value in seconds

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.