Skip to content

Commit

Permalink
chore: postres driver allow setting the max number of connection from…
Browse files Browse the repository at this point in the history
… a parameter (#2246)

* postres driver: allow setting the max number of connections from a parameter
  • Loading branch information
Ivansete-status authored Nov 24, 2023
1 parent a1ed517 commit b31c182
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions apps/wakunode2/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ proc setupProtocols(node: WakuNode,
let archiveDriverRes = ArchiveDriver.new(conf.storeMessageDbUrl,
conf.storeMessageDbVacuum,
conf.storeMessageDbMigration,
conf.storeMaxNumDbConnections,
onErrAction)
if archiveDriverRes.isErr():
return err("failed to setup archive driver: " & archiveDriverRes.error)
Expand Down
5 changes: 5 additions & 0 deletions apps/wakunode2/external_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ type
defaultValue: true,
name: "store-message-db-migration" }: bool

storeMaxNumDbConnections* {.
desc: "Maximum number of simultaneous Postgres connections.",
defaultValue: 50,
name: "store-max-num-db-connections" }: int

## Filter config

filter* {.
Expand Down
8 changes: 6 additions & 2 deletions waku/waku_archive/driver/builder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ proc new*(T: type ArchiveDriver,
url: string,
vacuum: bool,
migrate: bool,
maxNumConn: int,
onErrAction: OnErrHandler):
Result[T, string] =
## url - string that defines the database
## vacuum - if true, a cleanup operation will be applied to the database
## migrate - if true, the database schema will be updated
## onErrAction - called if, e.g., the connection with db got lost forever
## maxNumConn - defines the maximum number of connections to handle simultaneously (Postgres)
## onErrAction - called if, e.g., the connection with db got lost

let dbUrlValidationRes = dburl.validateDbUrl(url)
if dbUrlValidationRes.isErr():
Expand Down Expand Up @@ -81,7 +83,9 @@ proc new*(T: type ArchiveDriver,

of "postgres":
when defined(postgres):
let res = PostgresDriver.new(dbUrl = url, onErrAction = onErrAction)
let res = PostgresDriver.new(dbUrl = url,
maxConnections = maxNumConn,
onErrAction = onErrAction)
if res.isErr():
return err("failed to init postgres archive driver: " & res.error)

Expand Down
11 changes: 7 additions & 4 deletions waku/waku_archive/driver/postgres_driver/postgres_driver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,21 @@ const SelectWithCursorAscStmtDef =
storedAt <= $6
ORDER BY storedAt ASC LIMIT $7;"""

const MaxNumConns = 50 #TODO: we may need to set that from app args (maybe?)
const DefaultMaxNumConns = 50

proc new*(T: type PostgresDriver,
dbUrl: string,
maxConnections: int = MaxNumConns,
maxConnections = DefaultMaxNumConns,
onErrAction: OnErrHandler = nil):
ArchiveDriverResult[T] =

let readConnPool = PgAsyncPool.new(dbUrl, maxConnections).valueOr:
## Very simplistic split of max connections
let maxNumConnOnEachPool = int(maxConnections / 2)

let readConnPool = PgAsyncPool.new(dbUrl, maxNumConnOnEachPool).valueOr:
return err("error creating read conn pool PgAsyncPool")

let writeConnPool = PgAsyncPool.new(dbUrl, maxConnections).valueOr:
let writeConnPool = PgAsyncPool.new(dbUrl, maxNumConnOnEachPool).valueOr:
return err("error creating write conn pool PgAsyncPool")

if not isNil(onErrAction):
Expand Down

0 comments on commit b31c182

Please sign in to comment.