Skip to content
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
3 changes: 3 additions & 0 deletions dialect/providers/modules/deepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Provider(SoupProvider):
def __init__(self, **kwargs):
super().__init__(**kwargs)

self.retry_errors = (429,)
self.chars_limit = 5000

# DeepL API Free keys can be identified by the suffix ":fx"
Expand Down Expand Up @@ -132,6 +133,8 @@ def check_known_errors(self, status, data):
raise APIKeyInvalid(message)
case 456:
raise ServiceLimitReached(message)
case 429:
raise UnexpectedError("Too many requests!")

if status != 200:
raise UnexpectedError(message)
Expand Down
30 changes: 27 additions & 3 deletions dialect/providers/soup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import json
import logging
from asyncio import sleep
from typing import Any

from gi.repository import GLib, Soup
Expand All @@ -18,6 +19,11 @@ class SoupProvider(BaseProvider):

def __init__(self, **kwargs):
super().__init__(**kwargs)

self.retry_errors: tuple[int] = tuple()
""" Error codes that should be retried automatically """
self.max_retries = 5
""" Max number of tries """

def encode_data(self, data: Any) -> GLib.Bytes | None:
"""
Expand Down Expand Up @@ -123,6 +129,8 @@ async def send_and_read_and_process(

Converts `GLib.Error` to `RequestError`.

It also handles retries for status codes listen in ``self.retry_errors``.

Args:
message: Message to send.
check_common: If response data should be checked for errors using check_known_errors.
Expand All @@ -132,11 +140,27 @@ async def send_and_read_and_process(
The JSON deserialized to a python object or bytes if ``json`` is ``False``.
"""

try:
async def send_and_read() -> Any:
if return_json:
response = await self.send_and_read_json(message)
return await self.send_and_read_json(message)
else:
response = await self.send_and_read(message)
return await self.send_and_read(message)

try:
response = await send_and_read()

# Do retries with exponential backoff for errors
if message.get_status() in self.retry_errors:
delay = 1

for _ in range(self.max_retries):
await sleep(delay)
response = await send_and_read()

if message.get_status() in self.retry_errors:
delay *= 2
else:
break

if check_common:
self.check_known_errors(message.get_status(), response)
Expand Down