ripple-api/vendor/gopkg.in/redis.v5/internal/pool/conn.go

58 lines
1.0 KiB
Go
Raw Normal View History

2017-01-14 17:42:10 +00:00
package pool
import (
"net"
"time"
"gopkg.in/redis.v5/internal/proto"
)
const defaultBufSize = 4096
2017-01-14 17:42:10 +00:00
var noDeadline = time.Time{}
type Conn struct {
NetConn net.Conn
Rd *proto.Reader
Wb *proto.WriteBuffer
2017-01-14 17:42:10 +00:00
Inited bool
UsedAt time.Time
2017-01-14 17:42:10 +00:00
}
func NewConn(netConn net.Conn) *Conn {
cn := &Conn{
NetConn: netConn,
2017-01-14 17:42:10 +00:00
Wb: proto.NewWriteBuffer(),
UsedAt: time.Now(),
2017-01-14 17:42:10 +00:00
}
cn.Rd = proto.NewReader(cn.NetConn)
2017-01-14 17:42:10 +00:00
return cn
}
func (cn *Conn) IsStale(timeout time.Duration) bool {
return timeout > 0 && time.Since(cn.UsedAt) > timeout
2017-01-14 17:42:10 +00:00
}
func (cn *Conn) SetReadTimeout(timeout time.Duration) error {
cn.UsedAt = time.Now()
2017-01-14 17:42:10 +00:00
if timeout > 0 {
return cn.NetConn.SetReadDeadline(cn.UsedAt.Add(timeout))
2017-01-14 17:42:10 +00:00
}
return cn.NetConn.SetReadDeadline(noDeadline)
2017-01-14 17:42:10 +00:00
}
func (cn *Conn) SetWriteTimeout(timeout time.Duration) error {
cn.UsedAt = time.Now()
2017-01-14 17:42:10 +00:00
if timeout > 0 {
return cn.NetConn.SetWriteDeadline(cn.UsedAt.Add(timeout))
2017-01-14 17:42:10 +00:00
}
return cn.NetConn.SetWriteDeadline(noDeadline)
2017-01-14 17:42:10 +00:00
}
func (cn *Conn) Close() error {
return cn.NetConn.Close()
2017-01-14 17:42:10 +00:00
}