-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclickhouse_options.go
162 lines (150 loc) · 3.76 KB
/
clickhouse_options.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
// Licensed to ClickHouse, Inc. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. ClickHouse, Inc. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package proton
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/url"
"strconv"
"strings"
"time"
"github.com/timeplus-io/proton-go-driver/v2/lib/compress"
)
var CompressionLZ4 compress.Method = compress.LZ4
type Auth struct { // has_control_character
Database string
Username string
Password string
}
type Compression struct {
Method compress.Method
}
type ConnOpenStrategy uint8
const (
ConnOpenInOrder ConnOpenStrategy = iota
ConnOpenRoundRobin
)
func ParseDSN(dsn string) (*Options, error) {
opt := &Options{}
if err := opt.fromDSN(dsn); err != nil {
return nil, err
}
return opt, nil
}
type Options struct {
TLS *tls.Config
Addr []string
Auth Auth
DialContext func(ctx context.Context, addr string) (net.Conn, error)
Debug bool
Settings Settings
Compression *Compression
DialTimeout time.Duration // default 1 second
MaxOpenConns int // default MaxIdleConns + 5
MaxIdleConns int // default 5
ConnMaxLifetime time.Duration // default 1 hour
ConnOpenStrategy ConnOpenStrategy
}
func (o *Options) fromDSN(in string) error {
dsn, err := url.Parse(in)
if err != nil {
return err
}
if o.Settings == nil {
o.Settings = make(Settings)
}
if dsn.User != nil {
o.Auth.Username = dsn.User.Username()
o.Auth.Password, _ = dsn.User.Password()
}
o.Addr = append(o.Addr, strings.Split(dsn.Host, ",")...)
var (
secure bool
params = dsn.Query()
skipVerify bool
)
o.Auth.Database = strings.TrimPrefix(dsn.Path, "/")
for v := range params {
switch v {
case "debug":
o.Debug, _ = strconv.ParseBool(params.Get(v))
case "compress":
if on, _ := strconv.ParseBool(params.Get(v)); on {
o.Compression = &Compression{
Method: CompressionLZ4,
}
}
case "dial_timeout":
duration, err := time.ParseDuration(params.Get(v))
if err != nil {
return fmt.Errorf("proton [dsn parse]: dial timeout: %s", err)
}
o.DialTimeout = duration
case "secure":
secure = true
case "skip_verify":
skipVerify = true
case "connection_open_strategy":
switch params.Get(v) {
case "in_order":
o.ConnOpenStrategy = ConnOpenInOrder
case "round_robin":
o.ConnOpenStrategy = ConnOpenRoundRobin
}
default:
switch p := strings.ToLower(params.Get(v)); p {
case "true":
o.Settings[v] = int(1)
case "false":
o.Settings[v] = int(0)
default:
if n, err := strconv.Atoi(p); err == nil {
o.Settings[v] = n
}
}
}
}
if secure {
o.TLS = &tls.Config{
InsecureSkipVerify: skipVerify,
}
}
o.setDefaults()
return nil
}
func (o *Options) setDefaults() {
if len(o.Auth.Database) == 0 {
o.Auth.Database = "default"
}
if len(o.Auth.Username) == 0 {
o.Auth.Username = "default"
}
if o.DialTimeout == 0 {
o.DialTimeout = time.Second
}
if o.MaxIdleConns <= 0 {
o.MaxIdleConns = 5
}
if o.MaxOpenConns <= 0 {
o.MaxOpenConns = o.MaxIdleConns + 5
}
if o.ConnMaxLifetime == 0 {
o.ConnMaxLifetime = time.Hour
}
}