Connection Pool
The Connection Pool is a key component of ShotGrid MCP Server that efficiently manages connections to the ShotGrid API. It provides connection reuse, error handling, and automatic retries to ensure reliable communication with ShotGrid.Why Use a Connection Pool?
ShotGrid API connections are relatively expensive to establish and maintain. The Connection Pool provides several benefits:- Performance: Reuses existing connections instead of creating new ones for each request.
- Reliability: Handles connection errors and automatically retries failed operations.
- Resource Management: Limits the number of concurrent connections to prevent overwhelming the ShotGrid server.
- Monitoring: Tracks connection usage and performance metrics.
How It Works
The Connection Pool maintains a pool of ShotGrid API connections that can be borrowed, used, and returned:Using the Connection Pool
Automatic Usage
When you use theserver.connection
property or create tools with the @server.tool()
decorator, the Connection Pool is used automatically:
Manual Usage
For more control, you can manually acquire and release connections:async with
statement ensures the connection is properly returned to the pool, even if an exception occurs.
Configuration
You can configure the Connection Pool when creating the ShotGrid MCP Server:Configuration Options
Parameter | Type | Default | Description |
---|---|---|---|
max_connections | int | 10 | Maximum number of connections in the pool |
connection_timeout | int | 30 | Timeout for ShotGrid API calls (seconds) |
max_retries | int | 3 | Maximum number of retries for failed operations |
retry_delay | float | 1.0 | Delay between retries (seconds) |
health_check_interval | int | 60 | Interval for connection health checks (seconds) |
Advanced Usage
Connection Context
The Connection Pool provides a context manager for acquiring and releasing connections:Error Handling
The Connection Pool automatically handles many common ShotGrid API errors:- Connection Errors: If a connection fails, the pool will retry with a different connection.
- Timeout Errors: If a request times out, the pool will retry the operation.
- Authentication Errors: If authentication fails, the pool will attempt to re-authenticate.
Monitoring
The Connection Pool provides methods to monitor its status:Best Practices
-
Use Async Context Managers: When manually acquiring connections, always use the async context manager (
async with
) to ensure proper release. - Batch Operations: Use ShotGrid’s batch API for multiple operations to reduce the number of API calls.
- Limit Connection Time: Don’t hold onto connections for long periods. Get what you need and release the connection.
- Handle Errors Gracefully: Catch and handle specific ShotGrid errors to provide better feedback to users.
- Monitor Pool Health: In production, monitor the connection pool statistics to ensure it’s functioning properly.
Next Steps
Now that you understand the Connection Pool, you can:- Learn about optimized queries to make the most of your connections
- Explore batch operations for efficient data manipulation
- See how to handle errors gracefully