-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
255 lines (230 loc) · 6.41 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package makroud
import (
"fmt"
"io"
"net/url"
"github.com/pkg/errors"
)
// ClientOptions configure a Client instance.
type ClientOptions struct {
Port int
Host string
User string
Password string
Database string
SSLMode string
Timezone string
MaxOpenConnections int
MaxIdleConnections int
WithCache bool
SavepointEnabled bool
ApplicationName string
ConnectTimeout int
Logger Logger
Observer Observer
Entropy io.Reader
Node Node
}
func (e ClientOptions) String() string {
uri := fmt.Sprintf("%s://%s:%s@%s:%d/%s?sslmode=%s&timezone=%s",
ClientDriver,
url.QueryEscape(e.User),
url.QueryEscape(e.Password),
e.Host,
e.Port,
e.Database,
e.SSLMode,
e.Timezone,
)
if e.ApplicationName != "" {
uri = fmt.Sprintf("%s&application_name=%s", uri, e.ApplicationName)
}
if e.ConnectTimeout > 0 {
uri = fmt.Sprintf("%s&connect_timeout=%d", uri, e.ConnectTimeout)
}
return uri
}
// NewClientOptions creates a new ClientOptions instance with default options.
func NewClientOptions() *ClientOptions {
return &ClientOptions{
Host: "localhost",
Port: 5432,
User: "postgres",
Password: "",
Database: "postgres",
SSLMode: "disable",
Timezone: "UTC",
MaxOpenConnections: 5,
MaxIdleConnections: 2,
WithCache: true,
SavepointEnabled: false,
ApplicationName: "Makroud",
ConnectTimeout: 10,
Logger: nil,
Observer: nil,
Entropy: nil,
Node: nil,
}
}
// Option is used to define Client configuration.
type Option func(*ClientOptions) error
// Host will configure the Client to use the given server host.
func Host(host string) Option {
return func(options *ClientOptions) error {
options.Host = host
return nil
}
}
// Port will configure the Client to use the given server port.
func Port(port int) Option {
return func(options *ClientOptions) error {
options.Port = port
return nil
}
}
// User will configure the Client to use the given username.
func User(user string) Option {
return func(options *ClientOptions) error {
options.User = user
return nil
}
}
// Password will configure the Client to use the given username.
func Password(password string) Option {
return func(options *ClientOptions) error {
options.Password = password
return nil
}
}
// Database will configure the Client to use the given database name.
func Database(dbname string) Option {
return func(options *ClientOptions) error {
options.Database = dbname
return nil
}
}
// EnableSSL will configure the Client to enable SSL mode.
func EnableSSL() Option {
return func(options *ClientOptions) error {
options.SSLMode = "require"
return nil
}
}
// DisableSSL will configure the Client to disable SSL mode.
func DisableSSL() Option {
return func(options *ClientOptions) error {
options.SSLMode = "disable"
return nil
}
}
// SSLMode will configure the Client with given SSL mode.
func SSLMode(mode string) Option {
return func(options *ClientOptions) error {
options.SSLMode = mode
return nil
}
}
// Timezone will configure the Client to use given timezone.
func Timezone(timezone string) Option {
return func(options *ClientOptions) error {
options.Timezone = timezone
return nil
}
}
// MaxOpenConnections will configure the Client to use this maximum number of open connections to the database.
func MaxOpenConnections(maximum int) Option {
return func(options *ClientOptions) error {
if maximum < 0 {
return errors.New("makroud: the maximum number of open connections must be a positive number")
}
options.MaxOpenConnections = maximum
return nil
}
}
// MaxIdleConnections will configure the Client to keep this maximum number of idle connections in the
// connection pool.
func MaxIdleConnections(maximum int) Option {
return func(options *ClientOptions) error {
if maximum < 0 {
return errors.New("makroud: the maximum number of idle connections must be a positive number")
}
options.MaxIdleConnections = maximum
return nil
}
}
// Cache will configure if the Client should use a cache.
func Cache(enabled bool) Option {
return func(options *ClientOptions) error {
options.WithCache = enabled
return nil
}
}
// WithLogger will attach a logger on Client.
func WithLogger(logger Logger) Option {
return func(options *ClientOptions) error {
if logger == nil {
return errors.New("makroud: a logger instance is required")
}
options.Logger = logger
return nil
}
}
// WithObserver will attach an observer on Client.
func WithObserver(observer Observer) Option {
return func(options *ClientOptions) error {
if observer == nil {
return errors.New("makroud: a observer instance is required")
}
options.Observer = observer
return nil
}
}
// WithEntropy will attach a custom entropy source on Client.
func WithEntropy(entropy io.Reader) Option {
return func(options *ClientOptions) error {
if entropy == nil {
return errors.New("makroud: an entropy source is required")
}
options.Entropy = entropy
return nil
}
}
// WithNode will attach a custom node connector on Client.
// If you use this option, EnableSavepoint, MaxOpenConnections and MaxIdleConnections will be ignored.
func WithNode(node Node) Option {
return func(options *ClientOptions) error {
if node == nil {
return errors.New("makroud: a node is required")
}
options.Node = node
return nil
}
}
// EnableSavepoint will enable the SAVEPOINT postgresql feature.
func EnableSavepoint() Option {
return func(options *ClientOptions) error {
options.SavepointEnabled = true
return nil
}
}
// ApplicationName will configure the Client to use given application name.
func ApplicationName(name string) Option {
return func(options *ClientOptions) error {
if len(name) >= 64 {
return errors.New("makroud: application name must be less than 64 characters")
}
options.ApplicationName = name
return nil
}
}
// ConnectTimeout will configure the Client to wait this maximum number of seconds to obtain a connection.
// Zero or not specified means wait indefinitely.
func ConnectTimeout(timeout int) Option {
return func(options *ClientOptions) error {
if timeout < 0 {
return errors.New("makroud: the maximum wait for a connection must be a positive number")
}
options.ConnectTimeout = timeout
return nil
}
}