Skip to content

Commit

Permalink
more temporary logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status committed Sep 28, 2024
1 parent dd93f4f commit ec12747
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions waku/common/databases/db_postgres/pgasyncpool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ proc `$`(self: PgDbConn): string =
return "nil"

proc new*(T: type PgAsyncPool, dbUrl: string, maxConnections: int): DatabaseResult[T] =
debug "new"
var connString: string

try:
Expand All @@ -64,15 +65,19 @@ proc new*(T: type PgAsyncPool, dbUrl: string, maxConnections: int): DatabaseResu

return ok(pool)

func isLive(pool: PgAsyncPool): bool =
proc isLive(pool: PgAsyncPool): bool =
debug "isLive"
pool.state == PgAsyncPoolState.Live

func isBusy(pool: PgAsyncPool): bool =
proc isBusy(pool: PgAsyncPool): bool =
debug "isBusy"
pool.conns.mapIt(it.busy).allIt(it)

proc close*(pool: PgAsyncPool): Future[Result[void, string]] {.async.} =
## Gracefully wait and close all openned connections

debug "close"

if pool.state == PgAsyncPoolState.Closing:
while pool.state == PgAsyncPoolState.Closing:
await sleepAsync(0.milliseconds) # Do not block the async runtime
Expand Down Expand Up @@ -101,6 +106,7 @@ proc close*(pool: PgAsyncPool): Future[Result[void, string]] {.async.} =
return ok()

proc getFirstFreeConnIndex(pool: PgAsyncPool): DatabaseResult[int] =
debug "getFirstFreeConnIndex", conns = $pool.conns
for index in 0 ..< pool.conns.len:
if pool.conns[index].busy:
continue
Expand All @@ -117,17 +123,20 @@ proc getConnIndex(pool: PgAsyncPool): Future[DatabaseResult[int]] {.async.} =
return err("pool is not live")

if not pool.isBusy():
debug "getConnIndex"
return pool.getFirstFreeConnIndex()

## Pool is busy then

if pool.conns.len == pool.maxConnections:
## Can't create more connections. Wait for a free connection without blocking the async runtime.
while pool.isBusy():
debug "getConnIndex"
await sleepAsync(0.milliseconds)

return pool.getFirstFreeConnIndex()
elif pool.conns.len < pool.maxConnections:
debug "getConnIndex"
## stablish a new connection
let random = rand(0 .. 1000)
let connName = "connName_" & $random
Expand All @@ -145,7 +154,7 @@ proc resetConnPool*(pool: PgAsyncPool): Future[DatabaseResult[void]] {.async.} =
## Forces closing the connection pool.
## This proc is intended to be called when the connection with the database
## got interrupted from the database side or a connectivity problem happened.

debug "resetConnPool"
for i in 0 ..< pool.conns.len:
pool.conns[i].busy = false

Expand All @@ -157,9 +166,11 @@ proc resetConnPool*(pool: PgAsyncPool): Future[DatabaseResult[void]] {.async.} =

proc releaseConn(pool: PgAsyncPool, conn: DbConn) =
## Marks the connection as released.
debug "releaseConn", conns = $pool.conns
for i in 0 ..< pool.conns.len:
if pool.conns[i].dbConn == conn:
pool.conns[i].busy = false
debug "after releaseConn", conns = $pool.conns

const SlowQueryThresholdInNanoSeconds = 1_000_000_000

Expand Down

0 comments on commit ec12747

Please sign in to comment.