Skip to content

feat: Try to generalize and refactor connection pool from workflows#692

Closed
ad-claw000 wants to merge 1 commit into
developfrom
fix/issue-598
Closed

feat: Try to generalize and refactor connection pool from workflows#692
ad-claw000 wants to merge 1 commit into
developfrom
fix/issue-598

Conversation

@ad-claw000
Copy link
Copy Markdown
Contributor

Fixes #598

Copilot AI review requested due to automatic review settings May 19, 2026 20:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new ConnectionPool utility into the aperturedb Python SDK (ported/refactored from the workflows repo) and adds unit tests to validate basic pooling behavior.

Changes:

  • Added aperturedb/ConnectionPool.py implementing a queue-backed, context-manager-based connection pool.
  • Added test/test_ConnectionPool.py with unit tests for initialization, checkout/return, and query forwarding.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
aperturedb/ConnectionPool.py Adds the new ConnectionPool implementation and a convenience query() method.
test/test_ConnectionPool.py Adds tests covering pool sizing, get_connection() context management, and query() forwarding.
Comments suppressed due to low confidence (2)

aperturedb/ConnectionPool.py:83

  • query(self, query: str, blobs: list = [], ...) uses a mutable default ([]). If callers mutate blobs, that state can leak across calls. Prefer blobs: Optional[list] = None and normalize to [] inside the method.
    def query(self, query: str, blobs: list = [], **kwargs):
        """

aperturedb/ConnectionPool.py:60

  • The pool swallows connection creation errors (prints and continues), but _pool_size remains the requested size and total() reports that number even if fewer connections were actually created. Either fail fast when the pool cannot be fully populated, or track the actual number of created connections and have total() reflect reality (and/or expose both requested vs created).
        # Pre-populate the pool with connections
        for _ in range(pool_size):
            try:
                connection = self._connection_factory()
                if not connection:
                    raise ConnectionError("Failed to create a connection.")
                self._pool.put(connection)
            except Exception as e:
                print(f"Failed to create a connection for the pool: {e}")
                # Depending on requirements, you might want to raise an error
                # if the pool cannot be fully populated.

        if self.available() == 0:
            raise ConnectionError(
                "Failed to initialize any connections for the pool. "
                "Please check connection parameters and network."
            )

    def available(self) -> int:
        """Returns the number of available connections in the pool."""
        return self._pool.qsize()

    def total(self) -> int:
        """Returns the total number of connections in the pool."""
        return self._pool_size


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +82 to +99
def query(self, query: str, blobs: list = [], **kwargs):
"""
A convenience method to execute a query directly from the pool.

This method handles getting a connection, executing the query,
and returning the connection to the pool.

Args:
query (str): The query string to execute.
blobs (list): A list of blobs to include with the query.
**kwargs: Other arguments for the Connector's query method.

Returns:
Response from the executed query.
Blobs
"""
with self.get_connection() as connection:
return connection.query(query, blobs, **kwargs)
Comment on lines +32 to +34
# A lock to ensure the initial population is thread-safe, just in case.
self._lock = threading.Lock()

Comment on lines +39 to +43
response, blobs = pool.query("some query", [1, 2, 3], arg=True)

assert response == "response"
assert blobs == []
mock_connection.query.assert_called_once_with("some query", [1, 2, 3], arg=True)
@@ -0,0 +1,43 @@
import pytest
@luisremis
Copy link
Copy Markdown
Contributor

there is already a pr for this #666

@luisremis luisremis closed this May 19, 2026
@luisremis luisremis deleted the fix/issue-598 branch May 19, 2026 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Try to generalize and refactor connection pool from workflows.

3 participants