A simple, fast & thread-safe generic pooling library.
Homepage http://quasilabs.com/pooler/
We need pools for items which have heavy cost of creation and which we can reuse. A typical use case is connection pools.
Pool item creation (as required) is automatic on fetch-from pool. Pool-item’s are created and destroyed using user supplied funcitons. The pool has a idle timeout after which all the existing pool-item’s are destroyed and new ones created (pool-init). The pool has a threshold number of items which it tries to maintain.
Licence : MIT
An Example :
CL-USER> (pooler:make-pool :item-maker
#'(lambda () (clsql:connect '("127.0.0.1" "quasidb" "quasi" "*****")
:database-type :mysql
:if-exists :new))
:item-destroyer
#'(lambda (item) (clsql:disconnect :database item)))
#S(POOLER::POOL
:NAME "Default Pool"
:QUEUE #S(SB-CONCURRENCY:QUEUE
:HEAD (SB-CONCURRENCY::.DUMMY.)
:TAIL (SB-CONCURRENCY::.DUMMY.)
:NAME NIL)
:LOCK #<SB-THREAD:MUTEX "Pool Lock" (free)>
:ITEM-MAKER #<FUNCTION (LAMBDA #) {1005C9BFAB}>
:ITEM-DESTROYER #<FUNCTION (LAMBDA #) {1005CCAAAB}>
:CAPACITY 40
:THRESHOLD 2
:TIMEOUT 300
:LAST-ACCESS 0
:CURRENT-SIZE 0
:TOTAL-USES 0
:TOTAL-CREATED 0
:TOTAL-POOL-INITS 0)
CL-USER> (defvar *mysql-pool* *)
CL-USER> (pooler:fetch-from *mysql-pool*)
#<CLSQL-MYSQL:MYSQL-DATABASE 127.0.0.1/quasidb/quasi OPEN {1007571373}>
CL-USER> (pooler:return-to *mysql-pool* *)
2
CL-USER> (pooler:with-pool (db *mysql-pool*) (clsql:query "show tables;" :database db))
(("LOGIN_DATA"))
("Tables_in_quasidb")
Has been tested with SBCL (Steel Bank Common Lisp), CCL (Clozure Common Lisp) & CMUCL (CMU Common Lisp) on OSX. Should work on GNU/Linux.