generic connection pool
Project description
generic-connection-pool is a connection pool that can be used for TCP, http, database connections.
Features
generic nature: can be used for any connection you desire (TCP, http, database)
runtime agnostic: synchronous and asynchronous pool supported
flexibility: flexable connection retention and recycling policy
fully-typed: mypy type-checker compatible
Getting started
Connection pool supports the following configurations:
background_collector: if True starts a background worker that disposes expired and idle connections maintaining requested pool state. If False the connections will be disposed on each connection release.
dispose_batch_size: maximum number of expired and idle connections to be disposed on connection release (if background collector is started the parameter is ignored).
idle_timeout: inactivity time (in seconds) after which an extra connection will be disposed (a connection considered as extra if the number of endpoint connection exceeds min_idle).
max_lifetime: number of seconds after which any connection will be disposed.
min_idle: minimum number of connections in each endpoint the pool tries to hold. Connections that exceed that number will be considered as extra and disposed after idle_timeout seconds of inactivity.
max_size: maximum number of endpoint connections.
total_max_size: maximum number of all connections in the pool.
The following example illustrates how to create https pool:
import socket
import ssl
import urllib.parse
from http.client import HTTPResponse
from typing import Tuple
from generic_connection_pool.contrib.socket import SslSocketConnectionManager
from generic_connection_pool.threading import ConnectionPool
Hostname = str
Port = int
Endpoint = Tuple[Hostname, Port]
Connection = socket.socket
http_pool = ConnectionPool[Endpoint, Connection](
SslSocketConnectionManager(ssl.create_default_context()),
idle_timeout=30.0,
max_lifetime=600.0,
min_idle=3,
max_size=20,
total_max_size=100,
background_collector=True,
)
def fetch(url: str, timeout: float = 5.0) -> None:
url = urllib.parse.urlsplit(url)
port = url.port or 443 if url.scheme == 'https' else 80
with http_pool.connection(endpoint=(url.hostname, port), timeout=timeout) as sock:
request = (
'GET {path} HTTP/1.1\r\n'
'Host: {host}\r\n'
'\r\n'
'\r\n'
).format(host=url.hostname, path=url.path)
sock.write(request.encode())
response = HTTPResponse(sock)
response.begin()
status, body = response.getcode(), response.read(response.length)
print(status)
print(body)
try:
fetch('https://en.wikipedia.org/wiki/HTTP') # http connection is opened
fetch('https://en.wikipedia.org/wiki/Python_(programming_language)') # http connection is reused
finally:
http_pool.close()
See documentation for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for generic_connection_pool-0.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34bbb64de706a5f404f05c12586197934edfd91ae0f3dc2b303f5aff527ad3c4 |
|
MD5 | 59869c8b80064249aeded3ad422226b0 |
|
BLAKE2b-256 | f4e27260938024104d6cccb4bd9303ad8e2716ec1df80d2af28b159d9091fc0e |
Hashes for generic_connection_pool-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 463acef11f4ee762616f5c2c5e4f8834c8856917ad07a0d6ee903b7aec48bba7 |
|
MD5 | f627ad5b7108f9e11ab71f4b85d9d15c |
|
BLAKE2b-256 | 101a2cf094fe6369a4218e8b6a4c346c8aac3eccfac18457171af7b7dd014853 |