chore: postres driver allow setting the max number of connection from a parameter (#2246)

* postres driver: allow setting the max number of connections from a parameter
This commit is contained in:
Ivan FB 2023-11-24 16:21:22 +01:00 committed by GitHub
parent a1ed517f9c
commit b31c182325
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 6 deletions

View File

@ -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)

View File

@ -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* {.

View File

@ -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():
@ -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)

View File

@ -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):