Skip to content

Commit 52bf270

Browse files
committed
Introduce fetch translations timeout setting
1 parent dbd2ca3 commit 52bf270

3 files changed

Lines changed: 65 additions & 9 deletions

File tree

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,40 @@ To learn more about using Transifex Python toolkit check:
4343
* For a general overview visit [Transifex Native overview](https://developers.transifex.com/docs/native?utm_campaign=tx-native&utm_source=github&utm_medium=link)
4444
* For some common questions & answers check our [Transifex Native community](https://community.transifex.com/c/transifex-native/17)
4545

46+
# Django settings reference
47+
48+
The Transifex Native Django sdk is controlled via a set of configuration options defined in Django settings:
49+
50+
- TRANSIFEX_TOKEN: API token that connects your application to a Transifex project.
51+
Must be set for both pushing source strings and fetching translations.
52+
53+
- TRANSIFEX_SECRET: Secret used together with the token for authenticated operations against CDS (e.g. pushing source content, invalidating cache).
54+
55+
- TRANSIFEX_CDS_HOST: Override the default CDS host (https://cds.svc.transifex.net).
56+
57+
- TRANSIFEX_FILTER_STATUS: Optional CDS filter[status] parameter used when fetching translations (e.g. "reviewed", "proofread"). If not set, CDS returns all available statuses.
58+
59+
- TRANSIFEX_FILTER_TAGS: Optional CDS filter[tags] parameter used when fetching translations. Use this to limit fetched content to specific tags.
60+
61+
- TRANSIFEX_MISSING_POLICY: Custom “missing translation” policy class.
62+
Defaults to the built-in SourceStringPolicy when not provided.
63+
64+
- TRANSIFEX_ERROR_POLICY: Custom error handling policy class.
65+
Defaults to SourceErrorPolicy.
66+
67+
- TRANSIFEX_CACHE
68+
Custom cache implementation. Defaults to an in-memory cache (MemoryCache) if not provided. Can be used to integrate with a shared cache (e.g. Redis, memcached).
69+
70+
- SKIP_TRANSLATIONS_SYNC: If True, disables automatic translation sync (OTA) for this environment.
71+
72+
- TRANSIFEX_SYNC_INTERVAL: Interval in seconds for the background sync daemon that fetches translations. Default: 30 * 60 (30 minutes). Set to 0 to disable periodic sync and only fetch on startup.
73+
74+
- TRANSIFEX_FETCH_ALL_LANGUAGES: When True, fetch translations for all languages configured in CDS. When False (default), only fetch translations for languages listed in Django’s LANGUAGES setting.
75+
76+
- TRANSIFEX_FETCH_TEMEOUT:
77+
Maximum time in seconds to wait when fetching translations or locales from CDS.
78+
0 (default) = no global timeout.
79+
4680
# License
4781

4882
Licensed under Apache License 2.0, see `LICENSE` file.

transifex/native/cds.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import time
44
from urllib.parse import urlencode
5+
from transifex.native.django.settings import TRANSIFEX_FETCH_TEMEOUT
56

67
import requests
78
from transifex.native.consts import (KEY_CHARACTER_LIMIT,
@@ -364,16 +365,33 @@ def _get_headers(self, use_secret=False, etag=None):
364365

365366
def retry_get_request(self, *args, **kwargs):
366367
""" Resilient function for GET requests """
367-
retries, last_response_status = 0, 202
368-
while (last_response_status == 202 or
369-
500 <= last_response_status < 600 and
370-
retries < MAX_RETRIES):
371-
372-
if 500 <= last_response_status < 600:
373-
retries += 1
374-
time.sleep(retries * RETRY_DELAY_SEC)
368+
retries_5xx = 0
369+
last_response_status = 202
370+
start_ts = time.time()
371+
max_total_seconds = TRANSIFEX_FETCH_TEMEOUT
375372

373+
while True:
376374
response = requests.get(*args, **kwargs)
377375
last_response_status = response.status_code
378376

379-
return response
377+
# Success or non-retryable status -> return immediately
378+
if last_response_status < 500 and last_response_status != 202:
379+
return response
380+
381+
# 202 handling
382+
if last_response_status == 202:
383+
time.sleep(RETRY_DELAY_SEC)
384+
385+
# 5xx handling
386+
elif 500 <= last_response_status < 600:
387+
retries_5xx += 1
388+
if retries_5xx > MAX_RETRIES:
389+
return response
390+
time.sleep(retries_5xx * RETRY_DELAY_SEC)
391+
392+
# Timeout handling
393+
if (
394+
max_total_seconds > 0 and
395+
(time.time() - start_ts) >= max_total_seconds
396+
):
397+
return response

transifex/native/django/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@
1616
TRANSIFEX_FETCH_ALL_LANGUAGES = getattr(settings,
1717
'TRANSIFEX_FETCH_ALL_LANGUAGES',
1818
False)
19+
TRANSIFEX_FETCH_TEMEOUT = getattr(settings,
20+
'TRANSIFEX_FETCH_TEMEOUT',
21+
0)
22+

0 commit comments

Comments
 (0)