pool.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright (c) 2018 David Crawshaw <david@zentus.com>
  2. //
  3. // Permission to use, copy, modify, and distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. package sqlitex
  15. import (
  16. "context"
  17. "fmt"
  18. "runtime/trace"
  19. "sync"
  20. "time"
  21. "github.com/go-llsqlite/crawshaw"
  22. )
  23. // Pool is a pool of SQLite connections.
  24. //
  25. // It is safe for use by multiple goroutines concurrently.
  26. //
  27. // Typically, a goroutine that needs to use an SQLite *Conn
  28. // Gets it from the pool and defers its return:
  29. //
  30. // conn := dbpool.Get(nil)
  31. // defer dbpool.Put(conn)
  32. //
  33. // As Get may block, a context can be used to return if a task
  34. // is cancelled. In this case the Conn returned will be nil:
  35. //
  36. // conn := dbpool.Get(ctx)
  37. // if conn == nil {
  38. // return context.Canceled
  39. // }
  40. // defer dbpool.Put(conn)
  41. type Pool struct {
  42. // If checkReset, the Put method checks all of the connection's
  43. // prepared statements and ensures they were correctly cleaned up.
  44. // If they were not, Put will panic with details.
  45. //
  46. // TODO: export this? Is it enough of a performance concern?
  47. checkReset bool
  48. free chan *sqlite.Conn
  49. closed chan struct{}
  50. all map[*sqlite.Conn]context.CancelFunc
  51. mu sync.RWMutex
  52. }
  53. // Open opens a fixed-size pool of SQLite connections.
  54. //
  55. // A flags value of 0 defaults to:
  56. //
  57. // SQLITE_OPEN_READWRITE
  58. // SQLITE_OPEN_CREATE
  59. // SQLITE_OPEN_WAL
  60. // SQLITE_OPEN_URI
  61. // SQLITE_OPEN_NOMUTEX
  62. func Open(uri string, flags sqlite.OpenFlags, poolSize int) (pool *Pool, err error) {
  63. return OpenInit(nil, uri, flags, poolSize, "")
  64. }
  65. // OpenInit opens a fixed-size pool of SQLite connections, each initialized
  66. // with initScript.
  67. //
  68. // A flags value of 0 defaults to:
  69. //
  70. // SQLITE_OPEN_READWRITE
  71. // SQLITE_OPEN_CREATE
  72. // SQLITE_OPEN_WAL
  73. // SQLITE_OPEN_URI
  74. // SQLITE_OPEN_NOMUTEX
  75. //
  76. // Each initScript is run an all Conns in the Pool. This is intended for PRAGMA
  77. // or CREATE TEMP VIEW which need to be run on all connections.
  78. //
  79. // WARNING: Ensure all queries in initScript are completely idempotent, meaning
  80. // that running it multiple times is the same as running it once. For example
  81. // do not run INSERT in any of the initScripts or else it may create duplicate
  82. // data unintentionally or fail.
  83. func OpenInit(ctx context.Context, uri string, flags sqlite.OpenFlags, poolSize int, initScript string) (pool *Pool, err error) {
  84. if uri == ":memory:" {
  85. return nil, strerror{msg: `sqlite: ":memory:" does not work with multiple connections, use "file::memory:?mode=memory"`}
  86. }
  87. p := &Pool{
  88. checkReset: true,
  89. free: make(chan *sqlite.Conn, poolSize),
  90. closed: make(chan struct{}),
  91. }
  92. defer func() {
  93. // If an error occurred, call Close outside the lock so this doesn't deadlock.
  94. if err != nil {
  95. p.Close()
  96. }
  97. }()
  98. if flags == 0 {
  99. flags = sqlite.SQLITE_OPEN_READWRITE |
  100. sqlite.SQLITE_OPEN_CREATE |
  101. sqlite.SQLITE_OPEN_WAL |
  102. sqlite.SQLITE_OPEN_URI |
  103. sqlite.SQLITE_OPEN_NOMUTEX
  104. }
  105. // sqlitex_pool is also defined in package sqlite
  106. const sqlitex_pool = sqlite.OpenFlags(0x01000000)
  107. flags |= sqlitex_pool
  108. p.all = make(map[*sqlite.Conn]context.CancelFunc)
  109. for i := 0; i < poolSize; i++ {
  110. conn, err := sqlite.OpenConn(uri, flags)
  111. if err != nil {
  112. return nil, err
  113. }
  114. p.free <- conn
  115. p.all[conn] = func() {}
  116. if initScript != "" {
  117. conn.SetInterrupt(ctx.Done())
  118. if err := ExecScript(conn, initScript); err != nil {
  119. return nil, err
  120. }
  121. conn.SetInterrupt(nil)
  122. }
  123. }
  124. return p, nil
  125. }
  126. // Get returns an SQLite connection from the Pool.
  127. //
  128. // If no Conn is available, Get will block until one is, or until either the
  129. // Pool is closed or the context expires. If no Conn can be obtained, nil is
  130. // returned.
  131. //
  132. // The provided context is used to control the execution lifetime of the
  133. // connection. See Conn.SetInterrupt for details.
  134. //
  135. // Applications must ensure that all non-nil Conns returned from Get are
  136. // returned to the same Pool with Put.
  137. func (p *Pool) Get(ctx context.Context) *sqlite.Conn {
  138. var tr sqlite.Tracer
  139. if ctx != nil {
  140. tr = &tracer{ctx: ctx}
  141. } else {
  142. ctx = context.Background()
  143. }
  144. var cancel context.CancelFunc
  145. ctx, cancel = context.WithCancel(ctx)
  146. outer:
  147. select {
  148. case conn := <-p.free:
  149. p.mu.Lock()
  150. defer p.mu.Unlock()
  151. select {
  152. case <-p.closed:
  153. p.free <- conn
  154. break outer
  155. default:
  156. }
  157. conn.SetTracer(tr)
  158. conn.SetInterrupt(ctx.Done())
  159. p.all[conn] = cancel
  160. return conn
  161. case <-ctx.Done():
  162. case <-p.closed:
  163. }
  164. cancel()
  165. return nil
  166. }
  167. // Put puts an SQLite connection back into the Pool.
  168. //
  169. // Put will panic if conn is nil or if the conn was not originally created by
  170. // p.
  171. //
  172. // Applications must ensure that all non-nil Conns returned from Get are
  173. // returned to the same Pool with Put.
  174. func (p *Pool) Put(conn *sqlite.Conn) {
  175. if conn == nil {
  176. panic("attempted to Put a nil Conn into Pool")
  177. }
  178. if p.checkReset {
  179. query := conn.CheckReset()
  180. if query != "" {
  181. panic(fmt.Sprintf(
  182. "connection returned to pool has active statement: %q",
  183. query))
  184. }
  185. }
  186. p.mu.RLock()
  187. cancel, found := p.all[conn]
  188. p.mu.RUnlock()
  189. if !found {
  190. panic("sqlite.Pool.Put: connection not created by this pool")
  191. }
  192. cancel()
  193. p.free <- conn
  194. }
  195. // PoolCloseTimeout is the maximum time for Pool.Close to wait for all Conns to
  196. // be returned to the Pool.
  197. //
  198. // Do not modify this concurrently with calling Pool.Close.
  199. var PoolCloseTimeout = 5 * time.Second
  200. // Close interrupts and closes all the connections in the Pool.
  201. //
  202. // Close blocks until all connections are returned to the Pool.
  203. //
  204. // Close will panic if not all connections are returned before
  205. // PoolCloseTimeout.
  206. func (p *Pool) Close() (err error) {
  207. close(p.closed)
  208. p.mu.RLock()
  209. for _, cancel := range p.all {
  210. cancel()
  211. }
  212. p.mu.RUnlock()
  213. timeout := time.After(PoolCloseTimeout)
  214. for closed := 0; closed < len(p.all); closed++ {
  215. select {
  216. case conn := <-p.free:
  217. err2 := conn.Close()
  218. if err == nil {
  219. err = err2
  220. }
  221. case <-timeout:
  222. panic("not all connections returned to Pool before timeout")
  223. }
  224. }
  225. return
  226. }
  227. type strerror struct {
  228. msg string
  229. }
  230. func (err strerror) Error() string { return err.msg }
  231. type tracer struct {
  232. ctx context.Context
  233. ctxStack []context.Context
  234. taskStack []*trace.Task
  235. }
  236. func (t *tracer) pctx() context.Context {
  237. if len(t.ctxStack) != 0 {
  238. return t.ctxStack[len(t.ctxStack)-1]
  239. }
  240. return t.ctx
  241. }
  242. func (t *tracer) Push(name string) {
  243. ctx, task := trace.NewTask(t.pctx(), name)
  244. t.ctxStack = append(t.ctxStack, ctx)
  245. t.taskStack = append(t.taskStack, task)
  246. }
  247. func (t *tracer) Pop() {
  248. t.taskStack[len(t.taskStack)-1].End()
  249. t.taskStack = t.taskStack[:len(t.taskStack)-1]
  250. t.ctxStack = t.ctxStack[:len(t.ctxStack)-1]
  251. }
  252. func (t *tracer) NewTask(name string) sqlite.TracerTask {
  253. ctx, task := trace.NewTask(t.pctx(), name)
  254. return &tracerTask{
  255. ctx: ctx,
  256. task: task,
  257. }
  258. }
  259. type tracerTask struct {
  260. ctx context.Context
  261. task *trace.Task
  262. region *trace.Region
  263. }
  264. func (t *tracerTask) StartRegion(regionType string) {
  265. if t.region != nil {
  266. panic("sqlitex.tracerTask.StartRegion: already in region")
  267. }
  268. t.region = trace.StartRegion(t.ctx, regionType)
  269. }
  270. func (t *tracerTask) EndRegion() {
  271. t.region.End()
  272. t.region = nil
  273. }
  274. func (t *tracerTask) End() {
  275. t.task.End()
  276. }