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

Method setupServerPool

nats.go:1990–2038  ·  nats.go::Conn.setupServerPool

Create the server pool using the options given. We will place a Url option first, followed by any Server Options. We will randomize the server pool unless the NoRandomize flag is set.

()

Source from the content-addressed store, hash-verified

1988// Server Options. We will randomize the server pool unless
1989// the NoRandomize flag is set.
1990func (nc *Conn) setupServerPool() error {
1991 nc.srvPool = make([]*Server, 0, srvPoolSize)
1992 nc.urls = make(map[string]struct{}, srvPoolSize)
1993
1994 // Create srv objects from each url string in nc.Opts.Servers
1995 // and add them to the pool.
1996 for _, urlString := range nc.Opts.Servers {
1997 if err := nc.addURLToPool(urlString, false, false); err != nil {
1998 return err
1999 }
2000 }
2001
2002 // Randomize if allowed to
2003 if !nc.Opts.NoRandomize {
2004 nc.shufflePool(0)
2005 }
2006
2007 // Normally, if this one is set, Options.Servers should not be,
2008 // but we always allowed that, so continue to do so.
2009 if nc.Opts.Url != _EMPTY_ {
2010 // Add to the end of the array
2011 if err := nc.addURLToPool(nc.Opts.Url, false, false); err != nil {
2012 return err
2013 }
2014 // Then swap it with first to guarantee that Options.Url is tried first.
2015 last := len(nc.srvPool) - 1
2016 if last > 0 {
2017 nc.srvPool[0], nc.srvPool[last] = nc.srvPool[last], nc.srvPool[0]
2018 }
2019 } else if len(nc.srvPool) <= 0 {
2020 // Place default URL if pool is empty.
2021 if err := nc.addURLToPool(DefaultURL, false, false); err != nil {
2022 return err
2023 }
2024 }
2025
2026 // Check for Scheme hint to move to TLS mode.
2027 for _, srv := range nc.srvPool {
2028 if srv.URL.Scheme == tlsScheme || srv.URL.Scheme == wsSchemeTLS {
2029 // FIXME(dlc), this is for all in the pool, should be case by case.
2030 nc.Opts.Secure = true
2031 if nc.Opts.TLSConfig == nil {
2032 nc.Opts.TLSConfig = &tls.Config{MinVersion: tls.VersionTLS12}
2033 }
2034 }
2035 }
2036
2037 return nc.pickServer()
2038}
2039
2040// Helper function to return scheme
2041func (nc *Conn) connScheme() string {

Callers 9

TestWSTlsNoConfigFunction · 0.95
TestSimplifiedURLsFunction · 0.95
TestServersRandomizeFunction · 0.95
TestSelectNextServerFunction · 0.95
TestAsyncINFOFunction · 0.95
TestConnServersFunction · 0.95
TestNoEchoOldServerFunction · 0.95
ConnectMethod · 0.95

Calls 3

addURLToPoolMethod · 0.95
shufflePoolMethod · 0.95
pickServerMethod · 0.95

Tested by 8

TestWSTlsNoConfigFunction · 0.76
TestSimplifiedURLsFunction · 0.76
TestServersRandomizeFunction · 0.76
TestSelectNextServerFunction · 0.76
TestAsyncINFOFunction · 0.76
TestConnServersFunction · 0.76
TestNoEchoOldServerFunction · 0.76