general: add options to automatically retry requests (#49764)
gitea-wip/passerelle/pipeline/head There was a failure building this commit Details
gitea/passerelle/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Frédéric Péters 2020-12-28 20:56:59 +01:00
parent 9e2e2786a4
commit f1e7e5dd44
2 changed files with 19 additions and 1 deletions

View File

@ -142,6 +142,12 @@ class BaseResource(models.Model):
manager_form_base_class = GenericConnectorForm
hide_description_fields = []
# self.requests retry behaviour
retry_count = 0
retry_on_status = [429, 500, 502, 503, 504]
retry_on_methods = ['HEAD', 'GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'TRACE']
retry_backoff_factor = 0.5
# permission descriptions
_can_access_description = _('Access is limited to the following API users:')

View File

@ -24,9 +24,11 @@ import warnings
from requests import Session as RequestSession, Response as RequestResponse
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from requests.structures import CaseInsensitiveDict
from urllib3.exceptions import InsecureRequestWarning
from django.conf import settings
from django.core.cache import cache
from django.core.exceptions import PermissionDenied
@ -243,7 +245,17 @@ class Request(RequestSession):
self.resource = kwargs.pop('resource', None)
super(Request, self).__init__(*args, **kwargs)
if self.resource:
adapter = Request.ADAPTER_REGISTRY.setdefault(type(self.resource), HTTPAdapter())
if getattr(self.resource, 'retry_count', 0):
retry_strategy = Retry(
total=self.resource.retry_count,
backoff_factor=self.resource.retry_backoff_factor,
status_forcelist=self.resource.retry_on_status,
method_whitelist=self.resource.retry_on_methods
)
else:
retry_strategy = 0
adapter = Request.ADAPTER_REGISTRY.setdefault(type(self.resource),
HTTPAdapter(max_retries=retry_strategy))
self.mount('https://', adapter)
self.mount('http://', adapter)