http urls monitor.

settings.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package db
  22. import (
  23. "sync"
  24. "sync/atomic"
  25. "time"
  26. )
  27. // Settings defines methods to get or set configuration values.
  28. type Settings interface {
  29. // SetLogging enables or disables logging.
  30. SetLogging(bool)
  31. // LoggingEnabled returns true if logging is enabled, false otherwise.
  32. LoggingEnabled() bool
  33. // SetLogger defines which logger to use.
  34. SetLogger(Logger)
  35. // Returns the currently configured logger.
  36. Logger() Logger
  37. // SetPreparedStatementCache enables or disables the prepared statement
  38. // cache.
  39. SetPreparedStatementCache(bool)
  40. // PreparedStatementCacheEnabled returns true if the prepared statement cache
  41. // is enabled, false otherwise.
  42. PreparedStatementCacheEnabled() bool
  43. // SetConnMaxLifetime sets the default maximum amount of time a connection
  44. // may be reused.
  45. SetConnMaxLifetime(time.Duration)
  46. // ConnMaxLifetime returns the default maximum amount of time a connection
  47. // may be reused.
  48. ConnMaxLifetime() time.Duration
  49. // SetMaxIdleConns sets the default maximum number of connections in the idle
  50. // connection pool.
  51. SetMaxIdleConns(int)
  52. // MaxIdleConns returns the default maximum number of connections in the idle
  53. // connection pool.
  54. MaxIdleConns() int
  55. // SetMaxOpenConns sets the default maximum number of open connections to the
  56. // database.
  57. SetMaxOpenConns(int)
  58. // MaxOpenConns returns the default maximum number of open connections to the
  59. // database.
  60. MaxOpenConns() int
  61. }
  62. type settings struct {
  63. sync.RWMutex
  64. preparedStatementCacheEnabled uint32
  65. connMaxLifetime time.Duration
  66. maxOpenConns int
  67. maxIdleConns int
  68. loggingEnabled uint32
  69. queryLogger Logger
  70. queryLoggerMu sync.RWMutex
  71. defaultLogger defaultLogger
  72. }
  73. func (c *settings) Logger() Logger {
  74. c.queryLoggerMu.RLock()
  75. defer c.queryLoggerMu.RUnlock()
  76. if c.queryLogger == nil {
  77. return &c.defaultLogger
  78. }
  79. return c.queryLogger
  80. }
  81. func (c *settings) SetLogger(lg Logger) {
  82. c.queryLoggerMu.Lock()
  83. defer c.queryLoggerMu.Unlock()
  84. c.queryLogger = lg
  85. }
  86. func (c *settings) binaryOption(opt *uint32) bool {
  87. if atomic.LoadUint32(opt) == 1 {
  88. return true
  89. }
  90. return false
  91. }
  92. func (c *settings) setBinaryOption(opt *uint32, value bool) {
  93. if value {
  94. atomic.StoreUint32(opt, 1)
  95. return
  96. }
  97. atomic.StoreUint32(opt, 0)
  98. }
  99. func (c *settings) SetLogging(value bool) {
  100. c.setBinaryOption(&c.loggingEnabled, value)
  101. }
  102. func (c *settings) LoggingEnabled() bool {
  103. return c.binaryOption(&c.loggingEnabled)
  104. }
  105. func (c *settings) SetPreparedStatementCache(value bool) {
  106. c.setBinaryOption(&c.preparedStatementCacheEnabled, value)
  107. }
  108. func (c *settings) PreparedStatementCacheEnabled() bool {
  109. return c.binaryOption(&c.preparedStatementCacheEnabled)
  110. }
  111. func (c *settings) SetConnMaxLifetime(t time.Duration) {
  112. c.Lock()
  113. c.connMaxLifetime = t
  114. c.Unlock()
  115. }
  116. func (c *settings) ConnMaxLifetime() time.Duration {
  117. c.RLock()
  118. defer c.RUnlock()
  119. return c.connMaxLifetime
  120. }
  121. func (c *settings) SetMaxIdleConns(n int) {
  122. c.Lock()
  123. c.maxIdleConns = n
  124. c.Unlock()
  125. }
  126. func (c *settings) MaxIdleConns() int {
  127. c.RLock()
  128. defer c.RUnlock()
  129. return c.maxIdleConns
  130. }
  131. func (c *settings) SetMaxOpenConns(n int) {
  132. c.Lock()
  133. c.maxOpenConns = n
  134. c.Unlock()
  135. }
  136. func (c *settings) MaxOpenConns() int {
  137. c.RLock()
  138. defer c.RUnlock()
  139. return c.maxOpenConns
  140. }
  141. // NewSettings returns a new settings value prefilled with the current default
  142. // settings.
  143. func NewSettings() Settings {
  144. newSettings := *(DefaultSettings.(*settings))
  145. return &newSettings
  146. }
  147. // DefaultSettings provides default global configuration settings for database
  148. // sessions.
  149. var DefaultSettings Settings = &settings{
  150. preparedStatementCacheEnabled: 0,
  151. connMaxLifetime: time.Duration(0),
  152. maxIdleConns: 10,
  153. maxOpenConns: 0,
  154. }