From 1d7c9558057d479958fdf1db1d769ab3852a8d35 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sat, 4 Jun 2022 10:42:54 -0500 Subject: [PATCH] Bugfix: don't try to cull connections from an empty pool. The first step in culling connections was to take a subset of the pool's connections from index 0 to numToCull. This leads to an error if numToCull is also zero. --- fiber_orm.nimble | 2 +- src/fiber_orm/pool.nim | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/fiber_orm.nimble b/fiber_orm.nimble index 5ca3522..944a9b5 100644 --- a/fiber_orm.nimble +++ b/fiber_orm.nimble @@ -1,6 +1,6 @@ # Package -version = "1.0.1" +version = "1.0.2" author = "Jonathan Bernard" description = "Lightweight Postgres ORM for Nim." license = "GPL-3.0" diff --git a/src/fiber_orm/pool.nim b/src/fiber_orm/pool.nim index d7c06f7..bf36edd 100644 --- a/src/fiber_orm/pool.nim +++ b/src/fiber_orm/pool.nim @@ -62,14 +62,16 @@ proc maintain(pool: DbConnPool): void = let freeConns = pool.conns.filterIt(it.free) if pool.conns.len > pool.cfg.poolSize and freeConns.len > 0: 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)) - for culled in toCull: - try: culled.conn.close() - except: discard "" - log().debug( - "Trimming pool size. Culled $# free connections. $# connections remaining." % - [$toCull.len, $pool.conns.len]) + + if numToCull > 0: + let toCull = freeConns[0..numToCull] + pool.conns.keepIf((pc) => toCull.allIt(it.id != pc.id)) + for culled in toCull: + try: culled.conn.close() + except: discard "" + log().debug( + "Trimming pool size. Culled $# free connections. $# connections remaining." % + [$toCull.len, $pool.conns.len]) proc take*(pool: DbConnPool): tuple[id: int, conn: DbConn] = pool.maintain