2016-03-12 11:52:13 +03:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2017-02-08 12:24:09 +03:00
|
|
|
"sync/atomic"
|
2016-03-12 11:52:13 +03:00
|
|
|
"time"
|
2016-07-02 15:52:10 +03:00
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis/internal/proto"
|
2016-03-12 11:52:13 +03:00
|
|
|
)
|
|
|
|
|
2016-03-12 13:41:02 +03:00
|
|
|
var noDeadline = time.Time{}
|
2016-03-12 11:52:13 +03:00
|
|
|
|
|
|
|
type Conn struct {
|
2017-02-08 12:24:09 +03:00
|
|
|
netConn net.Conn
|
|
|
|
|
2019-05-31 17:54:30 +03:00
|
|
|
rd *proto.Reader
|
|
|
|
wr *proto.Writer
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2019-03-25 14:02:31 +03:00
|
|
|
Inited bool
|
|
|
|
pooled bool
|
|
|
|
createdAt time.Time
|
2019-05-31 17:54:30 +03:00
|
|
|
usedAt int64 // atomic
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConn(netConn net.Conn) *Conn {
|
|
|
|
cn := &Conn{
|
2019-03-25 14:02:31 +03:00
|
|
|
netConn: netConn,
|
|
|
|
createdAt: time.Now(),
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2018-08-17 13:56:37 +03:00
|
|
|
cn.rd = proto.NewReader(netConn)
|
|
|
|
cn.wr = proto.NewWriter(netConn)
|
2017-02-08 12:24:09 +03:00
|
|
|
cn.SetUsedAt(time.Now())
|
2016-03-12 11:52:13 +03:00
|
|
|
return cn
|
|
|
|
}
|
|
|
|
|
2017-02-08 12:24:09 +03:00
|
|
|
func (cn *Conn) UsedAt() time.Time {
|
2019-05-31 17:54:30 +03:00
|
|
|
unix := atomic.LoadInt64(&cn.usedAt)
|
|
|
|
return time.Unix(unix, 0)
|
2017-02-08 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) SetUsedAt(tm time.Time) {
|
2019-05-31 17:54:30 +03:00
|
|
|
atomic.StoreInt64(&cn.usedAt, tm.Unix())
|
2017-02-08 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) SetNetConn(netConn net.Conn) {
|
|
|
|
cn.netConn = netConn
|
2018-08-15 11:53:15 +03:00
|
|
|
cn.rd.Reset(netConn)
|
2018-08-17 13:56:37 +03:00
|
|
|
cn.wr.Reset(netConn)
|
2017-02-08 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-15 11:53:15 +03:00
|
|
|
func (cn *Conn) setReadTimeout(timeout time.Duration) error {
|
2017-02-08 12:24:09 +03:00
|
|
|
now := time.Now()
|
|
|
|
cn.SetUsedAt(now)
|
2016-12-03 18:30:13 +03:00
|
|
|
if timeout > 0 {
|
2018-08-15 11:53:15 +03:00
|
|
|
return cn.netConn.SetReadDeadline(now.Add(timeout))
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2018-08-15 11:53:15 +03:00
|
|
|
return cn.netConn.SetReadDeadline(noDeadline)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2018-08-15 11:53:15 +03:00
|
|
|
func (cn *Conn) setWriteTimeout(timeout time.Duration) error {
|
2017-02-08 12:24:09 +03:00
|
|
|
now := time.Now()
|
|
|
|
cn.SetUsedAt(now)
|
2016-12-03 18:30:13 +03:00
|
|
|
if timeout > 0 {
|
2018-08-15 11:53:15 +03:00
|
|
|
return cn.netConn.SetWriteDeadline(now.Add(timeout))
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2018-08-15 11:53:15 +03:00
|
|
|
return cn.netConn.SetWriteDeadline(noDeadline)
|
2017-02-08 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) Write(b []byte) (int, error) {
|
|
|
|
return cn.netConn.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) RemoteAddr() net.Addr {
|
|
|
|
return cn.netConn.RemoteAddr()
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2018-08-17 13:56:37 +03:00
|
|
|
func (cn *Conn) WithReader(timeout time.Duration, fn func(rd *proto.Reader) error) error {
|
2018-08-15 11:53:15 +03:00
|
|
|
_ = cn.setReadTimeout(timeout)
|
2018-08-17 13:56:37 +03:00
|
|
|
return fn(cn.rd)
|
2018-08-06 13:59:15 +03:00
|
|
|
}
|
|
|
|
|
2018-08-17 13:56:37 +03:00
|
|
|
func (cn *Conn) WithWriter(timeout time.Duration, fn func(wr *proto.Writer) error) error {
|
2018-08-15 11:53:15 +03:00
|
|
|
_ = cn.setWriteTimeout(timeout)
|
|
|
|
|
2018-08-17 13:56:37 +03:00
|
|
|
firstErr := fn(cn.wr)
|
|
|
|
err := cn.wr.Flush()
|
2018-08-15 11:53:15 +03:00
|
|
|
if err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
return firstErr
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:04:35 +03:00
|
|
|
func (cn *Conn) Close() error {
|
2017-02-08 12:24:09 +03:00
|
|
|
return cn.netConn.Close()
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|