Connection pools¶
rustmy.Pool lends connections to threads, and rustmy.aio.create_pool to
tasks. Both take connect()'s arguments, keep minsize connections open,
open more as needed up to maxsize, and then make callers wait for one to
come back, in order of arrival. Leaving the pool's block closes it, after
waiting for the connections in use.
Options¶
| Argument | Default | |
|---|---|---|
minsize |
1 | connections opened at once (their errors raise there) and kept open |
maxsize |
10 | connections open at most, lent or idle |
pre_ping |
30 | seconds idle after which a connection is pinged before it is lent (None: never) |
pool_recycle |
-1 | seconds after which a connection is closed instead of lent again (-1: never) |
idle_timeout |
None |
seconds unused after which idle connections beyond minsize are closed |
reset_on_release |
False |
reset the session (variables, temporary tables) of each connection given back: a round trip per use |
acquire(timeout=…) |
None |
seconds to wait for a connection before raising rustmy.PoolTimeout (None: forever) |
What a pool does for you¶
- A connection given back with a transaction open is rolled back, and one that is closed or failed is not lent again. Idle ones are lent most recently used first.
rustmy.Poolopens the connections for the minimum on threads of their own, so giving one back never waits for a new one to open.
Without a with block¶
pool.size and pool.freesize count the open and the idle connections;
clear() closes the idle ones, close() the pool (those in use as they come
back) and wait_closed() waits for them; terminate() closes every
connection at once.
Fair waiting¶
Waiters are served first come, first served: under contention a task never waits much longer than the others. aiomysql's and asyncmy's pools let the task that gives a connection back take it again at once, a little faster with more tasks than connections (a few percent; one more turn of the event loop per handover here), at the cost of tasks that wait far longer than the rest: with 32 tasks over 8 connections, the longest wait was 2.6 ms here against 102 ms with asyncmy.
The accounts of both pools are kept in Rust, in the same code, without I/O.
SQLAlchemy's pool¶
With SQLAlchemy, use its own pool (pool_pre_ping=True, and pool_recycle
below the server's wait_timeout): see
SQLAlchemy.