pool

Generic (connection) pools for Crystal

Pool

Generic (connection) pools for Crystal

Getting Started

A pool tailored for sharing connections across coroutines.

Connections will be checkout from the pool and tied to the current fiber, until they are checkin, and thus be usable by another coroutine. Otherwise they behave the same than the generic pool.

require "pg"
require "pool/connection"

pg = ConnectionPool.new(capacity: 25, timeout: 0.01) do
  PG.connect(ENV["DATABASE_URL"])
end

You may checkout as many connections in parallel as needed, then checkin the connections as required:

conn = pg.checkout
result = conn.exec("SELECT * FROM posts")
pg.checkin(conn)

You may checkout a single connection per coroutine. Trying to checkout another one will always return the same single connection:

conn = pg.connection
result = conn.exec("SELECT * FROM posts")
pg.release

You may also use a block, so the connection will be checkin back into the pool, unless the connection was already checkout, in which case it will be passed to the block, but not returned to the pool:

pg.connection do |conn|
  result = conn.exec("SELECT * FROM posts")
end

License

Licensed under the Apache License, Version 2.0

CHANGELOG

v0.2.3 - May 12, 2016

Fix:

  • Crystal 0.16.0 compatibility

v0.2.2 - Mar 22, 2016

Fix:

  • Crystal 0.14.0 compatibility

v0.2.1 - Jan 15, 2016

Fix:

  • Pool blocks when checkin a connecion too much times.

v0.2.0 - Dec 18, 2015

Feature:

  • Lazily start connections as needed.
Github statistic:
  • 47
  • 33
  • 7
  • 6
  • 1
  • 2 months ago

License:

Links: