Skip to content

Commit

Permalink
WithLogger connOption and Conn.logInfo bool (samuel#170)
Browse files Browse the repository at this point in the history
* add WithLogger connOption

This allows setting the logger as the connection is created, rather
than racing to call SetLogger while the connection's goroutine might
already be logging a successful (or failed) connection.

In addition using WithLogger works around the data race between SetLogger
and the connection's goroutine.

The data race is NOT fixed by this commit. AFAICS doing so is a nontrivial.
Every access to conn.logger would need to be atomic.

* add Conn.logInfo bool controling informational messages

By default it is enabled for backwards compatability, but
by setting logInfo=false you can have silence on stderr
when no errors are in fact occurring.

This is desireable for commandline tools, where the convention
I prefer is that silence indicates success.
  • Loading branch information
nsd20463 authored and samuel committed Nov 17, 2017
1 parent 9a96098 commit fb09205
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions zk/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ type Conn struct {
setWatchLimit int
setWatchCallback func([]*setWatchesRequest)

logger Logger
logger Logger
logInfo bool // true if information messages are logged; false if only errors are logged

buf []byte
}
Expand Down Expand Up @@ -200,6 +201,7 @@ func Connect(servers []string, sessionTimeout time.Duration, options ...connOpti
watchers: make(map[watchPathType][]chan Event),
passwd: emptyPassword,
logger: DefaultLogger,
logInfo: true, // default is true for backwards compatability
buf: make([]byte, bufferSize),
}

Expand Down Expand Up @@ -237,6 +239,21 @@ func WithHostProvider(hostProvider HostProvider) connOption {
}
}

// WithLogger returns a connection option specifying a non-default Logger
func WithLogger(logger Logger) connOption {
return func(c *Conn) {
c.logger = logger
}
}

// WithLogInfo returns a connection option specifying whether or not information messages
// shoud be logged.
func WithLogInfo(logInfo bool) connOption {
return func(c *Conn) {
c.logInfo = logInfo
}
}

// EventCallback is a function that is called when an Event occurs.
type EventCallback func(Event)

Expand Down Expand Up @@ -361,7 +378,9 @@ func (c *Conn) connect() error {
if err == nil {
c.conn = zkConn
c.setState(StateConnected)
c.logger.Printf("Connected to %s", c.Server())
if c.logInfo {
c.logger.Printf("Connected to %s", c.Server())
}
return nil
}

Expand All @@ -375,8 +394,10 @@ func (c *Conn) resendZkAuth(reauthReadyChan chan struct{}) {

defer close(reauthReadyChan)

c.logger.Printf("Re-submitting `%d` credentials after reconnect",
len(c.creds))
if c.logInfo {
c.logger.Printf("Re-submitting `%d` credentials after reconnect",
len(c.creds))
}

for _, cred := range c.creds {
resChan, err := c.sendRequest(
Expand Down Expand Up @@ -444,7 +465,9 @@ func (c *Conn) loop() {
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)
if c.logInfo {
c.logger.Printf("Authenticated: id=%d, timeout=%d", c.SessionID(), c.sessionTimeoutMs)
}
c.hostProvider.Connected() // mark success
c.closeChan = make(chan struct{}) // channel to tell send loop stop
reauthChan := make(chan struct{}) // channel to tell send loop that authdata has been resubmitted
Expand All @@ -454,15 +477,19 @@ func (c *Conn) loop() {
go func() {
<-reauthChan
err := c.sendLoop()
c.logger.Printf("Send loop terminated: err=%v", err)
if err != nil || c.logInfo {
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)
c.logger.Printf("Recv loop terminated: err=%v", err)
if err != io.EOF || c.logInfo {
c.logger.Printf("Recv loop terminated: err=%v", err)
}
if err == nil {
panic("zk: recvLoop should never return nil error")
}
Expand Down

0 comments on commit fb09205

Please sign in to comment.