Go Channe notes

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

Recently used go to write a crawler monitoring system, about 10 requests per second, the data after the database processing, and the data forwarded to the WebSocket to the front-end display.

There is no problem at the beginning of the test, but an online acceptance of the real data will run half an hour after the failure to respond to the service and out I have a print request body in the print information my other logic is not running, the first baffled.

At the beginning my code was like this:

func (this *MainController) Post() {    defer this.Ctx.Request.Body.Close()    body, err := ioutil.ReadAll(this.Ctx.Request.Body)    fmt.Println(string(body))    go RecodeBody(body)    if err == nil && body != nil {        models.Messages <- body:        this.Data["json"] = "ok"    } else {        this.Data["json"] = "fail"    }    this.ServeJSON()}

Then I recodebody the () method after some logical processing and it is possible to models before the database operation. Write in messages.
Models. Messages is a channel with 1000 buffers and is a cache that forwards messages to websocket.
After the first actual data test run half an hour after the case only promised to ask the body after thinking for a long time finally found the problem:
The problem is that when writing information to WebSocket, if a message might take more than one day to process because of the network or the number of connections, the channel's cache fills up for half an hour, causing a blockage, so this. Servejson () cannot be run so information cannot be returned. The message should then be written to the channel in my logic handler as well, causing a blockage. So the illusion that my server is about equal to dead center is caused.
I think the main reason I'm having this problem is that I don't know enough about go channel and I'm eager to implement it when I write code without paying attention to the logic and quality of the code.
Know the problem is always a good solution, so I send the message to messages all to the recodebody () function, and also through a channel to give it hull data, and send a message to the channel plus default to avoid clogging.

func (this *MainController) Post() {    defer this.Ctx.Request.Body.Close()    body, err := ioutil.ReadAll(this.Ctx.Request.Body)    fmt.Println(string(body))    if err == nil && body != nil {        select {        case models.PS <- body:            this.Data["json"] = "ok"        default:            this.Data["json"] = "server is busy"        }    } else {        this.Data["json"] = "fail"    }    this.ServeJSON()}

In any case, there will be no response, unless your machine is really overwhelmed by the amount of concurrency.

Summarize:
When a channel is used when processing a certain amount of concurrency, it is used to write information to the channel.
select {
case models.PS <- body:
this.Data["json"] = "ok"
default:
this.Data["json"] = "server is busy"
}

Format, so that when the channel because the consumption is not timely caused by the cache of the situation without prompting information, so you can not trace the problem.

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.