pool.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package util
  15. import (
  16. "database/sql"
  17. "strings"
  18. "sync"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "github.com/xo/dburl"
  21. )
  22. var GlobalPool *dbPool
  23. func init() {
  24. // GlobalPool maintained the *sql.DB group by the driver and DSN.
  25. // Multiple sql sources/sinks can directly fetch the `*sql.DB` from the GlobalPool and return it back when they don't need it anymore.
  26. // As multiple sql sources/sinks share the same `*sql.DB`, we can directly control the total count of connections by using `SetMaxOpenConns`
  27. GlobalPool = newDBPool()
  28. }
  29. type dbPool struct {
  30. isTesting bool
  31. sync.RWMutex
  32. // url -> *sql.DB
  33. pool map[string]*sql.DB
  34. // url -> connection count
  35. connections map[string]int
  36. }
  37. func newDBPool() *dbPool {
  38. return &dbPool{
  39. pool: map[string]*sql.DB{},
  40. connections: map[string]int{},
  41. }
  42. }
  43. func (dp *dbPool) getDBConnCount(url string) int {
  44. dp.RLock()
  45. defer dp.RUnlock()
  46. count, ok := dp.connections[url]
  47. if ok {
  48. return count
  49. }
  50. return 0
  51. }
  52. func (dp *dbPool) getOrCreate(url string) (*sql.DB, error) {
  53. dp.Lock()
  54. defer dp.Unlock()
  55. db, ok := dp.pool[url]
  56. if ok {
  57. dp.connections[url] = dp.connections[url] + 1
  58. return db, nil
  59. }
  60. newDb, err := openDB(url, dp.isTesting)
  61. if err != nil {
  62. return nil, err
  63. }
  64. conf.Log.Debugf("create new database instance: %v", url)
  65. dp.pool[url] = newDb
  66. dp.connections[url] = 1
  67. return newDb, nil
  68. }
  69. func openDB(url string, isTesting bool) (*sql.DB, error) {
  70. if isTesting {
  71. return nil, nil
  72. }
  73. if strings.HasPrefix(strings.ToLower(url), "dm://") {
  74. return openDMDB(url)
  75. }
  76. driver, dsn, err := ParseDBUrl(url)
  77. if err != nil {
  78. return nil, err
  79. }
  80. db, err := sql.Open(driver, dsn)
  81. if err != nil {
  82. return nil, err
  83. }
  84. c := conf.Config
  85. if c != nil && c.Basic.SQLConf != nil && c.Basic.SQLConf.MaxConnections > 0 {
  86. db.SetMaxOpenConns(c.Basic.SQLConf.MaxConnections)
  87. }
  88. return db, nil
  89. }
  90. func openDMDB(url string) (*sql.DB, error) {
  91. db, err := sql.Open("dm", url)
  92. if err != nil {
  93. return nil, err
  94. }
  95. c := conf.Config
  96. if c != nil && c.Basic.SQLConf != nil && c.Basic.SQLConf.MaxConnections > 0 {
  97. db.SetMaxOpenConns(c.Basic.SQLConf.MaxConnections)
  98. }
  99. return db, nil
  100. }
  101. func (dp *dbPool) closeOneConn(url string) error {
  102. dp.Lock()
  103. defer dp.Unlock()
  104. connCount, ok := dp.connections[url]
  105. if !ok {
  106. return nil
  107. }
  108. connCount--
  109. if connCount > 0 {
  110. dp.connections[url] = connCount
  111. return nil
  112. }
  113. conf.Log.Debugf("drop database instance: %v", url)
  114. db := dp.pool[url]
  115. // remove db instance from map in order to avoid memory leak
  116. delete(dp.pool, url)
  117. delete(dp.connections, url)
  118. if dp.isTesting {
  119. return nil
  120. }
  121. return db.Close()
  122. }
  123. func ParseDBUrl(urlstr string) (string, string, error) {
  124. u, err := dburl.Parse(urlstr)
  125. if err != nil {
  126. return "", "", err
  127. }
  128. // Open returns *sql.DB from urlstr
  129. // As we use modernc.org/sqlite with `sqlite` as driver name and dburl use `sqlite3` as driver name, we need to fix it before open sql.DB
  130. if strings.ToLower(u.Driver) == "sqlite3" {
  131. u.Driver = "sqlite"
  132. }
  133. return u.Driver, u.DSN, nil
  134. }
  135. func FetchDBToOneNode(pool *dbPool, url string) (*sql.DB, error) {
  136. return pool.getOrCreate(url)
  137. }
  138. func ReturnDBFromOneNode(pool *dbPool, url string) error {
  139. return pool.closeOneConn(url)
  140. }
  141. func getDBConnCount(pool *dbPool, url string) int {
  142. return pool.getDBConnCount(url)
  143. }
  144. func ParseDriver(url string) (string, error) {
  145. if strings.HasPrefix(strings.ToLower(url), "dm://") {
  146. return "dm", nil
  147. }
  148. u, err := dburl.Parse(url)
  149. if err != nil {
  150. return "", err
  151. }
  152. return u.Driver, nil
  153. }