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.