Configuration
Connection Pooling

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

OptionDefaultDescription
max_connections100Maximum total connections in the pool
max_keepalive_connections20Maximum persistent (keep-alive) connections
keepalive_expiry5.0Seconds 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

  1. First Request: Creates a new connection
  2. Connection Reuse: Subsequent requests to the same host reuse the connection
  3. Keep-Alive: Connections stay open for keepalive_expiry seconds after use
  4. Pool Limits: New requests wait if max_connections is reached
  5. 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

  1. Reuse Clients: Create one client and reuse it across requests
  2. Close When Done: Always close the client to release connections
  3. 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