Timeout Configuration

Robyn supports comprehensive timeout configuration to handle high-concurrency scenarios and prevent resource exhaustion like the "Too many open files" error.

Configuration Options

Method Parameters

Configure timeouts directly in the app.start() method:

from robyn import Robyn

app = Robyn(__file__)

@app.get("/")
async def hello(request):
    return "Hello, world!"

# Configure timeout settings
app.start(
    host="0.0.0.0", 
    port=8080,
    client_timeout=30,          # Client connection timeout (seconds)
    keep_alive_timeout=20       # Keep-alive timeout (seconds)
)

Environment Variables

Override configuration using environment variables:

# Set timeout configurations
export ROBYN_CLIENT_TIMEOUT=45
export ROBYN_KEEP_ALIVE_TIMEOUT=30

# Start your application
python app.py

Configuration Parameters

ParameterDefaultDescriptionEnvironment Variable
client_timeout30Maximum time (seconds) for client request processingROBYN_CLIENT_TIMEOUT
keep_alive_timeout20Time (seconds) to keep idle connections aliveROBYN_KEEP_ALIVE_TIMEOUT

Usage Examples

Basic Configuration

# Minimal timeout configuration
app.start(client_timeout=30)

High-Traffic Production Setup

# Optimized for high-traffic scenarios
app.start(
    host="0.0.0.0",
    port=8080,
    client_timeout=60,      # Allow longer processing time
    keep_alive_timeout=15   # Shorter keep-alive for faster turnover
)

Development Setup

# Development-friendly settings
app.start(
    client_timeout=300,     # Long timeout for debugging
    keep_alive_timeout=60   # Longer keep-alive for testing
)

Load Testing Configuration

# Optimized for load testing with tools like wrk
app.start(
    client_timeout=10,      # Quick timeouts
    keep_alive_timeout=5    # Fast connection turnover
)

Environment Variable Priority

Environment variables take precedence over method parameters:

# This will use ROBYN_CLIENT_TIMEOUT=60 if set, otherwise 30
app.start(client_timeout=30)

Troubleshooting

"Too Many Open Files" Error

If you encounter file descriptor exhaustion:

  1. Increase system limits:

    ulimit -n 65536
    
  2. Optimize timeout settings:

    app.start(
        client_timeout=15,      # Shorter timeouts
        keep_alive_timeout=5    # Faster connection cleanup
    )
    
  3. Use environment variables for deployment:

    export ROBYN_CLIENT_TIMEOUT=15
    export ROBYN_KEEP_ALIVE_TIMEOUT=5
    

Performance Tuning

For high-throughput APIs:

  • Lower keep_alive_timeout (5-15s)
  • Moderate client_timeout (15-30s)

For long-running operations:

  • Higher client_timeout (60-300s)
  • Standard keep_alive_timeout (20-30s)

Best Practices

  1. Always set explicit timeouts in production
  2. Use environment variables for deployment-specific configuration
  3. Test timeout settings with realistic load patterns
  4. Start with conservative values and tune based on metrics

Migration Guide

From Previous Versions

If upgrading from earlier Robyn versions, the default behavior changes:

Before (infinite timeout):

# Previously: no timeout (could cause resource exhaustion)
app.start(host="0.0.0.0", port=8080)

After (sensible defaults):

# Now: automatic 30s client timeout, 20s keep-alive
app.start(host="0.0.0.0", port=8080)
# Equivalent to:
app.start(
    host="0.0.0.0", 
    port=8080,
    client_timeout=30,
    keep_alive_timeout=20
)