mirror of
https://github.com/redis/go-redis.git
synced 2025-07-29 17:41:15 +03:00
Set read/write timeouts more consistently.
This commit is contained in:
@ -18,9 +18,6 @@ type Conn struct {
|
||||
|
||||
Inited bool
|
||||
UsedAt time.Time
|
||||
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
}
|
||||
|
||||
func NewConn(netConn net.Conn) *Conn {
|
||||
@ -30,7 +27,7 @@ func NewConn(netConn net.Conn) *Conn {
|
||||
|
||||
UsedAt: time.Now(),
|
||||
}
|
||||
cn.Rd = proto.NewReader(cn)
|
||||
cn.Rd = proto.NewReader(cn.NetConn)
|
||||
return cn
|
||||
}
|
||||
|
||||
@ -38,28 +35,21 @@ func (cn *Conn) IsStale(timeout time.Duration) bool {
|
||||
return timeout > 0 && time.Since(cn.UsedAt) > timeout
|
||||
}
|
||||
|
||||
func (cn *Conn) Read(b []byte) (int, error) {
|
||||
func (cn *Conn) SetReadTimeout(timeout time.Duration) error {
|
||||
cn.UsedAt = time.Now()
|
||||
if cn.ReadTimeout != 0 {
|
||||
cn.NetConn.SetReadDeadline(cn.UsedAt.Add(cn.ReadTimeout))
|
||||
} else {
|
||||
cn.NetConn.SetReadDeadline(noDeadline)
|
||||
if timeout > 0 {
|
||||
return cn.NetConn.SetReadDeadline(cn.UsedAt.Add(timeout))
|
||||
}
|
||||
return cn.NetConn.Read(b)
|
||||
return cn.NetConn.SetReadDeadline(noDeadline)
|
||||
|
||||
}
|
||||
|
||||
func (cn *Conn) Write(b []byte) (int, error) {
|
||||
func (cn *Conn) SetWriteTimeout(timeout time.Duration) error {
|
||||
cn.UsedAt = time.Now()
|
||||
if cn.WriteTimeout != 0 {
|
||||
cn.NetConn.SetWriteDeadline(cn.UsedAt.Add(cn.WriteTimeout))
|
||||
} else {
|
||||
cn.NetConn.SetWriteDeadline(noDeadline)
|
||||
if timeout > 0 {
|
||||
return cn.NetConn.SetWriteDeadline(cn.UsedAt.Add(timeout))
|
||||
}
|
||||
return cn.NetConn.Write(b)
|
||||
}
|
||||
|
||||
func (cn *Conn) RemoteAddr() net.Addr {
|
||||
return cn.NetConn.RemoteAddr()
|
||||
return cn.NetConn.SetWriteDeadline(noDeadline)
|
||||
}
|
||||
|
||||
func (cn *Conn) Close() error {
|
||||
|
@ -266,19 +266,19 @@ func (p *ConnPool) Closed() bool {
|
||||
return atomic.LoadInt32(&p._closed) == 1
|
||||
}
|
||||
|
||||
func (p *ConnPool) Close() (retErr error) {
|
||||
func (p *ConnPool) Close() error {
|
||||
if !atomic.CompareAndSwapInt32(&p._closed, 0, 1) {
|
||||
return ErrClosed
|
||||
}
|
||||
|
||||
p.connsMu.Lock()
|
||||
// Close all connections.
|
||||
var firstErr error
|
||||
for _, cn := range p.conns {
|
||||
if cn == nil {
|
||||
continue
|
||||
}
|
||||
if err := p.closeConn(cn, ErrClosed); err != nil && retErr == nil {
|
||||
retErr = err
|
||||
if err := p.closeConn(cn, ErrClosed); err != nil && firstErr == nil {
|
||||
firstErr = err
|
||||
}
|
||||
}
|
||||
p.conns = nil
|
||||
@ -288,7 +288,7 @@ func (p *ConnPool) Close() (retErr error) {
|
||||
p.freeConns = nil
|
||||
p.freeConnsMu.Unlock()
|
||||
|
||||
return retErr
|
||||
return firstErr
|
||||
}
|
||||
|
||||
func (p *ConnPool) closeConn(cn *Conn, reason error) error {
|
||||
|
@ -49,7 +49,7 @@ func (p *StickyConnPool) Get() (*Conn, bool, error) {
|
||||
return cn, true, nil
|
||||
}
|
||||
|
||||
func (p *StickyConnPool) put() (err error) {
|
||||
func (p *StickyConnPool) putUpstream() (err error) {
|
||||
err = p.pool.Put(p.cn)
|
||||
p.cn = nil
|
||||
return err
|
||||
@ -67,7 +67,7 @@ func (p *StickyConnPool) Put(cn *Conn) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *StickyConnPool) remove(reason error) error {
|
||||
func (p *StickyConnPool) removeUpstream(reason error) error {
|
||||
err := p.pool.Remove(p.cn, reason)
|
||||
p.cn = nil
|
||||
return err
|
||||
@ -85,7 +85,7 @@ func (p *StickyConnPool) Remove(cn *Conn, reason error) error {
|
||||
if cn != nil && p.cn != cn {
|
||||
panic("p.cn != cn")
|
||||
}
|
||||
return p.remove(reason)
|
||||
return p.removeUpstream(reason)
|
||||
}
|
||||
|
||||
func (p *StickyConnPool) Len() int {
|
||||
@ -120,10 +120,10 @@ func (p *StickyConnPool) Close() error {
|
||||
var err error
|
||||
if p.cn != nil {
|
||||
if p.reusable {
|
||||
err = p.put()
|
||||
err = p.putUpstream()
|
||||
} else {
|
||||
reason := errors.New("redis: sticky not reusable connection")
|
||||
err = p.remove(reason)
|
||||
reason := errors.New("redis: unreusable sticky connection")
|
||||
err = p.removeUpstream(reason)
|
||||
}
|
||||
}
|
||||
return err
|
||||
|
Reference in New Issue
Block a user