From bab3f23189efa6820d26b4db291b14641cc7cb39 Mon Sep 17 00:00:00 2001 From: Hitalo Souza Date: Thu, 10 Apr 2025 01:17:32 +0100 Subject: [PATCH] db: connection pool (#24161) --- vlib/db/mysql/pool.v | 38 ++++++++++++++++++++++++++++++++++++++ vlib/db/pg/pool.v | 38 ++++++++++++++++++++++++++++++++++++++ vlib/db/sqlite/pool.v | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 vlib/db/mysql/pool.v create mode 100644 vlib/db/pg/pool.v create mode 100644 vlib/db/sqlite/pool.v diff --git a/vlib/db/mysql/pool.v b/vlib/db/mysql/pool.v new file mode 100644 index 0000000000..648921fb8f --- /dev/null +++ b/vlib/db/mysql/pool.v @@ -0,0 +1,38 @@ +module mysql + +pub struct ConnectionPool { +mut: + connections chan DB + config Config +} + +// new_connection_pool creates a new connection pool with the given size and configuration. +pub fn new_connection_pool(config Config, size int) !ConnectionPool { + mut connections := chan DB{cap: size} + for _ in 0 .. size { + conn := connect(config)! + connections <- conn + } + return ConnectionPool{ + connections: connections + config: config + } +} + +// acquire gets a connection from the pool +pub fn (mut pool ConnectionPool) acquire() !DB { + return <-pool.connections or { return error('Failed to acquire a connection from the pool') } +} + +// release returns a connection back to the pool. +pub fn (mut pool ConnectionPool) release(conn DB) { + pool.connections <- conn +} + +// close closes all connections in the pool. +pub fn (mut pool ConnectionPool) close() { + for _ in 0 .. pool.connections.len { + mut conn := <-pool.connections or { break } + conn.close() + } +} diff --git a/vlib/db/pg/pool.v b/vlib/db/pg/pool.v new file mode 100644 index 0000000000..e8a32591d2 --- /dev/null +++ b/vlib/db/pg/pool.v @@ -0,0 +1,38 @@ +module pg + +pub struct ConnectionPool { +mut: + connections chan DB + config Config +} + +// new_connection_pool creates a new connection pool with the given size and configuration. +pub fn new_connection_pool(config Config, size int) !ConnectionPool { + mut connections := chan DB{cap: size} + for _ in 0 .. size { + conn := connect(config)! + connections <- conn + } + return ConnectionPool{ + connections: connections + config: config + } +} + +// acquire gets a connection from the pool +pub fn (mut pool ConnectionPool) acquire() !DB { + return <-pool.connections or { return error('Failed to acquire a connection from the pool') } +} + +// release returns a connection back to the pool. +pub fn (mut pool ConnectionPool) release(conn DB) { + pool.connections <- conn +} + +// close closes all connections in the pool. +pub fn (mut pool ConnectionPool) close() { + for _ in 0 .. pool.connections.len { + conn := <-pool.connections or { break } + conn.close() + } +} diff --git a/vlib/db/sqlite/pool.v b/vlib/db/sqlite/pool.v new file mode 100644 index 0000000000..a37b6c26c2 --- /dev/null +++ b/vlib/db/sqlite/pool.v @@ -0,0 +1,38 @@ +module sqlite + +pub struct ConnectionPool { +mut: + connections chan DB + config string +} + +// new_connection_pool creates a new connection pool with the given size and configuration. +pub fn new_connection_pool(config string, size int) !ConnectionPool { + mut connections := chan DB{cap: size} + for _ in 0 .. size { + conn := connect(config)! + connections <- conn + } + return ConnectionPool{ + connections: connections + config: config + } +} + +// acquire gets a connection from the pool +pub fn (mut pool ConnectionPool) acquire() !DB { + return <-pool.connections or { return error('Failed to acquire a connection from the pool') } +} + +// release returns a connection back to the pool. +pub fn (mut pool ConnectionPool) release(conn DB) { + pool.connections <- conn +} + +// close closes all connections in the pool. +pub fn (mut pool ConnectionPool) close() { + for _ in 0 .. pool.connections.len { + mut conn := <-pool.connections or { break } + conn.close() or { panic('Failed to close connection: ${err}') } + } +}