General-purpose connection pooling library.
This software is still ALPHA quality. The APIs will be likely to change.
(ql:quickload '(:anypool :dbi))
(use-package :anypool)
(defvar *connection-pool*
(make-pool :name "dbi-connections"
:connector (lambda ()
(dbi:connect :postgres
:database-name "webapp"
:username "fukamachi" :password "1ove1isp"))
:disconnector #'dbi:disconnect
:ping #'dbi:ping
:max-open-count 10
:max-idle-count 2))
(fetch *connection-pool*)
;=> #<DBD.POSTGRES:DBD-POSTGRES-CONNECTION {10054E07C3}>
(pool-open-count *connection-pool*)
;=> 1
(pool-idle-count *connection-pool*)
;=> 0
(putback *** *connection-pool*)
; No value
(pool-open-count *connection-pool*)
;=> 1
(pool-idle-count *connection-pool*)
;=> 1
;; Using `with-connection` macro.
(with-connection (mito:*connection* *connection-pool*)
(mito:find-dao 'user :name "fukamachi"))
[Constructor] make-pool (&key name connector disconnector ping max-open-count max-idle-count timeout idle-timeout)
:name
: The name of a new pool. This is used to print the object.:connector
(Required): A function to make and return a new connection object. It takes no arguments.:disconnector
: A function to disconnect a given object. It takes a single argument which is made by:connector
.:ping
: A function to check if the given object is still available. It takes a single argument.:max-open-count
: The maximum number of concurrently open connections. The default is4
and can be configured with*default-max-open-count*
.:max-idle-count
: The maximum number of idle/pooled connections. The default is2
and can be configured with*default-max-idle-count*
.:timeout
: The milliseconds to wait infetch
when the number of open connection reached to the maximum. Ifnil
, it waits forever. The default isnil
.:idle-timeout
: The milliseconds to disconnect idle resources after they'reputback
ed to the pool. Ifnil
, it won't disconnect automatically. The default isnil
.
Return the maximum number of concurrently open connections. If the number of open connections reached to the limit, fetch
waits until a connection is available.
Set the maximum number of concurrently open connections.
Return the maximum number of idle/pooled connections. If the number of idle connections reached to the limit, extra connections will be disconnected in putback
.
Set the maximum number of idle/pooled connections.
Return the milliseconds to disconnect idle resources after they're putback
ed to the pool. If nil
, this feature is disabled.
Set the milliseconds to disconnect idle resources after they're putback
ed to the pool. If nil
, this feature is disabled.
Return the number of currently open connections. The count is the sum of pool-active-count
and pool-idle-count
.
Return the number of currently in use connections.
Return the number of currently idle connections.
Return an available resource from the pool
. If no open resources available, it makes a new one with a function specified to make-pool
as :connector
.
No open resource available and can't open due to the max-open-count
, this function waits for :timeout
milliseconds. If :timeout
specified to nil
, this waits forever until a new one turns to available.
Send an in-use connection back to pool
to make it reusable by other threads. If the number of idle connections is reached to pool-max-idle-count
, the connection will be disconnected immediately.
- Eitaro Fukamachi (e.arrows@gmail.com)
Copyright (c) 2020 Eitaro Fukamachi (e.arrows@gmail.com)
Licensed under the BSD 2-Clause License.