Skip to content

Commit

Permalink
Better logging of connection state transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
horkhe committed Sep 29, 2015
1 parent b65054c commit 167bc0f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions zk/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func (c *Conn) connect() error {
if err == nil {
c.conn = zkConn
c.setState(StateConnected)
c.logger.Printf("Connected to %s", c.servers[c.serverIndex])
return nil
}

Expand All @@ -263,24 +264,29 @@ func (c *Conn) loop() {
err := c.authenticate()
switch {
case err == ErrSessionExpired:
c.logger.Printf("Authentication failed: %s", err)
c.invalidateWatches(err)
case err != nil && c.conn != nil:
c.logger.Printf("Authentication failed: %s", err)
c.conn.Close()
case err == nil:
c.logger.Printf("Authenticated: id=%d, timeout=%d", c.sessionID, c.sessionTimeoutMs)
c.lastServerIndex = c.serverIndex
closeChan := make(chan struct{}) // channel to tell send loop stop
var wg sync.WaitGroup

wg.Add(1)
go func() {
c.sendLoop(c.conn, closeChan)
err := c.sendLoop(c.conn, closeChan)
c.logger.Printf("Send loop terminated: err=%v", err)
c.conn.Close() // causes recv loop to EOF/exit
wg.Done()
}()

wg.Add(1)
go func() {
err = c.recvLoop(c.conn)
err := c.recvLoop(c.conn)
c.logger.Printf("Recv loop terminated: err=%v", err)
if err == nil {
panic("zk: recvLoop should never return nil error")
}
Expand All @@ -293,11 +299,6 @@ func (c *Conn) loop() {

c.setState(StateDisconnected)

// Yeesh
if err != io.EOF && err != ErrSessionExpired && !strings.Contains(err.Error(), "use of closed network connection") {
c.logger.Printf(err.Error())
}

select {
case <-c.shouldQuit:
c.flushRequests(ErrClosing)
Expand Down

0 comments on commit 167bc0f

Please sign in to comment.