mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-17 03:24:40 -07:00
Cleaner way of handling the initial sends
This commit is contained in:
@@ -86,10 +86,8 @@ func clientConnStr(conn net.Conn) interface{} {
|
||||
return "N/A"
|
||||
}
|
||||
|
||||
// Lock should be held
|
||||
func (c *client) initClient() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
s := c.srv
|
||||
c.cid = atomic.AddUint64(&s.gcid, 1)
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ type connectInfo struct {
|
||||
|
||||
const conProto = "CONNECT %s" + _CRLF_
|
||||
|
||||
// Lock should be held entering here.
|
||||
func (c *client) sendConnect() {
|
||||
var user, pass string
|
||||
if userInfo := c.route.url.User; userInfo != nil {
|
||||
@@ -81,13 +82,14 @@ func (s *Server) createRoute(conn net.Conn, rUrl *url.URL) *client {
|
||||
r := &route{didSolicit: didSolicit}
|
||||
c := &client{srv: s, nc: conn, opts: defaultOpts, typ: ROUTER, route: r}
|
||||
|
||||
// Grab lock
|
||||
c.mu.Lock()
|
||||
|
||||
// Initialize
|
||||
c.initClient()
|
||||
|
||||
Debug("Route connection created", clientConnStr(c.nc), c.cid)
|
||||
|
||||
c.mu.Lock()
|
||||
|
||||
// Queue Connect proto if we solicited the connection.
|
||||
if didSolicit {
|
||||
r.url = rUrl
|
||||
@@ -103,6 +105,8 @@ func (s *Server) createRoute(conn net.Conn, rUrl *url.URL) *client {
|
||||
ttl := secondsToDuration(s.opts.ClusterAuthTimeout)
|
||||
c.setAuthTimer(ttl)
|
||||
}
|
||||
|
||||
// Unlock to register.
|
||||
c.mu.Unlock()
|
||||
|
||||
// Register with the server.
|
||||
|
||||
@@ -304,20 +304,14 @@ func (s *Server) StartHTTPMonitoring() {
|
||||
func (s *Server) createClient(conn net.Conn) *client {
|
||||
c := &client{srv: s, nc: conn, opts: defaultOpts}
|
||||
|
||||
// Grab lock
|
||||
c.mu.Lock()
|
||||
|
||||
// Initialize
|
||||
c.initClient()
|
||||
|
||||
Debug("Client connection created", clientConnStr(c.nc), c.cid)
|
||||
|
||||
c.mu.Lock()
|
||||
|
||||
// After reaquiring the lock, check to see if we have been
|
||||
// closed already via a bad read in the readLoop()
|
||||
if c.nc == nil {
|
||||
c.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Send our information.
|
||||
s.sendInfo(c)
|
||||
|
||||
@@ -327,6 +321,7 @@ func (s *Server) createClient(conn net.Conn) *client {
|
||||
c.setAuthTimer(ttl)
|
||||
}
|
||||
|
||||
// Unlock to register
|
||||
c.mu.Unlock()
|
||||
|
||||
// Register with the server.
|
||||
@@ -339,9 +334,6 @@ func (s *Server) createClient(conn net.Conn) *client {
|
||||
|
||||
// Assume the lock is held upon entry.
|
||||
func (s *Server) sendInfo(c *client) {
|
||||
if c.nc == nil {
|
||||
return
|
||||
}
|
||||
switch c.typ {
|
||||
case CLIENT:
|
||||
c.nc.Write(s.infoJson)
|
||||
|
||||
Reference in New Issue
Block a user