Skip to content
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

Updated timeouts #132

Merged
merged 1 commit into from
Jul 27, 2023
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
48 changes: 35 additions & 13 deletions fauna/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
DefaultHttpWriteTimeout = timedelta(seconds=5)
DefaultHttpPoolTimeout = timedelta(seconds=5)
DefaultIdleConnectionTimeout = timedelta(seconds=5)
DefaultQueryTimeout = timedelta(seconds=5)
DefaultClientBufferTimeout = timedelta(seconds=5)
DefaultMaxConnections = 20
DefaultMaxIdleConnections = 20

Expand All @@ -38,7 +40,7 @@ class QueryOptions:

linearized: Optional[bool] = None
max_contention_retries: Optional[int] = None
query_timeout: Optional[timedelta] = None
query_timeout: Optional[timedelta] = DefaultQueryTimeout
query_tags: Optional[Mapping[str, str]] = None
traceparent: Optional[str] = None
typecheck: Optional[bool] = None
Expand All @@ -55,21 +57,33 @@ def __init__(
query_tags: Optional[Mapping[str, str]] = None,
linearized: Optional[bool] = None,
max_contention_retries: Optional[int] = None,
query_timeout: Optional[timedelta] = None,
typecheck: Optional[bool] = None,
additional_headers: Optional[Dict[str, str]] = None,
query_timeout: Optional[timedelta] = DefaultQueryTimeout,
client_buffer_timeout: Optional[timedelta] = DefaultClientBufferTimeout,
http_read_timeout: Optional[timedelta] = DefaultHttpReadTimeout,
http_write_timeout: Optional[timedelta] = DefaultHttpWriteTimeout,
http_connect_timeout: Optional[timedelta] = DefaultHttpConnectTimeout,
http_pool_timeout: Optional[timedelta] = DefaultHttpPoolTimeout,
http_idle_timeout: Optional[timedelta] = DefaultIdleConnectionTimeout,
):
"""Initializes a Client.

:param endpoint: The Fauna Endpoint to use. Defaults to https://db.fauna.com, or the FAUNA_ENDPOINT env variable.
:param secret: The Fauna Secret to use. Defaults to empty, or the FAUNA_SECRET env variable.
:param endpoint: The Fauna Endpoint to use. Defaults to https://db.fauna.com, or the `FAUNA_ENDPOINT` env variable.
:param secret: The Fauna Secret to use. Defaults to empty, or the `FAUNA_SECRET` env variable.
:param http_client: An :class:`HTTPClient` implementation. Defaults to a global :class:`HTTPXClient`.
:param query_tags: Tags to associate with the query. See `logging <https://docs.fauna.com/fauna/current/build/logs/query_log/>`_
:param linearized: If true, unconditionally run the query as strictly serialized. This affects read-only transactions. Transactions which write will always be strictly serialized.
:param max_contention_retries: The max number of times to retry the query if contention is encountered.
:param query_timeout: Controls the maximum amount of time (in milliseconds) Fauna will execute your query before marking it failed.
:param typecheck: Enable or disable typechecking of the query before evaluation. If not set, Fauna will use the value of the "typechecked" flag on the database configuration.
:param additional_headers: Add/update HTTP request headers for the query. In general, this should not be necessary.
:param query_timeout: Controls the maximum amount of time Fauna will execute your query before marking it failed, default is :py:data:`DefaultQueryTimeout`.
:param client_buffer_timeout: Time in milliseconds beyond query_timeout at which the client will abort a request if it has not received a response. The default is :py:data:`DefaultClientBufferTimeout`, which should account for network latency for most clients. The value must be greater than zero. The closer to zero the value is, the more likely the client is to abort the request before the server can report a legitimate response or error.
:param http_read_timeout: Set HTTP Read timeout, default is :py:data:`DefaultHttpReadTimeout`.
:param http_write_timeout: Set HTTP Write timeout, default is :py:data:`DefaultHttpWriteTimeout`.
:param http_connect_timeout: Set HTTP Connect timeout, default is :py:data:`DefaultHttpConnectTimeout`.
:param http_pool_timeout: Set HTTP Pool timeout, default is :py:data:`DefaultHttpPoolTimeout`.
:param http_idle_timeout: Set HTTP Idle timeout, default is :py:data:`DefaultIdleConnectionTimeout`.
"""

self._set_endpoint(endpoint)
Expand Down Expand Up @@ -119,14 +133,21 @@ def __init__(
self._session = http_client
else:
if fauna.global_http_client is None:
read_timeout: Optional[timedelta] = DefaultHttpReadTimeout
timeout_s: Optional[float] = None
if query_timeout is not None and client_buffer_timeout is not None:
timeout_s = (query_timeout + client_buffer_timeout).total_seconds()
read_timeout_s: Optional[float] = None
if read_timeout is not None:
read_timeout_s = read_timeout.total_seconds()

write_timeout_s = DefaultHttpWriteTimeout.total_seconds()
pool_timeout_s = DefaultHttpPoolTimeout.total_seconds()
idle_timeout_s = DefaultIdleConnectionTimeout.total_seconds()
if http_read_timeout is not None:
read_timeout_s = http_read_timeout.total_seconds()

write_timeout_s: Optional[float] = http_write_timeout.total_seconds(
) if http_write_timeout is not None else None
connect_timeout_s: Optional[float] = http_connect_timeout.total_seconds(
) if http_connect_timeout is not None else None
pool_timeout_s: Optional[float] = http_pool_timeout.total_seconds(
) if http_pool_timeout is not None else None
idle_timeout_s: Optional[float] = http_idle_timeout.total_seconds(
) if http_idle_timeout is not None else None

import httpx
from fauna.http.httpx_client import HTTPXClient
Expand All @@ -135,7 +156,8 @@ def __init__(
http1=False,
http2=True,
timeout=httpx.Timeout(
connect=DefaultMaxConnections,
timeout=timeout_s,
connect=connect_timeout_s,
read=read_timeout_s,
write=write_timeout_s,
pool=pool_timeout_s,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_client_defaults(monkeypatch):

assert client._endpoint == "https://db.fauna.com"
assert client._auth.secret == ""
assert client._query_timeout_ms is None
assert client._query_timeout_ms is not None
assert client._session == fauna.global_http_client


Expand Down Expand Up @@ -110,7 +110,7 @@ def test_get_set_transaction_time():


def test_get_query_timeout():
c = Client()
c = Client(query_timeout=None)
assert c.get_query_timeout() is None

c = Client(query_timeout=timedelta(minutes=1))
Expand Down