Golang: Using Timingwheel for a large number of ticker optimizations

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Ticker

Recent projects with go implementation of the server need to mount a large number of socket connections. How to tell if a connection is still alive is a question we need to consider.

Normally, if the socket is close by the client normally, the server can be detected, but if the client suddenly unplug the network cable, or a power outage, then the status of the socket may still be established on the server. In fact, the socket is no longer available.

In order to determine whether the connection is available, usually we will use the timer mechanism to detect the timing, in go, which is very easy to implement, as follows:

ticker := time.NewTicker(60 * time.Second)for {    select {        case <-ticker.C:            if err := ping(); err != nil {                close()            }    }}

Above we use a 60s ticker, timed to ping, if the ping failed to prove that the connection has been disconnected, this time need to close.

This mechanism is relatively simple and works well until our server is connected to the 10w+. Because each connection has a ticker, so at the same time there will be a lot of ticker run, the CPU has been hovering around 30%, performance can not be accepted.

In fact, all we need is a set of efficient timeout notification mechanisms.

Close Channel to broadcast

In Go, the channel is a very good thing, we can broadcast by the close channel. As follows:

ch := make(bool)for i := 0; i < 10; i++ {    go func() {        println("begin")        <-ch        println("end")    }}time.Sleep(10 * time.Second)close(ch)

Above, we started 10 goroutine, all of them will be block,10s after the data of the CH and then close the channel, then all the goroutine waiting for the channel will continue to execute down.

Timingwheel

Through the channel this close broadcast mechanism, we can very conveniently implement a TIMER,TIMER has a channel CH, all need to be notified at a certain time "T" Goroutine can try to read the CH, when T arrives, Close the CH, then all the Goroutine can receive the event.

Timingwheel is very simple to use, first we create a wheel

//这里我们创建了一个timingwheel,精度是1s,最大的超时等待时间为3600sw := timingwheel.NewTimingWheel(1 * time.Second, 3600)//等待10s<-w.After(10 * time.Second)

Because timingwheel has only a 1s ticker and only 3,600 channel is created, the overhead is minimal. When our program changes to Timingwheel, the 10w+ connection CPU cost is below 10%, achieves the optimization effect.

Timingwheel code is here.

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.