Skip to content

Commit

Permalink
Retry on protocol error (#73)
Browse files Browse the repository at this point in the history
Retry request in batch client in the event of a httpx.ProtocolError exception. Prevents failures in rare events in which the server response causes httpx to raise protocol error
  • Loading branch information
anjz authored Oct 19, 2023
1 parent a5ad78e commit c8b7376
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

- Improve upload speeds for files submitted with the batch client
- Retry requests in batch client on httpx.ProtocolError

## [1.11.0] - 2023-08-25

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ websockets>=10
httpx[http2]~=0.23
polling2~=0.5
toml~=0.10.2
tenacity~=8.2.3
17 changes: 13 additions & 4 deletions speechmatics/batch_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import httpx
from polling2 import poll
from tenacity import retry, retry_if_exception_type, stop_after_attempt

from speechmatics.exceptions import JobNotFoundException, TranscriptionError
from speechmatics.helpers import get_version
Expand Down Expand Up @@ -166,11 +167,19 @@ def send_request(self, method: str, path: str, **kwargs) -> httpx.Response:
:raises httpx.HTTPError: When a request fails, raises an HTTPError
"""

# pylint: disable=no-member
with self.api_client.stream(method, path, **kwargs) as response:
response.read()
response.raise_for_status()
return response
@retry(
stop=stop_after_attempt(2),
retry=retry_if_exception_type(httpx.RemoteProtocolError),
)
def send():
with self.api_client.stream(method, path, **kwargs) as response:
response.read()
response.raise_for_status()
return response

return send()

def list_jobs(self) -> List[Dict[str, Any]]:
"""
Expand Down

0 comments on commit c8b7376

Please sign in to comment.