Remember once Golang routine to die

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

The most convenient part of the go language is the freedom to get up and routine without having to maintain the queue yourself.

A very simple processing model, for raised here connection activity trivial link independent routine processing, convenient for the same connection context, read routine a received message unpacking generated message dropped to the corresponding socket in the routine B channel for processing, Routine B is disposed of in accordance with different tasks to the corresponding routine B1 or routine B2,

We need a channel back write routine B1 routine B2 exit signal to you B so that B responds to processing.

Well, then, if you think so, B and B1 B2 need two channel for message communication

The pseudo code is as follows, not friendly to handle.

varB1 B1consumervarB2 B2consumer//statch Writeback routine statusStatch: = Make (Chan statmsg,1) b1ch:= Make (Chan Msg, -) B2ch:= Make (Chan Msg, -) Go B1 (b1ch,statch) go B2 (b2ch,statch) for{    Select{     CaseCmsg, OK: = <-BCh:ifCmsg.type = =b1{b1ch<-Cmsg}Else{b2ch<-Cmsg} CaseStatmsg: = <-statch: Deal_state (msg)}}

At first glance there is nothing wrong with the subsequent test process occasionally discovering that the calling interface does not respond to timeouts.

After careful investigation, found that B1 routine on the exit wrote a done statmsg, and then quit.

This thing BCH still writing data if at this time b1ch write full, then write channel will die, and B1 exit signal has been sent not processing new data, then B routine will have been stuck in

B1ch<-cmsg

At this time already B routine can not carry out the next select operation, and so the BCH channel write full card dead read routine, and so many read routine card, the client performance is no response timeout.

It's easy to find a solution to the problem.

Increase the Write channel timeout and increase the size of the Statch

Statch: =Select  {            case b1ch <- cmsg:            case <-time  . After (time. Duration (closetimeout) * Time . Second):}

Increase the channel size to avoid multiple triggering timeouts, if a timeout occurs, the task will be freed.

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.