pub struct Pool { /* private fields */ }Expand description
Connections to one server, for the tasks of one worker thread.
Make one pool per worker, inside giong::Runtime::run: nothing is
shared between threads, so nothing is locked.
use giong_postgres::{Config, Pool};
fn main() -> std::io::Result<()> {
let config = Config::from_url("postgres://app:secret@db.internal/shop")?;
giong::Builder::new().build()?.run(move |_worker| {
let pool = Pool::new(config.clone(), 4);
async move {
let row = pool.query_one("SELECT count(*) FROM orders", &[]).await.unwrap();
let n: i64 = row.get(0).unwrap();
println!("{n} orders");
}
})
}query and the other statement methods do not take a
connection for themselves: they run on the least busy connection, beside
statements from other tasks (pipelining), and the pool opens another
connection, up to its maximum, when every open one is busy. That is the
fast path for statements that stand alone. On one connection the server
answers in order, so a statement waits for those sent before it; the pool
steers new statements away from a connection whose oldest statement has
been running for more than 5 ms. Run statements known to be slow (reports,
migrations) on a connection of their own, from get.
How many connections. Because statements pipeline, a few connections keep the server busy: start with the server’s cores, shared among the workers’ pools. More than that adds no throughput and raises tail latency, since a backend waiting for a core holds up every statement queued on its connection. Statements that wait on disk or on locks need more.
get takes a connection for one task alone, for a
transaction or for session settings. While it is out, no shared statement
runs on it. It comes back when the PooledClient is dropped; if a
transaction was left open, or a statement is still on its way, a
ROLLBACK is queued first.
Transactions never run on the shared connections, since the
statements of other tasks would run inside them. query, query_one,
query_opt and execute refuse BEGIN, COMMIT, ROLLBACK and the
other transaction statements before sending anything
(ErrorKind::Statement). batch_execute may
carry a whole transaction (BEGIN; ...; COMMIT); if a batch leaves one
open, its connection is closed, which rolls it back, and the batch and
every statement sent on that connection after it fail with
ErrorKind::Closed.
Connections the server closed, or that broke, are left behind as soon as that is noticed, which is when it happens: each connection is read continuously.
Implementations§
Source§impl Pool
impl Pool
Sourcepub fn new(config: Config, max_size: usize) -> Self
pub fn new(config: Config, max_size: usize) -> Self
A pool of at most max_size connections to the server in config.
Connections are opened when needed, not here.
Size it from the server’s max_connections: the pools of all workers
of all processes together must stay below it.
§Panics
Panics if max_size is 0.
Sourcepub fn wait_timeout(self, timeout: Duration) -> Self
pub fn wait_timeout(self, timeout: Duration) -> Self
The longest a task waits for a connection when all max_size are
taken: 5 seconds by default. A longer wait fails with
ErrorKind::PoolTimeout.
§Panics
Panics if other clones of this pool exist; set it right after
new.
Sourcepub async fn query(
&self,
sql: &str,
params: &[&dyn ToSql],
) -> Result<Vec<Row>, Error>
pub async fn query( &self, sql: &str, params: &[&dyn ToSql], ) -> Result<Vec<Row>, Error>
Runs a statement on a shared connection and returns its rows; see
Client::query. Transaction statements are refused
(ErrorKind::Statement): run them on a connection from
get.
Sourcepub async fn query_one(
&self,
sql: &str,
params: &[&dyn ToSql],
) -> Result<Row, Error>
pub async fn query_one( &self, sql: &str, params: &[&dyn ToSql], ) -> Result<Row, Error>
Runs a statement that returns exactly one row; see
Client::query_one. Transaction statements are refused.
Sourcepub async fn query_opt(
&self,
sql: &str,
params: &[&dyn ToSql],
) -> Result<Option<Row>, Error>
pub async fn query_opt( &self, sql: &str, params: &[&dyn ToSql], ) -> Result<Option<Row>, Error>
Runs a statement that returns at most one row; see
Client::query_opt. Transaction statements are refused.
Sourcepub async fn execute(
&self,
sql: &str,
params: &[&dyn ToSql],
) -> Result<u64, Error>
pub async fn execute( &self, sql: &str, params: &[&dyn ToSql], ) -> Result<u64, Error>
Runs a statement and returns the number of rows it changed; see
Client::execute. Transaction statements are refused.
Sourcepub async fn batch_execute(&self, sql: &str) -> Result<(), Error>
pub async fn batch_execute(&self, sql: &str) -> Result<(), Error>
Runs several statements without parameters; see
Client::batch_execute.
The batch may hold a whole transaction, BEGIN; ...; COMMIT. If it
leaves one open, the connection is closed, which rolls the
transaction back, and the batch fails with ErrorKind::Closed, as
do the statements other tasks sent on that connection after it: they
would otherwise have run inside the transaction.
Sourcepub async fn get(&self) -> Result<PooledClient, Error>
pub async fn get(&self) -> Result<PooledClient, Error>
Takes a connection for this task alone, for a transaction or session settings:
let mut conn = pool.get().await?;
let tx = conn.transaction().await?;
tx.execute("INSERT INTO audit (what) VALUES ($1)", &[&"signup"]).await?;
tx.commit().await?;Waits up to the wait timeout when every connection is taken or busy and the pool is full.