-
Notifications
You must be signed in to change notification settings - Fork 545
/
conn.go
177 lines (166 loc) · 4.04 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package clickhouse
import (
"context"
"crypto/tls"
"fmt"
"log"
"net"
"os"
"time"
"github.com/ClickHouse/clickhouse-go/v2/lib/binary"
"github.com/ClickHouse/clickhouse-go/v2/lib/io"
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
)
func dial(addr string, num int, opt *Options) (*connect, error) {
var (
err error
conn net.Conn
debugf = func(format string, v ...interface{}) {}
)
switch {
case opt.TLS != nil:
conn, err = tls.DialWithDialer(&net.Dialer{Timeout: opt.DialTimeout}, "tcp", addr, opt.TLS)
default:
conn, err = net.DialTimeout("tcp", addr, opt.DialTimeout)
}
if err != nil {
return nil, err
}
if opt.Debug {
debugf = log.New(os.Stdout, fmt.Sprintf("[clickhouse][conn=%d][%s]", num, conn.RemoteAddr()), 0).Printf
}
var compression bool
if opt.Compression != nil {
compression = opt.Compression.Method == CompressionLZ4
}
var (
stream = io.NewStream(conn)
connect = &connect{
opt: opt,
conn: conn,
debugf: debugf,
stream: stream,
encoder: binary.NewEncoder(stream),
decoder: binary.NewDecoder(stream),
revision: proto.ClientTCPProtocolVersion,
compression: compression,
connectedAt: time.Now(),
}
)
if err := connect.handshake(opt.Auth.Database, opt.Auth.Username, opt.Auth.Password); err != nil {
return nil, err
}
return connect, nil
}
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Client/Connection.cpp
type connect struct {
err error
opt *Options
conn net.Conn
debugf func(format string, v ...interface{})
server ServerVersion
stream *io.Stream
closed bool
encoder *binary.Encoder
decoder *binary.Decoder
released bool
revision uint64
compression bool
lastUsedIn time.Time
connectedAt time.Time
}
func (c *connect) settings(querySettings Settings) []proto.Setting {
settings := make([]proto.Setting, 0, len(c.opt.Settings)+len(querySettings))
for k, v := range c.opt.Settings {
settings = append(settings, proto.Setting{
Key: k,
Value: v,
})
}
for k, v := range querySettings {
settings = append(settings, proto.Setting{
Key: k,
Value: v,
})
}
return settings
}
func (c *connect) isBad() bool {
switch {
case c.closed, c.err != nil:
return true
}
if err := c.connCheck(); err != nil {
return true
}
if time.Since(c.lastUsedIn) > time.Minute {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
defer cancel()
if err := c.ping(ctx); err != nil {
return true
}
}
return false
}
func (c *connect) close() error {
if c.closed {
return nil
}
c.closed = true
c.encoder = nil
c.decoder = nil
c.stream.Close()
if err := c.conn.Close(); err != nil {
return err
}
return nil
}
func (c *connect) progress() (*Progress, error) {
var progress proto.Progress
if err := progress.Decode(c.decoder, c.revision); err != nil {
return nil, err
}
c.debugf("[progress] %s", &progress)
return &progress, nil
}
func (c *connect) exception() error {
var e Exception
if err := e.Decode(c.decoder); err != nil {
return err
}
c.debugf("[exception] %s", e.Error())
return &e
}
func (c *connect) sendData(block *proto.Block, name string) error {
c.debugf("[send data] compression=%t", c.compression)
if err := c.encoder.Byte(proto.ClientData); err != nil {
return err
}
if err := c.encoder.String(name); err != nil {
return err
}
if c.compression {
c.stream.Compress(true)
defer func() {
c.stream.Compress(false)
c.encoder.Flush()
}()
}
return block.Encode(c.encoder, c.revision)
}
func (c *connect) readData(packet byte, compressible bool) (*proto.Block, error) {
if _, err := c.decoder.String(); err != nil {
return nil, err
}
if compressible && c.compression {
c.stream.Compress(true)
defer c.stream.Compress(false)
}
var block proto.Block
if err := block.Decode(c.decoder, c.revision); err != nil {
return nil, err
}
block.Packet = packet
c.debugf("[read data] compression=%t. block: columns=%d, rows=%d", c.compression, len(block.Columns), block.Rows())
return &block, nil
}