mirror of
https://github.com/redis/go-redis.git
synced 2025-07-28 06:42:00 +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 {
|
||||
|
Reference in New Issue
Block a user