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"
|
|
|
|
"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
|
|
|
ctx, span := internal.StartSpan(ctx, "redis.with_reader")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil {
|
|
|
|
return internal.RecordError(ctx, span, err)
|
|
|
|
}
|
|
|
|
if err := fn(cn.rd); err != nil {
|
|
|
|
return internal.RecordError(ctx, span, err)
|
|
|
|
}
|
|
|
|
return nil
|
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
|
|
|
ctx, span := internal.StartSpan(ctx, "redis.with_writer")
|
|
|
|
defer span.End()
|
2020-09-27 21:09:46 +00:00
|
|
|
|
2021-04-23 00:08:53 +00:00
|
|
|
if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil {
|
|
|
|
return internal.RecordError(ctx, span, err)
|
|
|
|
}
|
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 {
|
|
|
|
return internal.RecordError(ctx, span, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cn.bw.Flush(); err != nil {
|
|
|
|
return internal.RecordError(ctx, span, err)
|
|
|
|
}
|
2021-02-10 21:28:32 +00:00
|
|
|
|
2021-04-23 00:08:53 +00:00
|
|
|
internal.WritesCounter.Add(ctx, 1)
|
2020-09-27 21:09:46 +00:00
|
|
|
|
2021-04-23 00:08:53 +00:00
|
|
|
return nil
|
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
|
|
|
|
}
|