{"id":15579007,"url":"https://github.com/dapper91/generic-connection-pool","last_synced_at":"2025-09-05T18:39:44.878Z","repository":{"id":96973327,"uuid":"608205463","full_name":"dapper91/generic-connection-pool","owner":"dapper91","description":"python generic connection pool","archived":false,"fork":false,"pushed_at":"2023-12-09T09:01:30.000Z","size":133,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T05:41:09.941Z","etag":null,"topics":["asyncio","connection-pool","db","generic-pool","pool","python","socket","tcp"],"latest_commit_sha":null,"homepage":"https://generic-connection-pool.readthedocs.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dapper91.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-01T14:38:45.000Z","updated_at":"2024-09-08T08:30:28.000Z","dependencies_parsed_at":"2025-03-07T04:41:13.717Z","dependency_job_id":null,"html_url":"https://github.com/dapper91/generic-connection-pool","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dapper91%2Fgeneric-connection-pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dapper91%2Fgeneric-connection-pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dapper91%2Fgeneric-connection-pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dapper91%2Fgeneric-connection-pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dapper91","download_url":"https://codeload.github.com/dapper91/generic-connection-pool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250543269,"owners_count":21447864,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asyncio","connection-pool","db","generic-pool","pool","python","socket","tcp"],"created_at":"2024-10-02T19:13:35.178Z","updated_at":"2025-04-24T01:27:56.798Z","avatar_url":"https://github.com/dapper91.png","language":"Python","readme":"=======================\ngeneric-connection-pool\n=======================\n\n.. image:: https://static.pepy.tech/personalized-badge/generic-connection-pool?period=month\u0026units=international_system\u0026left_color=grey\u0026right_color=orange\u0026left_text=Downloads/month\n    :target: https://pepy.tech/project/generic-connection-pool\n    :alt: Downloads/month\n.. image:: https://github.com/dapper91/generic-connection-pool/actions/workflows/test.yml/badge.svg?branch=master\n    :target: https://github.com/dapper91/generic-connection-pool/actions/workflows/test.yml\n    :alt: Build status\n.. image:: https://img.shields.io/pypi/l/generic-connection-pool.svg\n    :target: https://pypi.org/project/generic-connection-pool\n    :alt: License\n.. image:: https://img.shields.io/pypi/pyversions/generic-connection-pool.svg\n    :target: https://pypi.org/project/generic-connection-pool\n    :alt: Supported Python versions\n.. image:: https://codecov.io/gh/dapper91/generic-connection-pool/branch/master/graph/badge.svg\n    :target: https://codecov.io/gh/dapper91/generic-connection-pool\n    :alt: Code coverage\n.. image:: https://readthedocs.org/projects/generic-connection-pool/badge/?version=stable\u0026style=flat\n   :alt: ReadTheDocs status\n   :target: https://generic-connection-pool.readthedocs.io/en/stable/\n\n\n``generic-connection-pool`` is an extensible connection pool agnostic to the connection type it is managing.\nIt can be used for TCP, http, database or ssh connections.\n\nFeatures\n--------\n\n- **generic nature**: can be used for any connection you desire (TCP, http, database, ssh, etc.)\n- **runtime agnostic**: synchronous and asynchronous runtime supported\n- **flexibility**: flexable connection retention and recycling policy\n- **fully-typed**: mypy type-checker compatible\n\n\nGetting started\n---------------\n\nConnection pool supports the following configurations:\n\n* **background_collector**: if ``True`` starts a background worker that disposes expired and idle connections\n  maintaining requested pool state. If ``False`` the connections will be disposed on each connection release.\n* **dispose_batch_size**: maximum number of expired and idle connections to be disposed on connection release\n  (if background collector is started the parameter is ignored).\n* **idle_timeout**: inactivity time (in seconds) after which an extra connection will be disposed\n  (a connection considered as extra if the number of endpoint connection exceeds ``min_idle``).\n* **max_lifetime**: number of seconds after which any connection will be disposed.\n* **min_idle**: minimum number of connections in each endpoint the pool tries to hold. Connections that exceed\n  that number will be considered as extra and disposed after ``idle_timeout`` seconds of inactivity.\n* **max_size**: maximum number of endpoint connections.\n* **total_max_size**: maximum number of all connections in the pool.\n\n\n.. image:: /static/connection-pool.svg\n  :width: 1024\n  :alt: Connection Pool parameters\n\n\nThe following example illustrates how to create https pool:\n\n.. code-block:: python\n\n    import socket\n    import ssl\n    import urllib.parse\n    from http.client import HTTPResponse\n    from typing import Tuple\n\n    from generic_connection_pool.contrib.socket import SslSocketConnectionManager\n    from generic_connection_pool.threading import ConnectionPool\n\n    Hostname = str\n    Port = int\n    Endpoint = Tuple[Hostname, Port]\n    Connection = socket.socket\n\n\n    http_pool = ConnectionPool[Endpoint, Connection](\n        SslSocketConnectionManager(ssl.create_default_context()),\n        idle_timeout=30.0,\n        max_lifetime=600.0,\n        min_idle=3,\n        max_size=20,\n        total_max_size=100,\n        background_collector=True,\n    )\n\n\n    def fetch(url: str, timeout: float = 5.0) -\u003e None:\n        url = urllib.parse.urlsplit(url)\n        port = url.port or 443 if url.scheme == 'https' else 80\n\n        with http_pool.connection(endpoint=(url.hostname, port), timeout=timeout) as sock:\n            request = (\n                'GET {path} HTTP/1.1\\r\\n'\n                'Host: {host}\\r\\n'\n                '\\r\\n'\n                '\\r\\n'\n            ).format(host=url.hostname, path=url.path)\n\n            sock.write(request.encode())\n\n            response = HTTPResponse(sock)\n            response.begin()\n            status, body = response.getcode(), response.read(response.length)\n\n            print(status)\n            print(body)\n\n\n    try:\n        fetch('https://en.wikipedia.org/wiki/HTTP')  # http connection opened\n        fetch('https://en.wikipedia.org/wiki/Python_(programming_language)')  # http connection reused\n    finally:\n        http_pool.close()\n\n... or database one\n\n.. code-block:: python\n\n    import psycopg2.extensions\n\n    from generic_connection_pool.contrib.psycopg2 import DbConnectionManager\n    from generic_connection_pool.threading import ConnectionPool\n\n    Endpoint = str\n    Connection = psycopg2.extensions.connection\n\n\n    dsn_params = dict(dbname='postgres', user='postgres', password='secret')\n\n    pg_pool = ConnectionPool[Endpoint, Connection](\n        DbConnectionManager(\n            dsn_params={\n                'master': dict(dsn_params, host='db-master.local'),\n                'replica-1': dict(dsn_params, host='db-replica-1.local'),\n                'replica-2': dict(dsn_params, host='db-replica-2.local'),\n            },\n        ),\n        acquire_timeout=2.0,\n        idle_timeout=60.0,\n        max_lifetime=600.0,\n        min_idle=3,\n        max_size=10,\n        total_max_size=15,\n        background_collector=True,\n    )\n\n    try:\n        # connection opened\n        with pg_pool.connection(endpoint='master') as conn:\n            cur = conn.cursor()\n            cur.execute(\"SELECT * FROM pg_stats;\")\n            print(cur.fetchone())\n\n        # connection opened\n        with pg_pool.connection(endpoint='replica-1') as conn:\n            cur = conn.cursor()\n            cur.execute(\"SELECT * FROM pg_stats;\")\n            print(cur.fetchone())\n\n        # connection reused\n        with pg_pool.connection(endpoint='master') as conn:\n            cur = conn.cursor()\n            cur.execute(\"SELECT * FROM pg_stats;\")\n            print(cur.fetchone())\n\n    finally:\n        pg_pool.close()\n\n\nSee `documentation \u003chttps://generic-connection-pool.readthedocs.io/en/latest/\u003e`_ for more details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdapper91%2Fgeneric-connection-pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdapper91%2Fgeneric-connection-pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdapper91%2Fgeneric-connection-pool/lists"}