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

Method Connect

nats.go:1798–1879  ·  view source on GitHub ↗

Connect will attempt to connect to a NATS server with multiple options.

()

Source from the content-addressed store, hash-verified

1796
1797// Connect will attempt to connect to a NATS server with multiple options.
1798func (o Options) Connect() (*Conn, error) {
1799 nc := &Conn{Opts: o}
1800
1801 // Some default options processing.
1802 if nc.Opts.MaxPingsOut == 0 {
1803 nc.Opts.MaxPingsOut = DefaultMaxPingOut
1804 }
1805 // Allow old default for channel length to work correctly.
1806 if nc.Opts.SubChanLen == 0 {
1807 nc.Opts.SubChanLen = DefaultMaxChanLen
1808 }
1809 // Default ReconnectBufSize
1810 if nc.Opts.ReconnectBufSize == 0 {
1811 nc.Opts.ReconnectBufSize = DefaultReconnectBufSize
1812 }
1813 // Default WriteBufferSize
1814 if nc.Opts.WriteBufferSize <= 0 {
1815 nc.Opts.WriteBufferSize = DefaultWriteBufSize
1816 }
1817 // Ensure that Timeout is not 0
1818 if nc.Opts.Timeout == 0 {
1819 nc.Opts.Timeout = DefaultTimeout
1820 }
1821
1822 // Check first for user jwt callback being defined and nkey.
1823 if nc.Opts.UserJWT != nil && nc.Opts.Nkey != "" {
1824 return nil, ErrNkeyAndUser
1825 }
1826
1827 // Check if we have an nkey but no signature callback defined.
1828 if nc.Opts.Nkey != "" && nc.Opts.SignatureCB == nil {
1829 return nil, ErrNkeyButNoSigCB
1830 }
1831
1832 // Allow custom Dialer for connecting using a timeout by default
1833 if nc.Opts.Dialer == nil {
1834 nc.Opts.Dialer = &net.Dialer{
1835 Timeout: nc.Opts.Timeout,
1836 }
1837 }
1838
1839 // If the TLSHandshakeFirst option is specified, make sure that
1840 // the Secure boolean is true.
1841 if nc.Opts.TLSHandshakeFirst {
1842 nc.Opts.Secure = true
1843 }
1844
1845 if err := nc.setupServerPool(); err != nil {
1846 return nil, err
1847 }
1848
1849 // Create the async callback handler.
1850 nc.ach = &asyncCallbacksHandler{}
1851 nc.ach.cond = sync.NewCond(&nc.ach.mu)
1852
1853 // Set a default error handler that will print to stderr.
1854 if nc.Opts.AsyncErrorCB == nil {
1855 nc.Opts.AsyncErrorCB = defaultErrHandler

Callers 15

ExampleConnectFunction · 0.95
TestUseDefaultTimeoutFunction · 0.95
TestUseCustomDialerFunction · 0.95
TestWSNoMixingSchemeFunction · 0.80
ConnectFunction · 0.80
ExampleCustomDialerFunction · 0.80
ExampleConn_SubscribeFunction · 0.80

Calls 6

setupServerPoolMethod · 0.95
newReaderWriterMethod · 0.95
connectMethod · 0.95
asyncCBDispatcherMethod · 0.80
closeMethod · 0.45
pushMethod · 0.45

Tested by 15

ExampleConnectFunction · 0.76
TestUseDefaultTimeoutFunction · 0.76
TestUseCustomDialerFunction · 0.76
TestWSNoMixingSchemeFunction · 0.64
ExampleCustomDialerFunction · 0.64
ExampleConn_SubscribeFunction · 0.64