-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
127 lines (103 loc) · 2.98 KB
/
option.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
package clickhouse
import (
"fmt"
"time"
"github.com/SimifiniiCTO/simfiny-core-lib/instrumentation"
"go.uber.org/zap"
)
// Option is a function that sets a parameter for the client
type Option func(*Client)
// WithQueryTimeout sets the query timeout
func WithQueryTimeout(timeout *time.Duration) Option {
return func(conn *Client) {
conn.QueryTimeout = timeout
}
}
// WithMaxConnectionRetries sets the max connection retries
func WithMaxConnectionRetries(retries *int) Option {
return func(conn *Client) {
conn.maxConnectionRetries = retries
}
}
// WithMaxConnectionRetryTimeout sets the max connection retry timeout
func WithMaxConnectionRetryTimeout(timeout *time.Duration) Option {
return func(conn *Client) {
conn.maxConnectionRetryTimeout = timeout
}
}
// WithRetrySleep sets the retry sleep
func WithRetrySleep(sleep *time.Duration) Option {
return func(conn *Client) {
conn.retrySleep = sleep
}
}
// WithConnectionString sets the connection string
func WithConnectionString(connectionString *string) Option {
return func(conn *Client) {
conn.connectionURI = connectionString
}
}
// WithMaxIdleConnections sets the max idle connections
func WithMaxIdleConnections(maxIdleConnections *int) Option {
return func(conn *Client) {
conn.maxIdleConnections = maxIdleConnections
}
}
// WithMaxOpenConnections sets the max open connections
func WithMaxOpenConnections(maxOpenConnections *int) Option {
return func(conn *Client) {
conn.maxOpenConnections = maxOpenConnections
}
}
// WithMaxConnectionLifetime sets the max connection lifetime
func WithMaxConnectionLifetime(maxConnectionLifetime *time.Duration) Option {
return func(conn *Client) {
conn.maxConnectionLifetime = maxConnectionLifetime
}
}
// WithInstrumentationClient sets the instrumentation client
func WithInstrumentationClient(instrumentationClient *instrumentation.Client) Option {
return func(conn *Client) {
conn.InstrumentationClient = instrumentationClient
}
}
// WithLogger sets the logger
func WithLogger(logger *zap.Logger) Option {
return func(conn *Client) {
conn.Logger = logger
}
}
// Validate validates the client
func (c *Client) Validate() error {
if c.QueryTimeout == nil {
return fmt.Errorf("query timeout is nil")
}
if c.maxConnectionRetries == nil {
return fmt.Errorf("max connection retries is nil")
}
if c.maxConnectionRetryTimeout == nil {
return fmt.Errorf("max connection retry timeout is nil")
}
if c.retrySleep == nil {
return fmt.Errorf("retry sleep is nil")
}
if c.connectionURI == nil {
return fmt.Errorf("connection string is nil")
}
if c.maxIdleConnections == nil {
return fmt.Errorf("max idle connections is nil")
}
if c.maxOpenConnections == nil {
return fmt.Errorf("max open connections is nil")
}
if c.maxConnectionLifetime == nil {
return fmt.Errorf("max connection lifetime is nil")
}
if c.InstrumentationClient == nil {
return fmt.Errorf("instrumentation client is nil")
}
if c.Logger == nil {
return fmt.Errorf("logger is nil")
}
return nil
}