Skip to main content

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:
  1. Performance: Reuses existing connections instead of creating new ones for each request.
  2. Reliability: Handles connection errors and automatically retries failed operations.
  3. Resource Management: Limits the number of concurrent connections to prevent overwhelming the ShotGrid server.
  4. 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 the server.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:
The 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

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:
  1. Connection Errors: If a connection fails, the pool will retry with a different connection.
  2. Timeout Errors: If a request times out, the pool will retry the operation.
  3. Authentication Errors: If authentication fails, the pool will attempt to re-authenticate.
For custom error handling, you can catch specific exceptions:

Monitoring

The Connection Pool provides methods to monitor its status:

Best Practices

  1. Use Async Context Managers: When manually acquiring connections, always use the async context manager (async with) to ensure proper release.
  2. Batch Operations: Use ShotGrid’s batch API for multiple operations to reduce the number of API calls.
  3. Limit Connection Time: Don’t hold onto connections for long periods. Get what you need and release the connection.
  4. Handle Errors Gracefully: Catch and handle specific ShotGrid errors to provide better feedback to users.
  5. 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: