Skip to content

stndrs/db_pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

db_pool

Package Version Hex Docs

A database connection pool.

This library eagerly opens connections at startup. Connections can be checked out from the pool, marking them as active. Active connections are associated with the Pid of the caller that checked it out. Checking in connections will remove the association with the caller. Because active connections are associated with the Pid of a calling process, subsequent calls to check out a connection from the same process will return the already checked out connection.

If all connections are checked out, new callers attempting to check out will be added to a FIFO queue. Callers waiting in the queue will be given connections as they become available.

Callers are monitored so if they crash their checked out connections can be added back to the pool.

import database
import db_pool
import gleam/erlang/process

pub fn main() -> Nil {
  let name = process.new_name("db_pool")

  let db_pool =
    db_pool.new()
    |> db_pool.size(5)
    |> db_pool.on_open(database.open)
    |> db_pool.on_close(database.close)
    |> db_pool.on_interval(database.ping)

  let assert Ok(started) = db_pool.start(db_pool, name, 1000)

  let pool = started.data

  let self = process.self()

  let assert Ok(conn) = db_pool.checkout(pool, self, 500)

  let assert Ok(users) = database.query("SELECT * FROM users", conn)

  db_pool.checkin(pool, conn, self)

  let assert Ok(_) = db_pool.shutdown(pool, 1000)
}

Installation

gleam add db_pool

Development

gleam run   # Run the project
gleam test  # Run the tests

Acknowledgements

Inspired in part by bath.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published