Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ConnectionStrategy.make_connection return type #12

Merged
merged 10 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- uses: actions/setup-python@v5
with:
python-version: "3.9"
- run: pip install riot==0.4.0
- run: pip install riot==0.19.0
- run: riot -v run -s black -- --check .
mypy:
runs-on: ubuntu-latest
Expand All @@ -17,7 +17,7 @@ jobs:
- uses: actions/setup-python@v5
with:
python-version: "3.9"
- run: pip install riot==0.4.0
- run: pip install riot==0.19.0
- run: riot -v run mypy
flake8:
runs-on: ubuntu-latest
Expand All @@ -26,7 +26,7 @@ jobs:
- uses: actions/setup-python@v5
with:
python-version: "3.9"
- run: pip install riot==0.4.0
- run: pip install riot==0.19.0
- run: riot -v run flake8
test:
strategy:
Expand All @@ -41,12 +41,23 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: install riot
run: pip install riot==0.4.0
run: pip install riot==0.19.0
- name: run tests
run: riot -v run --python=${{ matrix.python-version }} test -- --cov=asyncio_connection_pool --cov-branch --cov-config=.coveragerc
- name: install coverage
run: pip install coverage
- name: upload coverage
run: pip install coveralls && coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
- name: print coverage
run: pip install coverage && coverage report
uses: coverallsapp/github-action@v2
with:
parallel: true
flag-name: run ${{ join(matrix.*, ' - ') }}
finish-coveralls:
needs: test
if: ${{ always() }}
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@v2
with:
parallel-finished: true
allow-empty: true
12 changes: 6 additions & 6 deletions asyncio_connection_pool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class ConnectionStrategy(ABC, Generic[Conn]):
@abstractmethod
async def make_connection(self) -> Awaitable[Conn]:
async def make_connection(self) -> Conn:
...

@abstractmethod
Expand Down Expand Up @@ -61,7 +61,7 @@ def __init__(
strategy: ConnectionStrategy[Conn],
max_size: int,
burst_limit: Optional[int] = None
):
) -> None:
self._loop = asyncio.get_event_loop()
self.strategy = strategy
self.max_size = max_size
Expand All @@ -82,20 +82,20 @@ def _waiters(self) -> int:
waiters = self.available._getters # type: ignore
return sum(not (w.done() or w.cancelled()) for w in waiters)

async def _connection_maker(self):
async def _connection_maker(self) -> Conn:
try:
conn = await self.strategy.make_connection()
finally:
self.currently_allocating -= 1
self.in_use += 1
return conn

async def _connection_waiter(self):
async def _connection_waiter(self) -> Conn:
conn = await self.available.get()
self.in_use += 1
return conn

def _get_conn(self) -> "Awaitable[Conn]":
def _get_conn(self) -> Awaitable[Conn]:
# This function is how we avoid explicitly locking. Since it is
# synchronous, we do all the "book-keeping" required to get a
# connection synchronously, and return a Future or Task which can be
Expand Down Expand Up @@ -132,7 +132,7 @@ def _get_conn(self) -> "Awaitable[Conn]":
return self._loop.create_task(self._connection_waiter())

@asynccontextmanager
async def get_connection(self) -> AsyncIterator[Conn]: # type: ignore
async def get_connection(self) -> AsyncIterator[Conn]:
# _get_conn atomically does any book-keeping and returns an awaitable
# that resolves to a connection.
conn = await self._get_conn()
Expand Down
2 changes: 1 addition & 1 deletion asyncio_connection_pool/contrib/datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _get_conn(self):
return super()._get_conn()

@asynccontextmanager
async def get_connection(self) -> AsyncIterator[Conn]: # type: ignore
async def get_connection(self) -> AsyncIterator[Conn]:
async with AsyncExitStack() as stack:
self._record_connection_acquiring(1)
try:
Expand Down
12 changes: 6 additions & 6 deletions riotfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
pys=3,
venvs=[
Venv(
pys=["3.8", "3.9", "3.10"],
pys=["3.8", "3.9", "3.10", "3.11", "3.12"],
name="test",
command="pytest {cmdargs}",
pkgs={
"pytest": "==6.2.5",
"pytest-asyncio": "==0.16.0",
"pytest": latest,
"pytest-asyncio": latest,
"pytest-cov": latest,
# extras_require
"ddtrace": latest,
Expand All @@ -21,11 +21,11 @@
name="mypy",
command="mypy asyncio_connection_pool",
pkgs={
"mypy": "==0.790",
"mypy": "==1.1.1",
},
),
Venv(
pkgs={"black": "==22.3.0"},
pkgs={"black": "==23.1.0"},
venvs=[
Venv(
name="fmt",
Expand All @@ -39,7 +39,7 @@
),
Venv(
name="flake8",
pkgs={"flake8": "==3.8.4"},
pkgs={"flake8": "==6.0.0"},
command="flake8 test asyncio_connection_pool",
),
],
Expand Down
Loading