Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickTaibel committed Oct 24, 2023
1 parent 42e2142 commit 294c2e1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func (this *Client) FlushRedisAndRespond() (err error) {

if redisConn.DatabaseId != this.DatabaseId {
if err = redisConn.SelectDatabase(this.DatabaseId); err != nil {
log.Error("Select database failed: %s", err)
return
}
}
Expand Down
34 changes: 13 additions & 21 deletions connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func (c *Connection) ReconnectIfNecessary() (err error) {

c.connection, err = net.DialTimeout(c.protocol, c.endpoint, c.connectTimeout)
if err != nil {
log.Error("NewConnection: Error received from dial: %s", err)
c.connection = nil
return err
}
Expand All @@ -109,7 +108,7 @@ func (c *Connection) ReconnectIfNecessary() (err error) {
c.Reader = bufio.NewReader(netReadWriter)

if err = c.authenticate(); err != nil {
return err
return fmt.Errorf("authentication failed: %w", err)
}

c.nextReconnect = time.Now().Add(c.reconnectInterval)
Expand All @@ -121,42 +120,38 @@ func (c *Connection) ReconnectIfNecessary() (err error) {
// Selects the given database, for the connection
// If an error is returned, or if an invalid response is returned from the select, then this will return an error
// If not, the connections internal database will be updated accordingly
func (this *Connection) SelectDatabase(DatabaseId int) (err error) {
func (this *Connection) SelectDatabase(DatabaseId int) error {
if this.connection == nil {
log.Error("SelectDatabase: Selecting on invalid connection")
return errors.New("Selecting database on an invalid connection")
return errors.New("selecting database on an invalid connection")
}

err = protocol.WriteLine([]byte(fmt.Sprintf("select %d", DatabaseId)), this.Writer, true)
err := protocol.WriteLine([]byte(fmt.Sprintf("select %d", DatabaseId)), this.Writer, true)
if err != nil {
log.Error("SelectDatabase: Error received from protocol.FlushLine: %s", err)
return err
return fmt.Errorf("flush line failed: %w", err)
}

if line, isPrefix, err := this.Reader.ReadLine(); err != nil || isPrefix || !bytes.Equal(line, protocol.OK_RESPONSE) {
if err == nil {
err = errors.New("unknown ReadLine error")
}

log.Error("SelectDatabase: Error while attempting to select database. Err:%q Response:%q isPrefix:%t", err, line, isPrefix)
this.Disconnect()
return errors.New("Invalid select response")
return fmt.Errorf("invalid select database response: err:%q Response:%q isPrefix:%t", err, line, isPrefix)
}

this.DatabaseId = DatabaseId
return
return nil
}

// Tries to authenticate the connection
// If an error is returned, or if an invalid response is returned from the AUTH command, then this will return an error
func (this *Connection) authenticate() (err error) {
func (this *Connection) authenticate() error {
if this.connection == nil {
log.Error("authenticate: Using an invalid connection")
return errors.New("authenticating against an invalid connection")
}

if this.authPassword == "" {
return
return nil
}

authCommand := fmt.Sprintf("AUTH %s", this.authPassword)
Expand All @@ -165,10 +160,9 @@ func (this *Connection) authenticate() (err error) {
authCommand = fmt.Sprintf("AUTH %s %s", this.authUser, this.authPassword)
}

err = protocol.WriteLine([]byte(authCommand), this.Writer, true)
err := protocol.WriteLine([]byte(authCommand), this.Writer, true)
if err != nil {
log.Error("authenticate: Error received from protocol.FlushLine: %s", err)
return
return fmt.Errorf("flush line failed: %w", err)
}

line, isPrefix, err := this.Reader.ReadLine()
Expand All @@ -177,12 +171,10 @@ func (this *Connection) authenticate() (err error) {
err = errors.New("unknown ReadLine error")
}

log.Error("authenticate: Error while attempting to authenticate. Err:%q Response:%q isPrefix:%t",
err, line, isPrefix)
this.Disconnect()
return errors.New("invalid authentication response")
return fmt.Errorf("invalid authentication response: err:%q Response:%q isPrefix:%t", err, line, isPrefix)
}
return
return nil
}

// Checks if the current connection is up or not
Expand Down

0 comments on commit 294c2e1

Please sign in to comment.