Process a connected connection and initialize properly.
()
| 2723 | |
| 2724 | // Process a connected connection and initialize properly. |
| 2725 | func (nc *Conn) processConnectInit() error { |
| 2726 | // Set our deadline for the whole connect process |
| 2727 | nc.conn.SetDeadline(time.Now().Add(nc.Opts.Timeout)) |
| 2728 | defer nc.conn.SetDeadline(time.Time{}) |
| 2729 | |
| 2730 | // Set our status to connecting. |
| 2731 | nc.changeConnStatus(CONNECTING) |
| 2732 | |
| 2733 | // If we need to have a TLS connection and want the TLS handshake to occur |
| 2734 | // first, do it now. |
| 2735 | if nc.Opts.Secure && nc.Opts.TLSHandshakeFirst { |
| 2736 | if err := nc.makeTLSConn(); err != nil { |
| 2737 | return err |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | // Process the INFO protocol received from the server |
| 2742 | err := nc.processExpectedInfo() |
| 2743 | if err != nil { |
| 2744 | return nc.tlsHandshakeEOF(err) |
| 2745 | } |
| 2746 | |
| 2747 | // Send the CONNECT protocol along with the initial PING protocol. |
| 2748 | // Wait for the PONG response (or any error that we get from the server). |
| 2749 | err = nc.sendConnect() |
| 2750 | if err != nil { |
| 2751 | return nc.tlsHandshakeEOF(err) |
| 2752 | } |
| 2753 | |
| 2754 | // Reset the number of PING sent out |
| 2755 | nc.pout = 0 |
| 2756 | |
| 2757 | // Start or reset Timer |
| 2758 | if nc.Opts.PingInterval > 0 { |
| 2759 | if nc.ptmr == nil { |
| 2760 | nc.ptmr = time.AfterFunc(nc.Opts.PingInterval, nc.processPingTimer) |
| 2761 | } else { |
| 2762 | nc.ptmr.Reset(nc.Opts.PingInterval) |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | // Start the readLoop and flusher go routines, we will wait on both on a reconnect event. |
| 2767 | nc.wg.Add(2) |
| 2768 | go nc.readLoop() |
| 2769 | go nc.flusher() |
| 2770 | |
| 2771 | // Notify the reader that we are done with the connect handshake, where |
| 2772 | // reads were done synchronously and under the connection lock. |
| 2773 | nc.br.doneWithConnect() |
| 2774 | |
| 2775 | return nil |
| 2776 | } |
| 2777 | |
| 2778 | // Main connect function. Will connect to the nats-server. |
| 2779 | func (nc *Conn) connect() (bool, error) { |
no test coverage detected