db_pool

Types

pub opaque type Message(conn, err)

A Pool configuration. Holds the size of the pool and functions for opening and closing connections. Can also be provided a function to be run every interval milliseconds.

Example:

  let db_pool = db_pool.new()
    |> db_pool.size(5)
    |> db_pool.interval(1000)
    |> db_pool.on_open(database.open)
    |> db_pool.on_close(database.close)
    |> db_pool.on_interval(database.ping)
pub opaque type Pool(conn, err)
pub type PoolError(err) {
  ConnectionError(err)
  ConnectionTimeout
}

Constructors

  • ConnectionError(err)
  • ConnectionTimeout

Values

pub fn checkin(
  pool: process.Subject(Message(conn, err)),
  conn: conn,
  caller: process.Pid,
) -> Nil

Returns a connection back to the pool.

pub fn checkout(
  pool: process.Subject(Message(conn, err)),
  caller: process.Pid,
  timeout: Int,
) -> Result(conn, PoolError(err))

Attempts to check out a connection from the pool.

pub fn interval(
  pool: Pool(conn, err),
  interval: Int,
) -> Pool(conn, err)

Sets the Pool’s interval value. The pool will call the configured on_interval function every interval milliseconds.

pub fn new() -> Pool(conn, err)

Returns a Pool that needs to be configured.

pub fn on_close(
  pool: Pool(conn, err),
  handle_close: fn(conn) -> Result(Nil, err),
) -> Pool(conn, err)

Sets the Pool’s on_close function. The provided function will be called on each idle connection when the pool is shut down or exits.

pub fn on_interval(
  pool: Pool(conn, err),
  handle_interval: fn(conn) -> Nil,
) -> Pool(conn, err)

Sets the Pool’s on_interval function. The provided function will be called every interval milliseconds.

pub fn on_open(
  pool: Pool(conn, err),
  handle_open: fn() -> Result(conn, err),
) -> Pool(conn, err)

Sets the Pool’s on_open function. The provided function will be called at startup to create connections.

pub fn shutdown(
  pool: process.Subject(Message(conn, err)),
  timeout: Int,
) -> Result(Nil, PoolError(err))

Shuts down the pool and any idle connections.

pub fn size(pool: Pool(conn, err), size: Int) -> Pool(conn, err)

Sets the size of the pool. At startup the pool will create size number of connections.

pub fn start(
  pool: Pool(conn, err),
  name: process.Name(Message(conn, err)),
  timeout: Int,
) -> Result(
  actor.Started(process.Subject(Message(conn, err))),
  actor.StartError,
)

Starts a connection pool.

pub fn supervised(
  pool: Pool(conn, err),
  name: process.Name(Message(conn, err)),
  timeout: Int,
) -> supervision.ChildSpecification(
  process.Subject(Message(conn, err)),
)

Creates a supervision.ChildSpecification so the pool can be added to an application’s supervision tree.

Search Document