mirror of https://github.com/go-redis/redis.git
Merge pull request #1132 from go-redis/fix/deadline-error
internal: return an error on setting deadline
This commit is contained in:
commit
e471faf7f8
|
@ -58,23 +58,31 @@ func (cn *Conn) RemoteAddr() net.Addr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(rd *proto.Reader) error) error {
|
func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(rd *proto.Reader) error) error {
|
||||||
tm := cn.deadline(ctx, timeout)
|
err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout))
|
||||||
_ = cn.netConn.SetReadDeadline(tm)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return fn(cn.rd)
|
return fn(cn.rd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cn *Conn) WithWriter(
|
func (cn *Conn) WithWriter(
|
||||||
ctx context.Context, timeout time.Duration, fn func(wr *proto.Writer) error,
|
ctx context.Context, timeout time.Duration, fn func(wr *proto.Writer) error,
|
||||||
) error {
|
) error {
|
||||||
tm := cn.deadline(ctx, timeout)
|
err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout))
|
||||||
_ = cn.netConn.SetWriteDeadline(tm)
|
if err != nil {
|
||||||
|
return err
|
||||||
firstErr := fn(cn.wr)
|
|
||||||
err := cn.wr.Flush()
|
|
||||||
if err != nil && firstErr == nil {
|
|
||||||
firstErr = err
|
|
||||||
}
|
}
|
||||||
return firstErr
|
|
||||||
|
if cn.wr.Buffered() > 0 {
|
||||||
|
cn.wr.Reset(cn.netConn)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fn(cn.wr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return cn.wr.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cn *Conn) Close() error {
|
func (cn *Conn) Close() error {
|
||||||
|
|
|
@ -152,6 +152,10 @@ func (w *Writer) crlf() error {
|
||||||
return w.wr.WriteByte('\n')
|
return w.wr.WriteByte('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Writer) Buffered() int {
|
||||||
|
return w.wr.Buffered()
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Writer) Reset(wr io.Writer) {
|
func (w *Writer) Reset(wr io.Writer) {
|
||||||
w.wr.Reset(wr)
|
w.wr.Reset(wr)
|
||||||
}
|
}
|
||||||
|
|
|
@ -343,6 +343,14 @@ type badConn struct {
|
||||||
|
|
||||||
var _ net.Conn = &badConn{}
|
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) {
|
func (cn *badConn) Read([]byte) (int, error) {
|
||||||
if cn.readDelay != 0 {
|
if cn.readDelay != 0 {
|
||||||
time.Sleep(cn.readDelay)
|
time.Sleep(cn.readDelay)
|
||||||
|
|
Loading…
Reference in New Issue