VOOZH about

URL: https://deepwiki.com/hypervel/redis/6.2-connection-pooling

⇱ Connection Pooling | hypervel/redis | DeepWiki


Loading...
Menu

Connection Pooling

Purpose and Scope

This page documents the RedisPool class, which provides custom connection pooling functionality for the Hypervel Redis library. The pool extends Hyperf's native Redis pool to create RedisConnection instances with Laravel-style API transformations instead of Hyperf's default connections.

For information about how pools are created and managed by the factory, see Redis Factory. For details about the connection class itself and its transformation capabilities, see Laravel-Style Redis Connection.

Sources: src/RedisPool.php1-16


Overview

The RedisPool class serves as a critical integration point between Hyperf's connection pooling system and Hypervel's Laravel-style Redis API. By extending Hyperf\Redis\Pool\RedisPool and overriding the connection creation method, it ensures that all connections retrieved from the pool are instances of RedisConnection with transformation capabilities enabled.

System Position


Diagram: RedisPool in System Architecture

The diagram illustrates how RedisPool intercepts the default connection creation process. While Hyperf's native pool would create standard Hyperf\Redis\RedisConnection instances, the custom pool creates Hypervel\Redis\RedisConnection instances that provide Laravel-style API transformations.

Sources: src/RedisPool.php1-16 src/RedisConnection.php260-261


RedisPool Class Implementation

The RedisPool class is remarkably minimal, consisting of only a single method override. This simplicity is by design—it leverages all of Hyperf's existing pooling infrastructure while changing only the connection creation behavior.

Class Definition

AspectDetails
NamespaceHypervel\Redis
Parent ClassHyperf\Redis\Pool\RedisPool
MethodscreateConnection() (override)
PropertiesInherits all from parent

Key Method: createConnection()


Diagram: Connection Creation Flow

The createConnection() method src/RedisPool.php12-15 is invoked by Hyperf's pooling system whenever a new connection needs to be instantiated. It receives three parameters from the parent pool infrastructure:

ParameterTypeSourcePurpose
$this->containerContainerInterfaceDependency injection containerProvides access to framework services
$this->poolRedisPoolSelf-referenceAllows connection to release itself back to pool
$this->configPoolConfigPool configurationContains Redis server settings, pool size, etc.

Sources: src/RedisPool.php12-15 src/RedisConnection.php260


Connection Creation Process

The connection creation process follows a specific sequence that integrates with Hyperf's connection pool lifecycle.

Creation Sequence


Diagram: Connection Instantiation Sequence

Connection State Initialization

When a RedisConnection is created, it initializes with the following default state:

PropertyInitial ValuePurpose
shouldTransformfalseTransformation disabled by default src/RedisConnection.php265
connectionphpredis instanceNative Redis connection (inherited)
poolRedisPool referenceUsed for releasing connection back to pool

The shouldTransform flag is set to false initially and gets enabled later by the Redis class when it retrieves a connection for Laravel-style operations.

Sources: src/RedisPool.php12-15 src/RedisConnection.php260-265


Connection Lifecycle in Pool

The pool manages connections through a borrow-and-return lifecycle, ensuring efficient resource utilization in a coroutine environment.

Borrow and Return Flow


Diagram: Connection State Machine

Key Lifecycle Methods

The connection lifecycle involves several methods that interact with the pool:

MethodLocationCalled ByPurpose
get()Inherited from HyperfRedisPoolRedis classBorrow connection from pool
release()RedisConnection src/RedisConnection.php289-294Redis class or connection itselfReturn connection to pool
shouldTransform()RedisConnection src/RedisConnection.php299-304Redis classEnable/disable transformations

Release Process

When a connection is released back to the pool src/RedisConnection.php289-294 it performs cleanup:

  1. Reset transformation flag: Sets shouldTransform = false to ensure the next borrower gets a clean state
  2. Call parent release: Invokes parent::release() to return the connection to Hyperf's pool

This two-step process ensures that transformation state doesn't leak between different uses of the same connection instance.

Sources: src/RedisConnection.php289-294 src/RedisConnection.php299-304


Multiple Pool Architecture

The system supports multiple named Redis pools, each managed by its own RedisPool instance. This enables applications to maintain separate connection pools for different purposes (e.g., caching, sessions, primary database).

Multi-Pool Structure


Diagram: Multiple Pool Configuration

Pool Configuration

Each pool is configured independently in the config/autoload/redis.php configuration file. The configuration structure looks like:


Pool Instantiation

The dependency injection container creates separate RedisPool instances for each configured pool name. The pool name is used as a key in the container, allowing the RedisFactory to retrieve the appropriate pool for a given connection request.

Sources: src/RedisPool.php1-16


Integration with Hyperf Framework

The RedisPool class integrates seamlessly with Hyperf's connection pooling infrastructure by leveraging the framework's built-in mechanisms.

Integration Points


Diagram: Framework Integration Points

Configuration Provider Override

The ConfigProvider class registers the custom RedisPool as a replacement for Hyperf's default pool through the dependencies configuration array. This override ensures that whenever the framework needs to create a Redis pool, it uses Hypervel\Redis\RedisPool instead of Hyperf\Redis\Pool\RedisPool.

Inherited Pooling Features

By extending HyperfRedisPool, the custom pool inherits all standard pooling features:

FeatureSourceDescription
Min/Max connectionsHyperf pool configConfigurable pool size bounds
Wait timeoutHyperf pool configMax time to wait for available connection
Connection validationHyperfRedisPoolAutomatic connection health checks
Coroutine safetyHyperf pooling systemThread-safe connection management
Connection recyclingHyperf pooling systemAutomatic connection lifecycle management
Lazy initializationHyperf pooling systemConnections created on-demand

The only aspect that changes is the type of connection created—everything else remains consistent with Hyperf's pooling behavior.

Sources: src/RedisPool.php1-16 src/RedisPool.php10


Connection Pool Size and Configuration

The pool size and behavior are controlled through Hyperf's standard pool configuration, which each RedisPool instance reads from its $this->config property.

Configuration Parameters

ParameterTypeDefaultPurpose
min_connectionsint1Minimum connections maintained in pool
max_connectionsint10Maximum connections allowed in pool
connect_timeoutfloat10.0Timeout for establishing new connection
wait_timeoutfloat3.0Max wait time when pool is exhausted
heartbeatint-1Connection heartbeat interval (seconds)
max_idle_timefloat60.0Max idle time before connection is closed

Pool Behavior Under Load


Diagram: Pool Behavior Under Different Load Conditions

When the pool is exhausted (all connections borrowed and max_connections reached), subsequent borrow requests will wait up to wait_timeout seconds for a connection to become available. If no connection becomes available within this time, Hyperf throws a ConnectionException.

Sources: src/RedisPool.php14


Connection Validation and Health Checks

The RedisPool inherits connection validation logic from HyperfRedisPool, which performs automatic health checks to ensure connections remain viable.

Validation Process


Diagram: Connection Validation Flow

Heartbeat Mechanism

If configured, the pool sends periodic heartbeat commands to maintain connection validity:

  • Heartbeat Command: The pool executes a lightweight command (typically PING) to verify the connection is responsive
  • Frequency: Controlled by the heartbeat configuration parameter (in seconds)
  • Purpose: Prevents connections from being closed by intermediate proxies or firewalls due to inactivity

Sources: src/RedisPool.php10


Summary

The RedisPool class provides a minimal but critical override to Hyperf's connection pooling system:

Key Characteristics

AspectImplementation
Lines of Code7 (excluding namespace and use statements)
ComplexityMinimal—single method override
PurposeCreate RedisConnection instances instead of default connections
IntegrationSeamless with Hyperf's pooling infrastructure
InheritanceAll pooling features from HyperfRedisPool

Design Philosophy

The design follows the principle of minimal intervention: change only what's necessary (connection type) while preserving all of Hyperf's robust pooling capabilities. This approach ensures:

  • Reliability: Leverages battle-tested pooling logic from Hyperf
  • Maintainability: Minimal custom code reduces maintenance burden
  • Compatibility: Works with all Hyperf pooling features and configurations
  • Flexibility: Supports multiple pools with different configurations

Sources: src/RedisPool.php1-16