Loop should be run as condition for `for` with receiving from (*Client).Errors() It will manage AMQP connection, run queue and exchange declarations, consumers. Will start to return false once (*Client).Close() called.
()
| 111 | // It will manage AMQP connection, run queue and exchange declarations, consumers. |
| 112 | // Will start to return false once (*Client).Close() called. |
| 113 | func (c *Client) Loop() bool { |
| 114 | var ( |
| 115 | err error |
| 116 | ) |
| 117 | |
| 118 | if atomic.LoadInt32(&c.run) == noRun { |
| 119 | return false |
| 120 | } |
| 121 | |
| 122 | conn, _ := c.conn.Load().(*amqp.Connection) |
| 123 | |
| 124 | if conn != nil { |
| 125 | return true |
| 126 | } |
| 127 | |
| 128 | if c.bo != nil { |
| 129 | time.Sleep(c.bo.Backoff(int(c.attempt))) |
| 130 | atomic.AddInt32(&c.attempt, 1) |
| 131 | } |
| 132 | |
| 133 | // set default Heartbeat to 10 seconds like in original amqp.Dial |
| 134 | if c.config.Heartbeat == 0 { |
| 135 | c.config.Heartbeat = 10 * time.Second |
| 136 | } |
| 137 | |
| 138 | conn, err = amqp.DialConfig(c.addr, c.config) |
| 139 | |
| 140 | if c.reportErr(err) { |
| 141 | return true |
| 142 | } |
| 143 | c.conn.Store(conn) |
| 144 | |
| 145 | atomic.StoreInt32(&c.attempt, 0) |
| 146 | |
| 147 | // guard conn |
| 148 | go func() { |
| 149 | chanErr := make(chan *amqp.Error) |
| 150 | chanBlocking := make(chan amqp.Blocking) |
| 151 | conn.NotifyClose(chanErr) |
| 152 | conn.NotifyBlocked(chanBlocking) |
| 153 | |
| 154 | // loop for blocking/deblocking |
| 155 | for { |
| 156 | select { |
| 157 | case err1 := <-chanErr: |
| 158 | c.reportErr(err1) |
| 159 | |
| 160 | if conn1 := c.conn.Load().(*amqp.Connection); conn1 != nil { |
| 161 | c.conn.Store((*amqp.Connection)(nil)) |
| 162 | conn1.Close() |
| 163 | } |
| 164 | // return from routine to launch reconnect process |
| 165 | return |
| 166 | case blocking := <-chanBlocking: |
| 167 | select { |
| 168 | case c.blocking <- blocking: |
| 169 | default: |
| 170 | } |