redis/internal/pool/conn.go

58 lines
1.0 KiB
Go
Raw Normal View History

package pool
import (
"net"
"time"
2016-07-02 15:52:10 +03:00
2016-10-09 13:49:28 +03:00
"gopkg.in/redis.v5/internal/proto"
)
const defaultBufSize = 4096
2016-03-12 13:41:02 +03:00
var noDeadline = time.Time{}
type Conn struct {
NetConn net.Conn
2016-07-02 15:52:10 +03:00
Rd *proto.Reader
Wb *proto.WriteBuffer
2016-03-15 15:04:35 +03:00
Inited bool
UsedAt time.Time
}
func NewConn(netConn net.Conn) *Conn {
cn := &Conn{
NetConn: netConn,
2016-07-02 15:52:10 +03:00
Wb: proto.NewWriteBuffer(),
UsedAt: time.Now(),
}
cn.Rd = proto.NewReader(cn.NetConn)
return cn
}
func (cn *Conn) IsStale(timeout time.Duration) bool {
return timeout > 0 && time.Since(cn.UsedAt) > timeout
2016-03-12 13:41:02 +03:00
}
func (cn *Conn) SetReadTimeout(timeout time.Duration) error {
cn.UsedAt = time.Now()
if timeout > 0 {
return cn.NetConn.SetReadDeadline(cn.UsedAt.Add(timeout))
}
return cn.NetConn.SetReadDeadline(noDeadline)
}
func (cn *Conn) SetWriteTimeout(timeout time.Duration) error {
cn.UsedAt = time.Now()
if timeout > 0 {
return cn.NetConn.SetWriteDeadline(cn.UsedAt.Add(timeout))
}
return cn.NetConn.SetWriteDeadline(noDeadline)
}
2016-03-15 15:04:35 +03:00
func (cn *Conn) Close() error {
return cn.NetConn.Close()
}