Skip to content

Retry once on remote protocol errors #80

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

Merged
merged 3 commits into from
Jan 18, 2025
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
21 changes: 19 additions & 2 deletions onvif/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@
return original_load(self, *args, **kwargs)


class AsyncTransportProtocolErrorHandler(AsyncTransport):
"""Retry on remote protocol error.

http://datatracker.ietf.org/doc/html/rfc2616#section-8.1.4 allows the server
# to close the connection at any time, we treat this as a normal and try again
# once since
"""

@retry_connection_error(attempts=2, exception=httpx.RemoteProtocolError)
async def post(self, address, message, headers):
return await super().post(address, message, headers)

Check warning on line 109 in onvif/client.py

View check run for this annotation

Codecov / codecov/patch

onvif/client.py#L109

Added line #L109 was not covered by tests

@retry_connection_error(attempts=2, exception=httpx.RemoteProtocolError)
async def get(self, address, params, headers):
return await super().get(address, params, headers)

Check warning on line 113 in onvif/client.py

View check run for this annotation

Codecov / codecov/patch

onvif/client.py#L113

Added line #L113 was not covered by tests


async def _cached_document(url: str) -> Document:
"""Load external XML document from disk."""
if url in _DOCUMENT_CACHE:
Expand Down Expand Up @@ -223,9 +240,9 @@
verify=_NO_VERIFY_SSL_CONTEXT, timeout=timeouts, limits=_HTTPX_LIMITS
)
self.transport = (
AsyncTransport(client=client, wsdl_client=wsdl_client)
AsyncTransportProtocolErrorHandler(client=client, wsdl_client=wsdl_client)
if no_cache
else AsyncTransport(
else AsyncTransportProtocolErrorHandler(
client=client, wsdl_client=wsdl_client, cache=SqliteCache()
)
)
Expand Down
3 changes: 2 additions & 1 deletion onvif/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

def retry_connection_error(
attempts: int = DEFAULT_ATTEMPTS,
exception: httpx.HTTPError = httpx.RequestError,
) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]:
"""Define a wrapper to retry on connection error."""

Expand All @@ -37,7 +38,7 @@
for attempt in range(attempts):
try:
return await func(*args, **kwargs)
except httpx.RequestError as ex:
except exception as ex:

Check warning on line 41 in onvif/wrappers.py

View check run for this annotation

Codecov / codecov/patch

onvif/wrappers.py#L41

Added line #L41 was not covered by tests
#
# We should only need to retry on RemoteProtocolError but some cameras
# are flakey and sometimes do not respond to the Renew request so we
Expand Down
Loading