|
|
@ -62,14 +62,16 @@ proc maintain(pool: DbConnPool): void =
|
|
|
|
let freeConns = pool.conns.filterIt(it.free)
|
|
|
|
let freeConns = pool.conns.filterIt(it.free)
|
|
|
|
if pool.conns.len > pool.cfg.poolSize and freeConns.len > 0:
|
|
|
|
if pool.conns.len > pool.cfg.poolSize and freeConns.len > 0:
|
|
|
|
let numToCull = min(freeConns.len, pool.conns.len - pool.cfg.poolSize)
|
|
|
|
let numToCull = min(freeConns.len, pool.conns.len - pool.cfg.poolSize)
|
|
|
|
let toCull = freeConns[0..numToCull]
|
|
|
|
|
|
|
|
pool.conns.keepIf((pc) => toCull.allIt(it.id != pc.id))
|
|
|
|
if numToCull > 0:
|
|
|
|
for culled in toCull:
|
|
|
|
let toCull = freeConns[0..numToCull]
|
|
|
|
try: culled.conn.close()
|
|
|
|
pool.conns.keepIf((pc) => toCull.allIt(it.id != pc.id))
|
|
|
|
except: discard ""
|
|
|
|
for culled in toCull:
|
|
|
|
log().debug(
|
|
|
|
try: culled.conn.close()
|
|
|
|
"Trimming pool size. Culled $# free connections. $# connections remaining." %
|
|
|
|
except: discard ""
|
|
|
|
[$toCull.len, $pool.conns.len])
|
|
|
|
log().debug(
|
|
|
|
|
|
|
|
"Trimming pool size. Culled $# free connections. $# connections remaining." %
|
|
|
|
|
|
|
|
[$toCull.len, $pool.conns.len])
|
|
|
|
|
|
|
|
|
|
|
|
proc take*(pool: DbConnPool): tuple[id: int, conn: DbConn] =
|
|
|
|
proc take*(pool: DbConnPool): tuple[id: int, conn: DbConn] =
|
|
|
|
pool.maintain
|
|
|
|
pool.maintain
|
|
|
@ -93,5 +95,5 @@ proc release*(pool: DbConnPool, connId: int): void =
|
|
|
|
|
|
|
|
|
|
|
|
template withConn*(pool: DbConnPool, stmt: untyped): untyped =
|
|
|
|
template withConn*(pool: DbConnPool, stmt: untyped): untyped =
|
|
|
|
let (connId, conn {.inject.}) = take(pool)
|
|
|
|
let (connId, conn {.inject.}) = take(pool)
|
|
|
|
stmt
|
|
|
|
try: stmt
|
|
|
|
release(pool, connId)
|
|
|
|
finally: release(pool, connId)
|
|
|
|