Skip to content

Commit

Permalink
api: meaningful read/write networt error
Browse files Browse the repository at this point in the history
We need to add meaningful error descriptions for a read/write socket
errors.

Part of #129
  • Loading branch information
oleg-jukovec committed Jun 27, 2023
1 parent 782c93b commit c5de6b0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
- Type() method to the Request interface (#158)
- Enumeration types for RLimitAction/iterators (#158)
- IsNullable flag for Field (#302)
- Meaningful errors for error descriptions for a read/write socket
errors (#129)

### Changed

Expand Down
20 changes: 20 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,10 @@ func (conn *Connection) writer(w writeFlusher, c Conn) {
runtime.Gosched()
if len(conn.dirtyShard) == 0 {
if err := w.Flush(); err != nil {
err = ClientError{
ErrIoError,
fmt.Sprintf("failed to flush data to the connection: %s", err),
}
conn.reconnect(err, c)
return
}
Expand All @@ -812,6 +816,10 @@ func (conn *Connection) writer(w writeFlusher, c Conn) {
continue
}
if _, err := w.Write(packet.b); err != nil {
err = ClientError{
ErrIoError,
fmt.Sprintf("failed to write data to the connection: %s", err),
}
conn.reconnect(err, c)
return
}
Expand Down Expand Up @@ -868,12 +876,20 @@ func (conn *Connection) reader(r io.Reader, c Conn) {
for atomic.LoadUint32(&conn.state) != connClosed {
respBytes, err := read(r, conn.lenbuf[:])
if err != nil {
err = ClientError{
ErrIoError,
fmt.Sprintf("failed to read data from the connection: %s", err),
}
conn.reconnect(err, c)
return
}
resp := &Response{buf: smallBuf{b: respBytes}}
err = resp.decodeHeader(conn.dec)
if err != nil {
err = ClientError{
ErrProtocolError,
fmt.Sprintf("failed to decode IPROTO header: %s", err),
}
conn.reconnect(err, c)
return
}
Expand All @@ -883,6 +899,10 @@ func (conn *Connection) reader(r io.Reader, c Conn) {
if event, err := readWatchEvent(&resp.buf); err == nil {
events <- event
} else {
err = ClientError{
ErrProtocolError,
fmt.Sprintf("failed to decode IPROTO_EVENT: %s", err),
}
conn.opts.Logger.Report(LogWatchEventReadFailed, conn, err)
}
continue
Expand Down
3 changes: 2 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (clierr ClientError) Error() string {
// - request is aborted due to rate limit
func (clierr ClientError) Temporary() bool {
switch clierr.Code {
case ErrConnectionNotReady, ErrTimeouted, ErrRateLimited:
case ErrConnectionNotReady, ErrTimeouted, ErrRateLimited, ErrIoError:
return true
default:
return false
Expand All @@ -60,4 +60,5 @@ const (
ErrTimeouted = 0x4000 + iota
ErrRateLimited = 0x4000 + iota
ErrConnectionShutdown = 0x4000 + iota
ErrIoError = 0x4000 + iota
)

0 comments on commit c5de6b0

Please sign in to comment.