// start listening MQ message service // reconnection implemented method FUNC (I * The Listener) the Start () (ERR error) { i.done = the make (Chan interface {}, i.workerNum) // confirm each coroutine whether to end rechan: = the make (Chan interface {}, i.workerNum) // reconnection flag is running: = to true for K: = 0 ; K <i.workerNum; K ++ { Go ListenMsg (i.done, rechan, & running) } Go rECONNECT (I, rechan) // start reconnection coroutine return nil } FUNC ListenMsg (DONE Chan <-interface{}, rechan chan<- interface{}, run *bool) { defer done <- struct{}{} msgs := Consume() process := func() { if *run == false { return } msg, ok := <-msgs if !ok { *run = false rechan <- struct{}{} //启动重连 } //do something } for * RUN { Process () } } FUNC RECONNECT (I * The Listener, rechan <-chan interface {}) { <- rechan // use Fibonacci number Fibonacci sequence of time intervals to reconnect for ! i.closed { ERR: = i.Start () IF ! ERR = nil { time.sleep (fibonacci.time) } the else { return } } } FUNC (I * the listener) the Close () { // wait for all listen queue coroutine end for I: = 0; i < i.workerNum; i++ { <-i.done } }