Skip to content

Commit 5e2a717

Browse files
committed
Adding request_timeout together with pool_timeout
1 parent 3eac4f6 commit 5e2a717

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

arango/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
)
1414
from arango.database import StandardDatabase
1515
from arango.exceptions import ServerConnectionError
16-
from arango.http import DefaultHTTPClient, HTTPClient
16+
from arango.http import DEFAULT_REQUEST_TIMEOUT, DefaultHTTPClient, HTTPClient
1717
from arango.resolver import (
1818
HostResolver,
1919
RandomHostResolver,
@@ -58,7 +58,7 @@ class ArangoClient:
5858
not specified. The default value is 60.
5959
None: No timeout.
6060
int: Timeout value in seconds.
61-
:type request_timeout: Any
61+
:type request_timeout: int | float
6262
"""
6363

6464
def __init__(
@@ -70,7 +70,7 @@ def __init__(
7070
serializer: Callable[..., str] = lambda x: dumps(x),
7171
deserializer: Callable[[str], Any] = lambda x: loads(x),
7272
verify_override: Union[bool, str, None] = None,
73-
request_timeout: Any = 60,
73+
request_timeout: Union[int, float] = DEFAULT_REQUEST_TIMEOUT,
7474
) -> None:
7575
if isinstance(hosts, str):
7676
self._hosts = [host.strip("/") for host in hosts.split(",")]

arango/http.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
__all__ = ["HTTPClient", "DefaultHTTPClient"]
1+
__all__ = ["HTTPClient", "DefaultHTTPClient", "DEFAULT_REQUEST_TIMEOUT"]
22

33
import typing
44
from abc import ABC, abstractmethod
55
from typing import Any, MutableMapping, Optional, Tuple, Union
66

77
from requests import Session
8-
from requests.adapters import (
9-
DEFAULT_POOL_TIMEOUT,
10-
DEFAULT_POOLBLOCK,
11-
DEFAULT_POOLSIZE,
12-
HTTPAdapter,
13-
)
8+
from requests.adapters import DEFAULT_POOLBLOCK, DEFAULT_POOLSIZE, HTTPAdapter
149
from requests_toolbelt import MultipartEncoder
1510
from urllib3.poolmanager import PoolManager
1611
from urllib3.util.retry import Retry
1712

1813
from arango.response import Response
1914
from arango.typings import Headers
2015

16+
DEFAULT_REQUEST_TIMEOUT = 60
17+
2118

2219
class HTTPClient(ABC): # pragma: no cover
2320
"""Abstract base class for HTTP clients."""
@@ -73,23 +70,29 @@ def send_request(
7370
class DefaultHTTPAdapter(HTTPAdapter):
7471
"""Default transport adapter implementation
7572
73+
:param connection_timeout: Socket timeout in seconds for each individual connection.
74+
:type connection_timeout: int | float
7675
:param pool_connections: The number of urllib3 connection pools to cache.
7776
:type pool_connections: int
7877
:param pool_maxsize: The maximum number of connections to save in the pool.
7978
:type pool_maxsize: int
80-
:param pool_timeout: Socket timeout in seconds for each individual connection.
79+
:param pool_timeout: If set, then the pool will be set to block=True,
80+
and requests will block for pool_timeout seconds and raise
81+
EmptyPoolError if no connection is available within the time period.
8182
:type pool_timeout: int | float | None
8283
:param kwargs: Additional keyword arguments passed to the HTTPAdapter constructor.
8384
:type kwargs: Any
8485
"""
8586

8687
def __init__(
8788
self,
89+
connection_timeout: Union[int, float] = DEFAULT_REQUEST_TIMEOUT,
8890
pool_connections: int = DEFAULT_POOLSIZE,
8991
pool_maxsize: int = DEFAULT_POOLSIZE,
90-
pool_timeout: Union[int, float, None] = DEFAULT_POOL_TIMEOUT,
92+
pool_timeout: Union[int, float, None] = None,
9193
**kwargs: Any
9294
) -> None:
95+
self._connection_timeout = connection_timeout
9396
self._pool_timeout = pool_timeout
9497
super().__init__(
9598
pool_connections=pool_connections, pool_maxsize=pool_maxsize, **kwargs
@@ -99,21 +102,28 @@ def __init__(
99102
def init_poolmanager(
100103
self, connections, maxsize, block=DEFAULT_POOLBLOCK, **pool_kwargs
101104
) -> None:
102-
self.poolmanager = PoolManager(
103-
num_pools=connections,
104-
maxsize=maxsize,
105-
block=block,
106-
strict=True,
107-
timeout=self._pool_timeout,
108-
**pool_kwargs
105+
kwargs = pool_kwargs
106+
kwargs.update(
107+
dict(
108+
num_pools=connections,
109+
maxsize=maxsize,
110+
strict=True,
111+
timeout=self._connection_timeout,
112+
)
109113
)
114+
if self._pool_timeout is not None:
115+
kwargs["block"] = True
116+
kwargs["timeout"] = self._pool_timeout
117+
else:
118+
kwargs["block"] = False
119+
self.poolmanager = PoolManager(**kwargs)
110120

111121

112122
class DefaultHTTPClient(HTTPClient):
113123
"""Default HTTP client implementation.
114124
115-
:param request_timeout: Default request timeout in seconds.
116-
:type request_timeout: int
125+
:param request_timeout: Timeout in seconds for each individual connection.
126+
:type request_timeout: int | float
117127
:param retry_attempts: Number of retry attempts.
118128
:type retry_attempts: int
119129
:param backoff_factor: Backoff factor for retry attempts.
@@ -122,18 +132,20 @@ class DefaultHTTPClient(HTTPClient):
122132
:type pool_connections: int
123133
:param pool_maxsize: The maximum number of connections to save in the pool.
124134
:type pool_maxsize: int
125-
:param pool_timeout: Socket timeout in seconds for each individual connection.
126-
:type pool_timeout: int | float
135+
:param pool_timeout: If set, then the pool will be set to block=True,
136+
and requests will block for pool_timeout seconds and raise
137+
EmptyPoolError if no connection is available within the time period.
138+
:type pool_timeout: int | float | None
127139
"""
128140

129141
def __init__(
130142
self,
131-
request_timeout: int = 60,
143+
request_timeout: Union[int, float] = DEFAULT_REQUEST_TIMEOUT,
132144
retry_attempts: int = 3,
133145
backoff_factor: float = 1.0,
134146
pool_connections: int = 10,
135147
pool_maxsize: int = 10,
136-
pool_timeout: Union[int, float, None] = DEFAULT_POOL_TIMEOUT,
148+
pool_timeout: Union[int, float, None] = None,
137149
) -> None:
138150
self.request_timeout = request_timeout
139151
self._retry_attempts = retry_attempts
@@ -157,6 +169,7 @@ def create_session(self, host: str) -> Session:
157169
allowed_methods=["HEAD", "GET", "OPTIONS"],
158170
)
159171
http_adapter = DefaultHTTPAdapter(
172+
connection_timeout=self.request_timeout,
160173
pool_connections=self._pool_connections,
161174
pool_maxsize=self._pool_maxsize,
162175
pool_timeout=self._pool_timeout,

0 commit comments

Comments
 (0)