Connection Pooling
Connection pooling improves performance by reusing HTTP connections across requests.
Default Configuration
The client uses sensible defaults for connection pooling:
from intapp_rest_client import RestClient
# Uses default pool settings
client = RestClient(
base_url="https://api.example.com",
api_key="your-key"
)Custom Configuration
from intapp_rest_client import RestClient, ConnectionPoolConfig
client = RestClient(
base_url="https://api.example.com",
api_key="your-key",
pool_config=ConnectionPoolConfig(
max_connections=100, # Total connections
max_keepalive_connections=20, # Persistent connections
keepalive_expiry=5.0 # Idle timeout (seconds)
)
)ConnectionPoolConfig Options
| Option | Default | Description |
|---|---|---|
max_connections | 100 | Maximum total connections in the pool |
max_keepalive_connections | 20 | Maximum persistent (keep-alive) connections |
keepalive_expiry | 5.0 | Seconds before closing idle connections |
When to Tune
High-Throughput Applications
If you're making many concurrent requests:
pool_config = ConnectionPoolConfig(
max_connections=200, # More total connections
max_keepalive_connections=50, # More persistent connections
keepalive_expiry=30.0 # Keep connections longer
)Memory-Constrained Environments
For serverless or containers with limited memory:
pool_config = ConnectionPoolConfig(
max_connections=20, # Fewer connections
max_keepalive_connections=5, # Minimal persistent
keepalive_expiry=2.0 # Quick cleanup
)Long-Running Services
For services that maintain connections over time:
pool_config = ConnectionPoolConfig(
max_connections=50,
max_keepalive_connections=20,
keepalive_expiry=60.0 # Keep connections alive longer
)How It Works
- First Request: Creates a new connection
- Connection Reuse: Subsequent requests to the same host reuse the connection
- Keep-Alive: Connections stay open for
keepalive_expiryseconds after use - Pool Limits: New requests wait if
max_connectionsis reached - Cleanup: Idle connections are closed after
keepalive_expiry
💡
Connection pooling is automatic. The client handles all connection lifecycle management.
Monitoring Connections
The underlying httpx client manages the pool. For debugging:
# Access the internal client (sync)
client._sync_client
# Check if client exists (lazy initialized)
if client._sync_client:
print("Sync client active")Best Practices
- Reuse Clients: Create one client and reuse it across requests
- Close When Done: Always close the client to release connections
- Use Context Managers: Ensures proper cleanup
# ✅ Good: Reuse client
client = RestClient(base_url="https://api.example.com", api_key="key")
for item in items:
client.post("/api/items", json=item)
client.close()
# ❌ Bad: New client per request
for item in items:
client = RestClient(base_url="https://api.example.com", api_key="key")
client.post("/api/items", json=item)
client.close()Next Steps
- Retry Configuration - Handle transient failures
- Logging - Debug connection issues
- Async Operations - Async connection pooling