2020-09-27 21:09:46 +00:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
2021-02-10 21:28:32 +00:00
|
|
|
"bufio"
|
2020-09-27 21:09:46 +00:00
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2021-02-10 21:28:32 +00:00
|
|
|
"github.com/go-redis/redis/v8/internal/proto"
|
2020-09-27 21:09:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var noDeadline = time.Time{}
|
|
|
|
|
|
|
|
type Conn struct {
|
2021-02-10 21:28:32 +00:00
|
|
|
usedAt int64 // atomic
|
2020-09-27 21:09:46 +00:00
|
|
|
netConn net.Conn
|
|
|
|
|
|
|
|
rd *proto.Reader
|
2021-02-10 21:28:32 +00:00
|
|
|
bw *bufio.Writer
|
2020-09-27 21:09:46 +00:00
|
|
|
wr *proto.Writer
|
|
|
|
|
|
|
|
Inited bool
|
|
|
|
pooled bool
|
|
|
|
createdAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConn(netConn net.Conn) *Conn {
|
|
|
|
cn := &Conn{
|
|
|
|
netConn: netConn,
|
|
|
|
createdAt: time.Now(),
|
|
|
|
}
|
|
|
|
cn.rd = proto.NewReader(netConn)
|
2021-02-10 21:28:32 +00:00
|
|
|
cn.bw = bufio.NewWriter(netConn)
|
|
|
|
cn.wr = proto.NewWriter(cn.bw)
|
2020-09-27 21:09:46 +00:00
|
|
|
cn.SetUsedAt(time.Now())
|
|
|
|
return cn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) UsedAt() time.Time {
|
|
|
|
unix := atomic.LoadInt64(&cn.usedAt)
|
|
|
|
return time.Unix(unix, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) SetUsedAt(tm time.Time) {
|
|
|
|
atomic.StoreInt64(&cn.usedAt, tm.Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) SetNetConn(netConn net.Conn) {
|
|
|
|
cn.netConn = netConn
|
|
|
|
cn.rd.Reset(netConn)
|
2021-02-10 21:28:32 +00:00
|
|
|
cn.bw.Reset(netConn)
|
2020-09-27 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) Write(b []byte) (int, error) {
|
|
|
|
return cn.netConn.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) RemoteAddr() net.Addr {
|
2021-02-10 21:28:32 +00:00
|
|
|
if cn.netConn != nil {
|
|
|
|
return cn.netConn.RemoteAddr()
|
|
|
|
}
|
|
|
|
return nil
|
2020-09-27 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(rd *proto.Reader) error) error {
|
2021-04-23 00:08:53 +00:00
|
|
|
if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil {
|
2021-06-10 14:44:25 +00:00
|
|
|
return err
|
2021-04-23 00:08:53 +00:00
|
|
|
}
|
2021-06-10 14:44:25 +00:00
|
|
|
return fn(cn.rd)
|
2020-09-27 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) WithWriter(
|
|
|
|
ctx context.Context, timeout time.Duration, fn func(wr *proto.Writer) error,
|
|
|
|
) error {
|
2021-04-23 00:08:53 +00:00
|
|
|
if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil {
|
2021-06-10 14:44:25 +00:00
|
|
|
return err
|
2021-04-23 00:08:53 +00:00
|
|
|
}
|
2020-09-27 21:09:46 +00:00
|
|
|
|
2021-04-23 00:08:53 +00:00
|
|
|
if cn.bw.Buffered() > 0 {
|
|
|
|
cn.bw.Reset(cn.netConn)
|
|
|
|
}
|
2021-02-10 21:28:32 +00:00
|
|
|
|
2021-04-23 00:08:53 +00:00
|
|
|
if err := fn(cn.wr); err != nil {
|
2021-06-10 14:44:25 +00:00
|
|
|
return err
|
2021-04-23 00:08:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 02:06:10 +00:00
|
|
|
return cn.bw.Flush()
|
2020-09-27 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) Close() error {
|
|
|
|
return cn.netConn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) deadline(ctx context.Context, timeout time.Duration) time.Time {
|
|
|
|
tm := time.Now()
|
|
|
|
cn.SetUsedAt(tm)
|
|
|
|
|
|
|
|
if timeout > 0 {
|
|
|
|
tm = tm.Add(timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx != nil {
|
|
|
|
deadline, ok := ctx.Deadline()
|
|
|
|
if ok {
|
|
|
|
if timeout == 0 {
|
|
|
|
return deadline
|
|
|
|
}
|
|
|
|
if deadline.Before(tm) {
|
|
|
|
return deadline
|
|
|
|
}
|
|
|
|
return tm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeout > 0 {
|
|
|
|
return tm
|
|
|
|
}
|
|
|
|
|
|
|
|
return noDeadline
|
|
|
|
}
|