Skip to content

Commit

Permalink
internal: return an error on setting deadline error
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Aug 16, 2019
1 parent 063afa6 commit 7f637a6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
26 changes: 16 additions & 10 deletions internal/pool/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,31 @@ func (cn *Conn) NextID() string {
}

func (cn *Conn) WithReader(
c context.Context, timeout time.Duration, fn func(rd *internal.BufReader) error,
ctx context.Context, timeout time.Duration, fn func(rd *internal.BufReader) error,
) error {
_ = cn.netConn.SetReadDeadline(cn.deadline(c, timeout))
err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout))
if err != nil {
return err
}
return fn(cn.rd)
}

func (cn *Conn) WithWriter(
c context.Context, timeout time.Duration, fn func(wb *WriteBuffer) error,
ctx context.Context, timeout time.Duration, fn func(wb *WriteBuffer) error,
) error {
_ = cn.netConn.SetWriteDeadline(cn.deadline(c, timeout))
firstErr := fn(cn.wb)
err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout))
if err != nil {
return err
}

buf := cn.wb.Flush()
_, err := cn.netConn.Write(buf)
if err != nil && firstErr == nil {
firstErr = err
cn.wb.Reset()
err = fn(cn.wb)
if err != nil {
return err
}

return firstErr
_, err = cn.netConn.Write(cn.wb.Bytes)
return err
}

func (cn *Conn) Close() error {
Expand Down
6 changes: 0 additions & 6 deletions internal/pool/write_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ func (buf *WriteBuffer) ResetBuffer(b []byte) {
buf.Bytes = b[:0]
}

func (buf *WriteBuffer) Flush() []byte {
b := buf.Bytes
buf.Reset()
return b
}

func (buf *WriteBuffer) StartMessage(c byte) {
if c == 0 {
buf.msgStart = len(buf.Bytes)
Expand Down
8 changes: 8 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ type badConn struct {

var _ net.Conn = &badConn{}

func (cn *badConn) SetReadDeadline(t time.Time) error {
return nil
}

func (cn *badConn) SetWriteDeadline(t time.Time) error {
return nil
}

func (cn *badConn) Read([]byte) (int, error) {
if cn.readDelay != 0 {
time.Sleep(cn.readDelay)
Expand Down

0 comments on commit 7f637a6

Please sign in to comment.