/
clickhouse.go
134 lines (110 loc) · 3.03 KB
/
clickhouse.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
package clickhouse
import (
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/scratchdata/scratchdata/pkg/util"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/rs/zerolog/log"
)
type ClickhouseServer struct {
Host string `mapstructure:"host"`
HTTPProtocol string `mapstructure:"http_protocol"`
HTTPPort int `mapstructure:"http_port"`
TCPPort int `mapstructure:"tcp_port"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Database string `mapstructure:"database"`
TLS bool `mapstructure:"tls"`
StoragePolicy string `mapstructure:"storage_policy"`
MaxOpenConns int `mapstructure:"max_open_conns"`
MaxIdleConns int `mapstructure:"max_idle_conns"`
ConnMaxLifetimeSecs int `mapstructure:"conn_max_lifetime_secs"`
conn driver.Conn
}
func openConn(s *ClickhouseServer) (driver.Conn, error) {
options := &clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", s.Host, s.TCPPort)},
Auth: clickhouse.Auth{
Username: s.Username,
Password: s.Password,
},
Debug: false,
DialTimeout: 120 * time.Second,
}
if s.MaxOpenConns > 0 {
options.MaxOpenConns = s.MaxOpenConns
}
if s.MaxIdleConns > 0 {
options.MaxIdleConns = s.MaxIdleConns
}
if s.ConnMaxLifetimeSecs > 0 {
options.ConnMaxLifetime = time.Second * time.Duration(s.ConnMaxLifetimeSecs)
}
if s.TLS {
options.TLS = &tls.Config{
InsecureSkipVerify: true,
}
}
var ctx = context.Background()
var conn, err = clickhouse.Open(options)
if err != nil {
return nil, err
}
if err := conn.Ping(ctx); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
log.Err(err).
Int("code", int(exception.Code)).
Str("message", exception.Message).
Str("stackTrace", exception.StackTrace).
Send()
}
return nil, err
}
return conn, nil
}
func (s *ClickhouseServer) Close() error {
return s.conn.Close()
}
func (s *ClickhouseServer) httpQuery(query string) (io.ReadCloser, error) {
url := fmt.Sprintf("%s://%s:%d", s.HTTPProtocol, s.Host, s.HTTPPort)
var jsonStr = []byte(query)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
return nil, err
}
req.Header.Set("X-Clickhouse-User", s.Username)
req.Header.Set("X-Clickhouse-Key", s.Password)
req.Header.Set("X-Clickhouse-Database", s.Database)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Error().Err(err).Msg("request failed")
resp.Body.Close()
return nil, err
}
if resp.StatusCode != 200 {
errMsg, readErr := io.ReadAll(resp.Body)
resp.Body.Close()
if readErr != nil {
return nil, readErr
}
return nil, errors.New(string(errMsg))
}
return resp.Body, nil
}
func OpenServer(settings map[string]any) (*ClickhouseServer, error) {
srv := util.ConfigToStruct[ClickhouseServer](settings)
conn, err := openConn(srv)
if err != nil {
return nil, fmt.Errorf("OpenServer: %w", err)
}
srv.conn = conn
return srv, nil
}