2022-01-23 09:36:24 +02:00
|
|
|
package chpool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/uptrace/go-clickhouse/ch/chproto"
|
|
|
|
)
|
|
|
|
|
|
|
|
var noDeadline = time.Time{}
|
|
|
|
|
|
|
|
type Conn struct {
|
|
|
|
netConn net.Conn
|
|
|
|
|
|
|
|
rd *chproto.Reader
|
|
|
|
wr *chproto.Writer
|
|
|
|
|
|
|
|
ServerInfo chproto.ServerInfo
|
|
|
|
|
|
|
|
Inited bool
|
|
|
|
createdAt time.Time
|
|
|
|
usedAt int64 // atomic
|
|
|
|
closed uint32 // atomic
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConn(netConn net.Conn) *Conn {
|
|
|
|
cn := &Conn{
|
|
|
|
netConn: netConn,
|
|
|
|
rd: chproto.NewReader(netConn),
|
|
|
|
wr: chproto.NewWriter(netConn),
|
|
|
|
createdAt: time.Now(),
|
|
|
|
}
|
2022-03-23 10:48:38 +02:00
|
|
|
cn.SetUsedAt(time.Now())
|
2022-01-23 09:36:24 +02:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2022-05-18 16:23:57 +03:00
|
|
|
func (cn *Conn) LocalAddr() net.Addr {
|
|
|
|
return cn.netConn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
2022-01-23 09:36:24 +02:00
|
|
|
func (cn *Conn) RemoteAddr() net.Addr {
|
|
|
|
return cn.netConn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
2022-04-30 10:30:34 +03:00
|
|
|
func (cn *Conn) Reader(ctx context.Context, timeout time.Duration) *chproto.Reader {
|
|
|
|
_ = cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout))
|
|
|
|
return cn.rd
|
|
|
|
}
|
|
|
|
|
2022-01-23 09:36:24 +02:00
|
|
|
func (cn *Conn) WithReader(
|
|
|
|
ctx context.Context,
|
|
|
|
timeout time.Duration,
|
|
|
|
fn func(rd *chproto.Reader) error,
|
|
|
|
) error {
|
|
|
|
if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fn(cn.rd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) WithWriter(
|
|
|
|
ctx context.Context,
|
|
|
|
timeout time.Duration,
|
|
|
|
fn func(wb *chproto.Writer),
|
|
|
|
) error {
|
|
|
|
if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(cn.wr)
|
|
|
|
|
|
|
|
if err := cn.wr.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) Close() error {
|
|
|
|
if !atomic.CompareAndSwapUint32(&cn.closed, 0, 1) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cn.netConn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *Conn) Closed() bool {
|
|
|
|
return atomic.LoadUint32(&cn.closed) == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|