Connection pooling with GridDB

14 views Asked by At

I am integrating GridDB into a high-traffic web application to manage real-time data from IoT devices. The application is expected to handle thousands of concurrent requests. Given the scale of concurrency, I am exploring efficient connection pooling strategies.

What are the recommended practices for implementing connection pooling with GridDB, particularly in high-concurrency scenarios?

from griddb_python import griddb
from queue import Queue
import threading

class GridDBConnectionPool:
    def __init__(self, maxsize, **connection_args):
        self._connection_args = connection_args
        self._pool = Queue(maxsize)
        for _ in range(maxsize):
            self._pool.put(self._create_connection())

    def _create_connection(self):
        store = griddb.StoreFactory.get_instance().get_store(**self._connection_args)
        return store

    def get_connection(self):
        return self._pool.get(block=True)

    def return_connection(self, connection):
        self._pool.put(connection)

    def close_all(self):
        while not self._pool.empty():
            connection = self._pool.get(block=False)
            connection.close()

# Example usage
if __name__ == "__main__":
    pool = GridDBConnectionPool(
        maxsize=10,
        host='localhost',
        port=10001,
        cluster_name='defaultCluster',
        username='admin',
        password='admin'
    )

    # Example of borrowing and returning a connection
    conn = pool.get_connection()
    # Perform operations with conn
    pool.return_connection(conn)

I am looking for ways to manage the pool size dynamically based on load, to optimize resource usage without overburdening the GridDB cluster.

0

There are 0 answers