From 4ecf4a20779f1c32651eaa9ec196202156369353 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 11 Mar 2026 13:08:58 +0300 Subject: [PATCH] db: support and cooperate with pool to implement a connection pool (fixes #26500) --- vlib/db/redis/README.md | 3 +++ vlib/db/redis/redis.v | 14 ++++++++++++++ .../interfaces/redis_connection_poolable_test.v | 13 +++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 vlib/v/tests/interfaces/redis_connection_poolable_test.v diff --git a/vlib/db/redis/README.md b/vlib/db/redis/README.md index 55dc16cd9..0353dd02f 100644 --- a/vlib/db/redis/README.md +++ b/vlib/db/redis/README.md @@ -141,6 +141,9 @@ defer { } ``` +`redis.DB` also implements `pool.ConnectionPoolable`, so it can be used +directly with `vlib/pool` connection pools. + ## Performance Tips 1. **Reuse Connections**: Maintain connections instead of creating new ones diff --git a/vlib/db/redis/redis.v b/vlib/db/redis/redis.v index f28aa6d8c..52fe1f8a6 100644 --- a/vlib/db/redis/redis.v +++ b/vlib/db/redis/redis.v @@ -198,6 +198,20 @@ pub fn (mut db DB) ping() !string { return db.cmd('PING')! as string } +// validate checks whether the Redis connection is still responsive. +pub fn (mut db DB) validate() !bool { + return db.ping()! == 'PONG' +} + +// reset clears any queued pipeline state before the connection is reused. +pub fn (mut db DB) reset() ! { + db.pipeline_mode = false + db.pipeline_cmd_count = 0 + db.cmd_buf.clear() + db.resp_buf.clear() + db.pipeline_buffer.clear() +} + // del deletes a `key` pub fn (mut db DB) del(key string) !i64 { // *2\r\n$3\r\nDEL\r\n$6\r\ncounter\r\n diff --git a/vlib/v/tests/interfaces/redis_connection_poolable_test.v b/vlib/v/tests/interfaces/redis_connection_poolable_test.v new file mode 100644 index 000000000..d9838c7be --- /dev/null +++ b/vlib/v/tests/interfaces/redis_connection_poolable_test.v @@ -0,0 +1,13 @@ +import db.redis +import pool + +fn create_conn() !&pool.ConnectionPoolable { + mut redis_client := redis.DB{} + conn := &redis_client + return conn +} + +fn test_redis_db_implements_connection_poolable() { + mut conn := create_conn()! + conn.reset()! +} -- 2.39.5