Skip to content

Commit

Permalink
Improve shield usage in old Python. Code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusSintonen committed Jun 11, 2024
1 parent 2a27e9e commit 2402e8e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 108 deletions.
6 changes: 3 additions & 3 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend
from .._exceptions import ConnectionNotAvailable, UnsupportedProtocol
from .._models import Origin, Request, Response
from .._synchronization import AsyncEvent, AsyncShieldCancellation, AsyncThreadLock
from .._synchronization import AsyncEvent, AsyncThreadLock, async_cancel_shield
from .connection import AsyncHTTPConnection
from .interfaces import AsyncConnectionInterface, AsyncRequestInterface

Expand Down Expand Up @@ -307,7 +307,7 @@ async def close() -> None:
for connection in closing:
await connection.aclose()

await AsyncShieldCancellation.shield(close)
await async_cancel_shield(close)

async def aclose(self) -> None:
# Explicitly close the connection pool.
Expand Down Expand Up @@ -376,7 +376,7 @@ async def aclose(self) -> None:
self._closed = True

if hasattr(self._stream, "aclose"):
await AsyncShieldCancellation.shield(self._stream.aclose)
await async_cancel_shield(self._stream.aclose)

with self._pool._optional_thread_lock:
self._pool._requests.remove(self._pool_request)
Expand Down
6 changes: 3 additions & 3 deletions httpcore/_async/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
map_exceptions,
)
from .._models import Origin, Request, Response
from .._synchronization import AsyncLock, AsyncShieldCancellation
from .._synchronization import AsyncLock, async_cancel_shield
from .._trace import Trace
from .interfaces import AsyncConnectionInterface

Expand Down Expand Up @@ -138,7 +138,7 @@ async def handle_async_request(self, request: Request) -> Response:
)
except BaseException as exc:
async with Trace("response_closed", logger, request) as trace:
await AsyncShieldCancellation.shield(self._response_closed)
await async_cancel_shield(self._response_closed)
raise exc

# Sending the request...
Expand Down Expand Up @@ -343,7 +343,7 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
# If we get an exception while streaming the response,
# we want to close the response (and possibly the connection)
# before raising that exception.
await AsyncShieldCancellation.shield(self.aclose)
await async_cancel_shield(self.aclose)
raise exc

async def aclose(self) -> None:
Expand Down
8 changes: 4 additions & 4 deletions httpcore/_async/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
RemoteProtocolError,
)
from .._models import Origin, Request, Response
from .._synchronization import AsyncLock, AsyncSemaphore, AsyncShieldCancellation
from .._synchronization import AsyncLock, AsyncSemaphore, async_cancel_shield
from .._trace import Trace
from .interfaces import AsyncConnectionInterface

Expand Down Expand Up @@ -108,7 +108,7 @@ async def handle_async_request(self, request: Request) -> Response:
async with Trace("send_connection_init", logger, request, kwargs):
await self._send_connection_init(**kwargs)
except BaseException as exc:
await AsyncShieldCancellation.shield(self.aclose)
await async_cancel_shield(self.aclose)
raise exc

self._sent_connection_init = True
Expand Down Expand Up @@ -166,7 +166,7 @@ async def close() -> None:
async with Trace("response_closed", logger, request, kwargs):
await self._response_closed(stream_id=stream_id)

await AsyncShieldCancellation.shield(close)
await async_cancel_shield(close)

if isinstance(exc, h2.exceptions.ProtocolError):
# One case where h2 can raise a protocol error is when a
Expand Down Expand Up @@ -579,7 +579,7 @@ async def __aiter__(self) -> typing.AsyncIterator[bytes]:
# If we get an exception while streaming the response,
# we want to close the response (and possibly the connection)
# before raising that exception.
await AsyncShieldCancellation.shield(self.aclose)
await async_cancel_shield(self.aclose)
raise exc

async def aclose(self) -> None:
Expand Down
6 changes: 3 additions & 3 deletions httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .._backends.base import SOCKET_OPTION, NetworkBackend
from .._exceptions import ConnectionNotAvailable, UnsupportedProtocol
from .._models import Origin, Request, Response
from .._synchronization import Event, ShieldCancellation, ThreadLock
from .._synchronization import Event, ThreadLock, sync_cancel_shield
from .connection import HTTPConnection
from .interfaces import ConnectionInterface, RequestInterface

Expand Down Expand Up @@ -307,7 +307,7 @@ def close() -> None:
for connection in closing:
connection.close()

ShieldCancellation.shield(close)
sync_cancel_shield(close)

def close(self) -> None:
# Explicitly close the connection pool.
Expand Down Expand Up @@ -376,7 +376,7 @@ def close(self) -> None:
self._closed = True

if hasattr(self._stream, "close"):
ShieldCancellation.shield(self._stream.close)
sync_cancel_shield(self._stream.close)

with self._pool._optional_thread_lock:
self._pool._requests.remove(self._pool_request)
Expand Down
6 changes: 3 additions & 3 deletions httpcore/_sync/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
map_exceptions,
)
from .._models import Origin, Request, Response
from .._synchronization import Lock, ShieldCancellation
from .._synchronization import Lock, sync_cancel_shield
from .._trace import Trace
from .interfaces import ConnectionInterface

Expand Down Expand Up @@ -138,7 +138,7 @@ def handle_request(self, request: Request) -> Response:
)
except BaseException as exc:
with Trace("response_closed", logger, request) as trace:
ShieldCancellation.shield(self._response_closed)
sync_cancel_shield(self._response_closed)
raise exc

# Sending the request...
Expand Down Expand Up @@ -343,7 +343,7 @@ def __iter__(self) -> Iterator[bytes]:
# If we get an exception while streaming the response,
# we want to close the response (and possibly the connection)
# before raising that exception.
ShieldCancellation.shield(self.close)
sync_cancel_shield(self.close)
raise exc

def close(self) -> None:
Expand Down
8 changes: 4 additions & 4 deletions httpcore/_sync/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
RemoteProtocolError,
)
from .._models import Origin, Request, Response
from .._synchronization import Lock, Semaphore, ShieldCancellation
from .._synchronization import Lock, Semaphore, sync_cancel_shield
from .._trace import Trace
from .interfaces import ConnectionInterface

Expand Down Expand Up @@ -108,7 +108,7 @@ def handle_request(self, request: Request) -> Response:
with Trace("send_connection_init", logger, request, kwargs):
self._send_connection_init(**kwargs)
except BaseException as exc:
ShieldCancellation.shield(self.close)
sync_cancel_shield(self.close)
raise exc

self._sent_connection_init = True
Expand Down Expand Up @@ -166,7 +166,7 @@ def close() -> None:
with Trace("response_closed", logger, request, kwargs):
self._response_closed(stream_id=stream_id)

ShieldCancellation.shield(close)
sync_cancel_shield(close)

if isinstance(exc, h2.exceptions.ProtocolError):
# One case where h2 can raise a protocol error is when a
Expand Down Expand Up @@ -579,7 +579,7 @@ def __iter__(self) -> typing.Iterator[bytes]:
# If we get an exception while streaming the response,
# we want to close the response (and possibly the connection)
# before raising that exception.
ShieldCancellation.shield(self.close)
sync_cancel_shield(self.close)
raise exc

def close(self) -> None:
Expand Down
Loading

0 comments on commit 2402e8e

Please sign in to comment.