2016-03-12 11:52:13 +03:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
2020-06-09 16:29:53 +03:00
|
|
|
"bufio"
|
2019-06-08 15:02:51 +03:00
|
|
|
"context"
|
2016-03-12 11:52:13 +03:00
|
|
|
"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
|
|
|
|
2020-03-11 17:29:16 +03:00
|
|
|
"github.com/go-redis/redis/v8/internal"
|
|
|
|
"github.com/go-redis/redis/v8/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 {
|
2020-06-09 16:29:53 +03:00
|
|
|
usedAt int64 // atomic
|
2017-02-08 12:24:09 +03:00
|
|
|
netConn net.Conn
|
|
|
|
|
2019-05-31 17:54:30 +03:00
|
|
|
rd *proto.Reader
|
2020-06-09 16:29:53 +03:00
|
|
|
bw *bufio.Writer
|
2019-05-31 17:54:30 +03:00
|
|
|
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
|
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)
|
2020-06-09 16:29:53 +03:00
|
|
|
cn.bw = bufio.NewWriter(netConn)
|
|
|
|
cn.wr = proto.NewWriter(cn.bw)
|
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)
|
2020-06-09 16:29:53 +03:00
|
|
|
cn.bw.Reset(netConn)
|
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
|
|
|
}
|
|
|
|
|
2019-06-08 15:02:51 +03:00
|
|
|
func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(rd *proto.Reader) error) error {
|
2020-03-11 17:26:42 +03:00
|
|
|
return internal.WithSpan(ctx, "with_reader", func(ctx context.Context) error {
|
2020-07-09 10:39:46 +03:00
|
|
|
if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil {
|
|
|
|
return internal.RecordError(ctx, err)
|
2020-03-11 17:26:42 +03:00
|
|
|
}
|
2020-07-09 10:39:46 +03:00
|
|
|
if err := fn(cn.rd); err != nil {
|
|
|
|
return internal.RecordError(ctx, err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-11 17:26:42 +03:00
|
|
|
})
|
2018-08-06 13:59:15 +03:00
|
|
|
}
|
|
|
|
|
2019-06-08 15:02:51 +03:00
|
|
|
func (cn *Conn) WithWriter(
|
|
|
|
ctx context.Context, timeout time.Duration, fn func(wr *proto.Writer) error,
|
|
|
|
) error {
|
2020-03-11 17:26:42 +03:00
|
|
|
return internal.WithSpan(ctx, "with_writer", func(ctx context.Context) error {
|
2020-07-09 10:39:46 +03:00
|
|
|
if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil {
|
|
|
|
return internal.RecordError(ctx, err)
|
2020-03-11 17:26:42 +03:00
|
|
|
}
|
2018-08-15 11:53:15 +03:00
|
|
|
|
2020-06-09 16:29:53 +03:00
|
|
|
if cn.bw.Buffered() > 0 {
|
|
|
|
cn.bw.Reset(cn.netConn)
|
2020-03-11 17:26:42 +03:00
|
|
|
}
|
2019-08-16 17:45:45 +03:00
|
|
|
|
2020-07-09 10:39:46 +03:00
|
|
|
if err := fn(cn.wr); err != nil {
|
|
|
|
return internal.RecordError(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cn.bw.Flush(); err != nil {
|
|
|
|
return internal.RecordError(ctx, err)
|
2020-03-11 17:26:42 +03:00
|
|
|
}
|
2019-08-16 17:45:45 +03:00
|
|
|
|
2020-07-15 12:07:07 +03:00
|
|
|
internal.WritesCounter.Add(ctx, 1)
|
|
|
|
|
2020-07-09 10:39:46 +03:00
|
|
|
return nil
|
2020-03-11 17:26:42 +03:00
|
|
|
})
|
2018-08-15 11:53:15 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2019-06-08 15:02:51 +03:00
|
|
|
|
|
|
|
func (cn *Conn) deadline(ctx context.Context, timeout time.Duration) time.Time {
|
2019-06-14 14:50:58 +03:00
|
|
|
tm := time.Now()
|
|
|
|
cn.SetUsedAt(tm)
|
|
|
|
|
|
|
|
if timeout > 0 {
|
|
|
|
tm = tm.Add(timeout)
|
|
|
|
}
|
2019-06-09 12:29:23 +03:00
|
|
|
|
2019-06-08 15:02:51 +03:00
|
|
|
if ctx != nil {
|
2019-06-14 14:50:58 +03:00
|
|
|
deadline, ok := ctx.Deadline()
|
2019-06-08 15:02:51 +03:00
|
|
|
if ok {
|
2019-06-14 14:50:58 +03:00
|
|
|
if timeout == 0 {
|
|
|
|
return deadline
|
|
|
|
}
|
2019-06-14 16:00:03 +03:00
|
|
|
if deadline.Before(tm) {
|
|
|
|
return deadline
|
|
|
|
}
|
|
|
|
return tm
|
2019-06-08 15:02:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeout > 0 {
|
2019-06-14 14:50:58 +03:00
|
|
|
return tm
|
2019-06-08 15:02:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return noDeadline
|
|
|
|
}
|