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
Parameter | Default | Description | Environment Variable |
---|---|---|---|
client_timeout | 30 | Maximum time (seconds) for client request processing | ROBYN_CLIENT_TIMEOUT |
keep_alive_timeout | 20 | Time (seconds) to keep idle connections alive | ROBYN_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:
-
Increase system limits:
ulimit -n 65536
Copy -
Optimize timeout settings:
app.start( client_timeout=15, # Shorter timeouts keep_alive_timeout=5 # Faster connection cleanup )
Copy -
Use environment variables for deployment:
export ROBYN_CLIENT_TIMEOUT=15 export ROBYN_KEEP_ALIVE_TIMEOUT=5
Copy
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
- Always set explicit timeouts in production
- Use environment variables for deployment-specific configuration
- Test timeout settings with realistic load patterns
- 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
)