MCPcopy
hub / github.com/nats-io/nats.go / processConnectInit

Method processConnectInit

nats.go:2725–2776  ·  view source on GitHub ↗

Process a connected connection and initialize properly.

()

Source from the content-addressed store, hash-verified

2723
2724// Process a connected connection and initialize properly.
2725func (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.
2779func (nc *Conn) connect() (bool, error) {

Callers 2

connectMethod · 0.95
doReconnectMethod · 0.95

Calls 11

changeConnStatusMethod · 0.95
makeTLSConnMethod · 0.95
processExpectedInfoMethod · 0.95
tlsHandshakeEOFMethod · 0.95
sendConnectMethod · 0.95
readLoopMethod · 0.95
flusherMethod · 0.95
SetDeadlineMethod · 0.80
AddMethod · 0.65
ResetMethod · 0.65
doneWithConnectMethod · 0.45

Tested by

no test coverage detected