mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-03 22:43:09 +00:00
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
d840d3e7cf
commit
75cbea86cb
@ -491,6 +491,7 @@ proc setupProtocols(node: WakuNode,
|
|||||||
let archiveDriverRes = ArchiveDriver.new(conf.storeMessageDbUrl,
|
let archiveDriverRes = ArchiveDriver.new(conf.storeMessageDbUrl,
|
||||||
conf.storeMessageDbVacuum,
|
conf.storeMessageDbVacuum,
|
||||||
conf.storeMessageDbMigration,
|
conf.storeMessageDbMigration,
|
||||||
|
conf.storeMaxNumDbConnections,
|
||||||
onErrAction)
|
onErrAction)
|
||||||
if archiveDriverRes.isErr():
|
if archiveDriverRes.isErr():
|
||||||
return err("failed to setup archive driver: " & archiveDriverRes.error)
|
return err("failed to setup archive driver: " & archiveDriverRes.error)
|
||||||
|
|||||||
@ -273,6 +273,11 @@ type
|
|||||||
defaultValue: true,
|
defaultValue: true,
|
||||||
name: "store-message-db-migration" }: bool
|
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 config
|
||||||
|
|
||||||
filter* {.
|
filter* {.
|
||||||
|
|||||||
@ -28,12 +28,14 @@ proc new*(T: type ArchiveDriver,
|
|||||||
url: string,
|
url: string,
|
||||||
vacuum: bool,
|
vacuum: bool,
|
||||||
migrate: bool,
|
migrate: bool,
|
||||||
|
maxNumConn: int,
|
||||||
onErrAction: OnErrHandler):
|
onErrAction: OnErrHandler):
|
||||||
Result[T, string] =
|
Result[T, string] =
|
||||||
## url - string that defines the database
|
## url - string that defines the database
|
||||||
## vacuum - if true, a cleanup operation will be applied to the database
|
## vacuum - if true, a cleanup operation will be applied to the database
|
||||||
## migrate - if true, the database schema will be updated
|
## 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)
|
let dbUrlValidationRes = dburl.validateDbUrl(url)
|
||||||
if dbUrlValidationRes.isErr():
|
if dbUrlValidationRes.isErr():
|
||||||
@ -81,7 +83,9 @@ proc new*(T: type ArchiveDriver,
|
|||||||
|
|
||||||
of "postgres":
|
of "postgres":
|
||||||
when defined(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():
|
if res.isErr():
|
||||||
return err("failed to init postgres archive driver: " & res.error)
|
return err("failed to init postgres archive driver: " & res.error)
|
||||||
|
|
||||||
|
|||||||
@ -84,18 +84,21 @@ const SelectWithCursorAscStmtDef =
|
|||||||
storedAt <= $6
|
storedAt <= $6
|
||||||
ORDER BY storedAt ASC LIMIT $7;"""
|
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,
|
proc new*(T: type PostgresDriver,
|
||||||
dbUrl: string,
|
dbUrl: string,
|
||||||
maxConnections: int = MaxNumConns,
|
maxConnections = DefaultMaxNumConns,
|
||||||
onErrAction: OnErrHandler = nil):
|
onErrAction: OnErrHandler = nil):
|
||||||
ArchiveDriverResult[T] =
|
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")
|
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")
|
return err("error creating write conn pool PgAsyncPool")
|
||||||
|
|
||||||
if not isNil(onErrAction):
|
if not isNil(onErrAction):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user