Skip to content

Commit

Permalink
Add retry mechanism on server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lpellegr committed Jun 14, 2024
1 parent 9fdfc6a commit 8e5bcc3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
24 changes: 24 additions & 0 deletions ipregistry/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import sys
import urllib.parse
from abc import ABC, abstractmethod

from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception

from typing import Union

import requests
Expand Down Expand Up @@ -72,7 +75,21 @@ def _build_base_url(self, resource, options):
return result


def is_server_error(exception):
return isinstance(exception, ApiError) and (exception.code == 'INTERNAL')


retry_on_server_error = retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=0.5, max=3),
retry=retry_if_exception(is_server_error),
reraise=True
)


class DefaultRequestHandler(IpregistryRequestHandler):

@retry_on_server_error
def batch_lookup_asns(self, asns, options):
response = None
try:
Expand All @@ -96,6 +113,7 @@ def batch_lookup_asns(self, asns, options):
except Exception as e:
raise ClientError(e)

@retry_on_server_error
def batch_lookup_ips(self, ips, options):
response = None
try:
Expand All @@ -119,6 +137,7 @@ def batch_lookup_ips(self, ips, options):
except Exception as e:
raise ClientError(e)

@retry_on_server_error
def batch_parse_user_agents(self, user_agents, options):
response = None
try:
Expand All @@ -142,6 +161,7 @@ def batch_parse_user_agents(self, user_agents, options):
except Exception as e:
raise ClientError(e)

@retry_on_server_error
def lookup_asn(self, asn, options):
response = None
try:
Expand All @@ -163,6 +183,7 @@ def lookup_asn(self, asn, options):
except Exception as err:
raise ClientError(err)

@retry_on_server_error
def lookup_ip(self, ip, options):
response = None
try:
Expand All @@ -184,9 +205,11 @@ def lookup_ip(self, ip, options):
except Exception as err:
raise ClientError(err)

@retry_on_server_error
def origin_lookup_ip(self, options):
return self.lookup_ip('', options)

@retry_on_server_error
def origin_parse_user_agent(self, options):
response = None
try:
Expand Down Expand Up @@ -259,3 +282,4 @@ def __headers(self):
python_version +
"; +https://github.com/ipregistry/ipregistry-python)"
}

17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ cachetools = '^5.3.3'
pydantic = "^2.7.3"
python = '>=3.8'
requests = '^2.32.3'
tenacity = "^8.3.0"

[tool.poetry.dev-dependencies]
pytest = '>=6.2.5'
Expand Down

0 comments on commit 8e5bcc3

Please sign in to comment.