Skip to content

feat: Redis connection pool with worker affinity #136

@FumingPower3925

Description

@FumingPower3925

Summary

Implement a connection pool for the Redis driver with the same worker-affinity model as the PostgreSQL driver. Redis connections are routed to the same engine worker as the HTTP connection.

Design

Package: driver/redis/pool.go

Pool Architecture

Same design as PostgreSQL pool:

type Pool struct {
    workers    []*workerPool   // one per engine worker
    standalone *standalonePool // fallback when no engine sharing
    overflow   *sharedPool     // shared pool for contention overflow
    pubsubPool *pubsubPool     // dedicated pool for Pub/Sub connections
}

Worker Affinity

Same flow as PostgreSQL:

  1. HTTP request on worker N → pool.Get(ctx) checks workers[N] first
  2. Fallback to overflow pool
  3. Create new connection on worker N's event loop if needed
  4. Release() returns to workers[N]

Redis-Specific Considerations

  • AUTH + SELECT on connect: new connections authenticated and database selected before entering pool
  • Pub/Sub connections: separate pool — Pub/Sub connections are in push mode and cannot be reused for commands
  • MULTI/EXEC transactions: connection held exclusively during transaction (same as PG)
  • Pipeline connections: can use pooled connections — pipeline commands are atomic from Redis's perspective

Configuration

type PoolConfig struct {
    MaxOpen      int           // max command connections (default: NumWorkers * 4)
    MaxIdle      int           // max idle per worker (default: 2)
    MaxLifetime  time.Duration // max connection lifetime (default: 30m)
    MaxIdleTime  time.Duration // max idle time (default: 5m)
    MaxPubSub    int           // max Pub/Sub connections (default: NumWorkers)
}

Modes

client, err := redis.NewClient("localhost:6379")                          // standalone
client, err := redis.NewClient("localhost:6379", redis.WithEngine(srv))   // integrated

Acceptance Criteria

  • Per-worker pools with worker affinity
  • Overflow to shared pool
  • Separate Pub/Sub connection pool
  • AUTH + SELECT on new connections
  • Standalone mode with mini event loop
  • Integrated mode sharing HTTP server's event loop
  • Connection health check (PING)
  • MaxOpen, MaxIdle, MaxLifetime, MaxIdleTime enforced
  • Benchmark: pool Get/Release throughput

Dependencies

  • Depends on 133 (Redis client — pool manages Redis connections)
  • Depends on 113 (EventLoopProvider — for integrated mode)

Metadata

Metadata

Labels

area/driverDatabase/cache driver infrastructuredriver/redisRedis driver

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions