src/norm/pool

Types

Pool[T] = ref object
  defaultSize: Natural
  conns: seq[T]
  getDbProc: proc (): T
  poolExhaustedPolicy: PoolExhaustedPolicy
  lock: Lock
PoolExhaustedError = object of CatchableError
PoolExhaustedPolicy = enum
  pepRaise, pepExtend

Procs

func add[T: DbConn](pool: var Pool; dbConn: T)

Add a connection to the pool.

Use to return a borrowed connection to the pool.

func close(pool: var Pool)
Close the pool by closing and removing all its connetions.
func defaultSize(pool: Pool): Natural
proc newPool[T: DbConn](defaultSize: Positive; getDbProc: proc (): T = getDb;
                        poolExhaustedPolicy = pepRaise): Pool[T]

Create a connection pool of the given size.

poolExhaustedPolicy defines how the pool reacts when a connection is requested but the pool has no connection available:

  • pepRaise (default) means throw PoolExhaustedError
  • pepExtend means throw “add another connection to the pool.”
proc pop[T: DbConn](pool: var Pool[T]): T

Take a connection from the pool.

If you're calling this manually, don't forget to add it back!

func reset(pool: var Pool)
Reset pool size to defaultSize by closing and removing extra connections.
func size(pool: Pool): Natural

Templates

template withDb(pool: var Pool; body: untyped): untyped

Wrapper for DB operations.

Takes a DbConn from a pool as db variable, runs your code in a try block, and returns connection to the pool afterward.