pool.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/xo/dburl"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  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. conf.Log.Errorf("create new database instance %v failed, err:%v", url, err)
  63. return nil, err
  64. }
  65. conf.Log.Infof("create new database instance: %v", url)
  66. dp.pool[url] = newDb
  67. dp.connections[url] = 1
  68. return newDb, nil
  69. }
  70. func openDB(url string, isTesting bool) (*sql.DB, error) {
  71. if isTesting {
  72. return nil, nil
  73. }
  74. if strings.HasPrefix(strings.ToLower(url), "dm://") {
  75. return openDMDB(url)
  76. }
  77. driver, dsn, err := ParseDBUrl(url)
  78. if err != nil {
  79. return nil, err
  80. }
  81. db, err := sql.Open(driver, dsn)
  82. if err != nil {
  83. return nil, err
  84. }
  85. c := conf.Config
  86. if c != nil && c.Basic.SQLConf != nil && c.Basic.SQLConf.MaxConnections > 0 {
  87. db.SetMaxOpenConns(c.Basic.SQLConf.MaxConnections)
  88. }
  89. return db, nil
  90. }
  91. func openDMDB(url string) (*sql.DB, error) {
  92. db, err := sql.Open("dm", url)
  93. if err != nil {
  94. return nil, err
  95. }
  96. c := conf.Config
  97. if c != nil && c.Basic.SQLConf != nil && c.Basic.SQLConf.MaxConnections > 0 {
  98. db.SetMaxOpenConns(c.Basic.SQLConf.MaxConnections)
  99. }
  100. return db, nil
  101. }
  102. func (dp *dbPool) closeOneConn(url string) error {
  103. dp.Lock()
  104. defer dp.Unlock()
  105. connCount, ok := dp.connections[url]
  106. if !ok {
  107. return nil
  108. }
  109. connCount--
  110. if connCount > 0 {
  111. dp.connections[url] = connCount
  112. return nil
  113. }
  114. conf.Log.Infof("drop database instance: %v", url)
  115. db := dp.pool[url]
  116. // remove db instance from map in order to avoid memory leak
  117. delete(dp.pool, url)
  118. delete(dp.connections, url)
  119. if dp.isTesting {
  120. return nil
  121. }
  122. return db.Close()
  123. }
  124. func ParseDBUrl(urlstr string) (string, string, error) {
  125. u, err := dburl.Parse(urlstr)
  126. if err != nil {
  127. return "", "", err
  128. }
  129. // Open returns *sql.DB from urlstr
  130. // 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
  131. if strings.ToLower(u.Driver) == "sqlite3" {
  132. u.Driver = "sqlite"
  133. }
  134. return u.Driver, u.DSN, nil
  135. }
  136. func FetchDBToOneNode(pool *dbPool, url string) (*sql.DB, error) {
  137. return pool.getOrCreate(url)
  138. }
  139. func ReturnDBFromOneNode(pool *dbPool, url string) error {
  140. return pool.closeOneConn(url)
  141. }
  142. func getDBConnCount(pool *dbPool, url string) int {
  143. return pool.getDBConnCount(url)
  144. }
  145. func ParseDriver(url string) (string, error) {
  146. if strings.HasPrefix(strings.ToLower(url), "dm://") {
  147. return "dm", nil
  148. }
  149. u, err := dburl.Parse(url)
  150. if err != nil {
  151. return "", err
  152. }
  153. return u.Driver, nil
  154. }