123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- // 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,
- }
|