11# asyncio-connection-pool
22
3- [ ![ GitHub Workflow Status (main)] ( https://img.shields.io/github/workflow/status/fellowinsights /asyncio-connection-pool/CI/main? style=flat )] [ main CI ]
3+ [ ![ GitHub Workflow Status (main)] ( https://img.shields.io/github/actions/ workflow/status/fellowapp /asyncio-connection-pool/ci.yml?branch=main& style=flat )] [ main CI ]
44[ ![ PyPI] ( https://img.shields.io/pypi/v/asyncio-connection-pool?style=flat )] [ package ]
55[ ![ PyPI - Python Version] ( https://img.shields.io/pypi/pyversions/asyncio-connection-pool?style=flat )] [ package ]
6+ [ ![ codecov] ( https://codecov.io/gh/fellowapp/asyncio-connection-pool/graph/badge.svg?token=F3D4D9EG6M )] ( https://codecov.io/gh/fellowapp/asyncio-connection-pool )
7+ [ ![ License] ( https://img.shields.io/pypi/l/prosemirror.svg?style=flat )] ( https://github.com/fellowapp/asyncio-connection-pool/blob/master/LICENSE.md )
68[ ![ Fellow Careers] ( https://img.shields.io/badge/fellow.app-hiring-576cf7.svg?style=flat )] ( https://fellow.app/careers/ )
79
8- [ main CI ] : https://github.com/fellowinsights /asyncio-connection-pool/actions?query=workflow%3ACI+branch%3Amain
10+ [ main CI ] : https://github.com/fellowapp /asyncio-connection-pool/actions?query=workflow%3ACI+branch%3Amain
911[ package ] : https://pypi.org/project/asyncio-connection-pool/
1012
1113This is a generic, high-throughput, optionally-burstable pool for asyncio.
@@ -21,10 +23,11 @@ Some cool features:
2123- The contents of the pool can be anything; just implement a
2224 ` ConnectionStrategy ` .
2325
24- [ ^ 1 ] : Theoretically, there is an implicit "lock" that is held while an asyncio
25- task is executing. No other task can execute until the current task
26- yields (since it's cooperative multitasking), so any operations during
27- that time are atomic.
26+ [ ^ 1 ] :
27+ Theoretically, there is an implicit "lock" that is held while an asyncio
28+ task is executing. No other task can execute until the current task
29+ yields (since it's cooperative multitasking), so any operations during
30+ that time are atomic.
2831
2932## Why?
3033
@@ -36,10 +39,8 @@ We also thought it would be nice if we didn't need to keep many connections
3639open when they weren't needed, but still have the ability to make more when
3740they are required.
3841
39-
4042## API
4143
42-
4344### ` asyncio_connection_pool.ConnectionPool `
4445
4546This is the implementation of the pool. It is generic over a type of
@@ -57,7 +58,6 @@ pool = ConnectionPool(strategy=my_strategy, max_size=15)
5758The constructor can optionally be passed an integer as ` burst_limit ` . This
5859allows the pool to open more connections than ` max_size ` temporarily.
5960
60-
6161#### ` @asynccontextmanager async def get_connection(self) -> AsyncIterator[Conn] `
6262
6363This method is the only way to get a connection from the pool. It is expected
@@ -77,13 +77,11 @@ are available, the caller will yield to the event loop.
7777
7878When the block is exited, the connection will be returned to the pool.
7979
80-
8180### ` asyncio_connection_pool.ConnectionStrategy `
8281
8382This is an abstract class that defines the interface of the object passed as
8483` strategy ` . A subclass _ must_ implement the following methods:
8584
86-
8785#### ` async def create_connection(self) -> Awaitable[Conn] `
8886
8987This method is called to create a new connection to the resource. This happens
@@ -96,7 +94,6 @@ the pool, and in most cases will be stored in the pool to be re-used later.
9694If this method raises an exception, it will bubble up to the frame where
9795` ConnectionPool.get_connection() ` was called.
9896
99-
10097#### ` def connection_is_closed(self, conn: Conn) -> bool `
10198
10299This method is called to check if a connection is no longer able to be used.
@@ -111,7 +108,6 @@ exception is suppressed unless it is not a `BaseException`, like
111108` asyncio.CancelledError ` . It is the responsibility of the ` ConnectionStrategy `
112109implementation to avoid leaking a connection in this case.
113110
114-
115111#### ` async def close_connection(self, conn: Conn) `
116112
117113This method is called to close a connection. This occurs when the pool has
@@ -122,8 +118,7 @@ If this method raises an exception, the connection is assumed to be closed and
122118the exception bubbles to the caller of ` ConnectionPool.get_connection().__aexit__ `
123119(usually an ` async with ` block).
124120
125-
126- ## Integrations with 3rd-party libraries
121+ ## Integrations with 3rd-party libraries
127122
128123This package includes support for [ ` ddtrace ` ] [ ddtrace ] /[ ` datadog ` ] [ datadog ] and
129124for [ ` aioredis ` ] [ aioredis ] (<2.0.0).
@@ -142,14 +137,12 @@ arguments of the base class, supports:
142137- Optional ` extra_tags ` argument: Additional tags to provide to all metrics
143138 (strings in a ` "key:value" ` format)
144139
145-
146140### ` asyncio_connection_pool.contrib.aioredis.RedisConnectionStrategy `
147141
148142This class implements the ` ConnectionStrategy ` abstract methods, using
149143` aioredis.Redis ` objects as connections. The constructor takes arbitrary
150144arguments and forwards them to ` aioredis.create_redis ` .
151145
152-
153146## How is this safe without locks?
154147
155148I encourage you to read the [ source] ( https://github.com/fellowinsights/asyncio-connection-pool/blob/master/asyncio_connection_pool/__init__.py )
0 commit comments