Add pool management.

This commit is contained in:
2025-11-04 00:32:53 +00:00
parent 71a14721c5
commit 97cf896a17
6 changed files with 500 additions and 27 deletions
+61
View File
@@ -43,6 +43,10 @@ Therefore, I present you the simple client to which you can copy & paste your gr
* `GRAPHQL_RETRIES_NUMBER` - Number of retries: Default: `3`
* `GRAPHQL_RETRIES_DELAY` - Delay in retries in milliseconds. Default: `250`
* `GRAPHQL_RETRIES_PATTERNS` - Comma-separated error patterns that trigger retries. Default: `postgres,connection,timeout,transaction,could not,temporarily unavailable,deadlock`
* `GRAPHQL_POOL_WARMUP_ENABLED` - Enable intelligent connection pool warmup. Default: `false`
* `GRAPHQL_POOL_SIZE` - Number of connections to pre-create and maintain. Default: `5`
* `GRAPHQL_POOL_WARMUP_QUERY` - Query used for warming up connections. Default: `query{__typename}`
* `GRAPHQL_POOL_HEALTH_INTERVAL` - Interval in seconds for pool health checks. Default: `30`
### Modifiers on the fly
@@ -85,6 +89,63 @@ export GRAPHQL_RETRIES_DELAY=250
This will retry GraphQL queries up to 3 times when postgres transaction errors occur, with exponential backoff starting at 250ms.
### Intelligent Connection Pooling
The library supports intelligent connection pooling with pre-warmed connections and automatic health monitoring. This significantly improves performance during traffic spikes by keeping hot standby connections ready.
**How it works:**
- **Pre-warming**: Connections are created and warmed up during initialization
- **Health monitoring**: Background process periodically checks connection health
- **Auto-refresh**: Automatically replaces stale or broken connections
- **Hot standbys**: Ready connections eliminate cold-start latency
**Configuration:**
Enable connection pool warmup for production environments with variable traffic:
```bash
export GRAPHQL_POOL_WARMUP_ENABLED=true
export GRAPHQL_POOL_SIZE=5 # Number of hot standby connections
export GRAPHQL_POOL_HEALTH_INTERVAL=30 # Health check every 30 seconds
export GRAPHQL_POOL_WARMUP_QUERY="query{__typename}" # Lightweight health check query
```
**Benefits:**
- ⚡ **Instant response** - Connections already established during traffic spikes
- 🔄 **Auto-healing** - Detects and replaces broken connections automatically
- 📊 **Predictable performance** - Eliminates connection establishment latency
- 🛡️ **Resilience** - Maintains pool health even during network instability
**Recommended settings by use case:**
```bash
# High-traffic API (e.g., production web app)
GRAPHQL_POOL_WARMUP_ENABLED=true
GRAPHQL_POOL_SIZE=10
GRAPHQL_POOL_HEALTH_INTERVAL=15
# Medium-traffic service (e.g., background jobs)
GRAPHQL_POOL_WARMUP_ENABLED=true
GRAPHQL_POOL_SIZE=5
GRAPHQL_POOL_HEALTH_INTERVAL=30
# Low-traffic/development
GRAPHQL_POOL_WARMUP_ENABLED=false # Default: on-demand connections
```
**Graceful shutdown:**
When shutting down your application, stop the pool monitor gracefully:
```go
func main() {
gql := graphql.NewConnection()
defer gql.StopPoolMonitor() // Ensure clean shutdown
// Your application logic
}
```
### Cache
You have two options to enable the cache: