Skip to content

Add 'read_timeout' option in client.py #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 17, 2022
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
27 changes: 27 additions & 0 deletions arango/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class ArangoClient:
False: Do not verify TLS certificate.
str: Path to a custom CA bundle file or directory.
:type verify_override: Union[bool, str, None]
:param request_timeout: This is the default request timeout (in seconds)
for http requests issued by the client if the parameter http_client is
not secified. The default value is 60.
None: No timeout.
int: Timeout value in seconds.
:type request_timeout: Any
"""

def __init__(
Expand All @@ -64,6 +70,7 @@ def __init__(
serializer: Callable[..., str] = lambda x: dumps(x),
deserializer: Callable[[str], Any] = lambda x: loads(x),
verify_override: Union[bool, str, None] = None,
request_timeout: Any = 60,
) -> None:
if isinstance(hosts, str):
self._hosts = [host.strip("/") for host in hosts.split(",")]
Expand All @@ -80,7 +87,13 @@ def __init__(
else:
self._host_resolver = RoundRobinHostResolver(host_count, resolver_max_tries)

# Initializes the http client
self._http = http_client or DefaultHTTPClient()
# Sets the request timeout.
# This call can only happen AFTER initializing the http client.
if http_client is None:
self.request_timeout = request_timeout

self._serializer = serializer
self._deserializer = deserializer
self._sessions = [self._http.create_session(h) for h in self._hosts]
Expand Down Expand Up @@ -117,6 +130,20 @@ def version(self) -> str:
version: str = get_distribution("python-arango").version
return version

@property
def request_timeout(self) -> Any:
"""Return the request timeout of the http client.

:return: Request timeout.
:rtype: Any
"""
return self._http.REQUEST_TIMEOUT # type: ignore

# Setter for request_timeout
@request_timeout.setter
def request_timeout(self, value: Any) -> None:
self._http.REQUEST_TIMEOUT = value # type: ignore

def db(
self,
name: str = "_system",
Expand Down
3 changes: 3 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def test_client_attributes():
assert repr(client) == client_repr
assert isinstance(client._host_resolver, RandomHostResolver)

client = ArangoClient(hosts=client_hosts, request_timeout=120)
assert client.request_timeout == client._http.REQUEST_TIMEOUT == 120


def test_client_good_connection(db, username, password):
client = ArangoClient(hosts="http://127.0.0.1:8529")
Expand Down