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
|
|
|
|
|
|
|
|
Rd *proto.Reader
|
2018-08-06 13:59:15 +03:00
|
|
|
wb *proto.WriteBuffer
|
|
|
|
|
|
|
|
concurrentReadWrite bool
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
InitedAt time.Time
|
|
|
|
pooled bool
|
|
|
|
usedAt atomic.Value
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConn(netConn net.Conn) *Conn {
|
|
|
|
cn := &Conn{
|
2017-02-08 12:24:09 +03:00
|
|
|
netConn: netConn,
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2018-08-06 11:54:47 +03:00
|
|
|
buf := proto.NewElasticBufReader(netConn)
|
2018-08-04 12:19:19 +03:00
|
|
|
cn.Rd = proto.NewReader(buf)
|
2018-08-06 13:59:15 +03:00
|
|
|
cn.wb = proto.NewWriteBuffer()
|
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 {
|
|
|
|
return cn.usedAt.Load().(time.Time)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) SetUsedAt(tm time.Time) {
|
|
|
|
cn.usedAt.Store(tm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) SetNetConn(netConn net.Conn) {
|
|
|
|
cn.netConn = netConn
|
|
|
|
cn.Rd.Reset(netConn)
|
|
|
|
}
|
|
|
|
|
2018-07-22 09:27:36 +03:00
|
|
|
func (cn *Conn) SetReadTimeout(timeout time.Duration) {
|
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-07-22 09:27:36 +03:00
|
|
|
cn.netConn.SetReadDeadline(now.Add(timeout))
|
|
|
|
} else {
|
|
|
|
cn.netConn.SetReadDeadline(noDeadline)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 09:27:36 +03:00
|
|
|
func (cn *Conn) SetWriteTimeout(timeout time.Duration) {
|
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-07-22 09:27:36 +03:00
|
|
|
cn.netConn.SetWriteDeadline(now.Add(timeout))
|
|
|
|
} else {
|
|
|
|
cn.netConn.SetWriteDeadline(noDeadline)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
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-06 13:59:15 +03:00
|
|
|
func (cn *Conn) EnableConcurrentReadWrite() {
|
|
|
|
cn.concurrentReadWrite = true
|
|
|
|
cn.wb.ResetBuffer(make([]byte, 4096))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) PrepareWriteBuffer() *proto.WriteBuffer {
|
|
|
|
if !cn.concurrentReadWrite {
|
|
|
|
cn.wb.ResetBuffer(cn.Rd.Buffer())
|
|
|
|
}
|
|
|
|
cn.wb.Reset()
|
|
|
|
return cn.wb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) FlushWriteBuffer(wb *proto.WriteBuffer) error {
|
|
|
|
_, err := cn.netConn.Write(wb.Bytes())
|
|
|
|
if !cn.concurrentReadWrite {
|
|
|
|
cn.Rd.ResetBuffer(wb.Buffer())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|