https://github.com/ysbaddaden/pool
Generic (connection) pools for Crystal
https://github.com/ysbaddaden/pool
crystal
Last synced: 9 months ago
JSON representation
Generic (connection) pools for Crystal
- Host: GitHub
- URL: https://github.com/ysbaddaden/pool
- Owner: ysbaddaden
- Created: 2015-11-18T21:23:50.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2023-02-08T17:24:47.000Z (about 3 years ago)
- Last Synced: 2025-04-26T16:46:06.589Z (11 months ago)
- Topics: crystal
- Language: Crystal
- Size: 11.7 KB
- Stars: 76
- Watchers: 7
- Forks: 11
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# 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.
```crystal
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:
```crystal
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:
```crystal
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:
```crystal
pg.connection do |conn|
result = conn.exec("SELECT * FROM posts")
end
```
## License
Licensed under the Apache License, Version 2.0