forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/redis/go-redis/v9/internal/pool/conn.go

199 lines
2.2 KiB
Go
Raw Normal View History

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