| 541 | } |
| 542 | |
| 543 | func (c *Client) Close() { |
| 544 | if !atomic.CompareAndSwapInt32(&c.closed, 0, 1) { |
| 545 | return // TODO: ErrAlreadyClosed |
| 546 | } |
| 547 | |
| 548 | c.muReconnect.Lock() |
| 549 | // TODO(rjeczalik): add another internal field for controlling redials |
| 550 | // instead of mutating public field |
| 551 | c.Reconnect = false |
| 552 | c.muReconnect.Unlock() |
| 553 | |
| 554 | close(c.closeChan) |
| 555 | |
| 556 | if c.closeRenewer != nil { |
| 557 | select { |
| 558 | case c.closeRenewer <- struct{}{}: |
| 559 | default: |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | // wait for consumers to finish buffered messages |
| 564 | c.wg.Wait() |
| 565 | |
| 566 | if session := c.getSession(); session != nil { |
| 567 | session.Close(3000, "Go away!") |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | // sendhub sends the msg received from the send channel to the remote client |
| 572 | func (c *Client) sendHub() { |