-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.go
188 lines (154 loc) · 4.79 KB
/
settings.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
// Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package db
import (
"sync"
"sync/atomic"
"time"
)
// Settings defines methods to get or set configuration values.
type Settings interface {
// SetLogging enables or disables logging.
SetLogging(bool)
// LoggingEnabled returns true if logging is enabled, false otherwise.
LoggingEnabled() bool
// SetLogger defines which logger to use.
SetLogger(Logger)
// Returns the currently configured logger.
Logger() Logger
// SetPreparedStatementCache enables or disables the prepared statement
// cache.
SetPreparedStatementCache(bool)
// PreparedStatementCacheEnabled returns true if the prepared statement cache
// is enabled, false otherwise.
PreparedStatementCacheEnabled() bool
// SetConnMaxLifetime sets the default maximum amount of time a connection
// may be reused.
SetConnMaxLifetime(time.Duration)
// ConnMaxLifetime returns the default maximum amount of time a connection
// may be reused.
ConnMaxLifetime() time.Duration
// SetMaxIdleConns sets the default maximum number of connections in the idle
// connection pool.
SetMaxIdleConns(int)
// MaxIdleConns returns the default maximum number of connections in the idle
// connection pool.
MaxIdleConns() int
// SetMaxOpenConns sets the default maximum number of open connections to the
// database.
SetMaxOpenConns(int)
// MaxOpenConns returns the default maximum number of open connections to the
// database.
MaxOpenConns() int
}
type settings struct {
sync.RWMutex
preparedStatementCacheEnabled uint32
connMaxLifetime time.Duration
maxOpenConns int
maxIdleConns int
loggingEnabled uint32
queryLogger Logger
queryLoggerMu sync.RWMutex
defaultLogger defaultLogger
}
func (c *settings) Logger() Logger {
c.queryLoggerMu.RLock()
defer c.queryLoggerMu.RUnlock()
if c.queryLogger == nil {
return &c.defaultLogger
}
return c.queryLogger
}
func (c *settings) SetLogger(lg Logger) {
c.queryLoggerMu.Lock()
defer c.queryLoggerMu.Unlock()
c.queryLogger = lg
}
func (c *settings) binaryOption(opt *uint32) bool {
if atomic.LoadUint32(opt) == 1 {
return true
}
return false
}
func (c *settings) setBinaryOption(opt *uint32, value bool) {
if value {
atomic.StoreUint32(opt, 1)
return
}
atomic.StoreUint32(opt, 0)
}
func (c *settings) SetLogging(value bool) {
c.setBinaryOption(&c.loggingEnabled, value)
}
func (c *settings) LoggingEnabled() bool {
return c.binaryOption(&c.loggingEnabled)
}
func (c *settings) SetPreparedStatementCache(value bool) {
c.setBinaryOption(&c.preparedStatementCacheEnabled, value)
}
func (c *settings) PreparedStatementCacheEnabled() bool {
return c.binaryOption(&c.preparedStatementCacheEnabled)
}
func (c *settings) SetConnMaxLifetime(t time.Duration) {
c.Lock()
c.connMaxLifetime = t
c.Unlock()
}
func (c *settings) ConnMaxLifetime() time.Duration {
c.RLock()
defer c.RUnlock()
return c.connMaxLifetime
}
func (c *settings) SetMaxIdleConns(n int) {
c.Lock()
c.maxIdleConns = n
c.Unlock()
}
func (c *settings) MaxIdleConns() int {
c.RLock()
defer c.RUnlock()
return c.maxIdleConns
}
func (c *settings) SetMaxOpenConns(n int) {
c.Lock()
c.maxOpenConns = n
c.Unlock()
}
func (c *settings) MaxOpenConns() int {
c.RLock()
defer c.RUnlock()
return c.maxOpenConns
}
// NewSettings returns a new settings value prefilled with the current default
// settings.
func NewSettings() Settings {
newSettings := *(DefaultSettings.(*settings))
return &newSettings
}
// DefaultSettings provides default global configuration settings for database
// sessions.
var DefaultSettings Settings = &settings{
preparedStatementCacheEnabled: 0,
connMaxLifetime: time.Duration(0),
maxIdleConns: 10,
maxOpenConns: 0,
}