mirror of https://github.com/waku-org/nwaku.git
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:
parent
a1ed517f9c
commit
b31c182325
|
@ -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)
|
||||
|
|
|
@ -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* {.
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue