1
- __all__ = ["HTTPClient" , "DefaultHTTPClient" ]
1
+ __all__ = ["HTTPClient" , "DefaultHTTPClient" , "DEFAULT_REQUEST_TIMEOUT" ]
2
2
3
3
import typing
4
4
from abc import ABC , abstractmethod
5
5
from typing import Any , MutableMapping , Optional , Tuple , Union
6
6
7
7
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
14
9
from requests_toolbelt import MultipartEncoder
15
10
from urllib3 .poolmanager import PoolManager
16
11
from urllib3 .util .retry import Retry
17
12
18
13
from arango .response import Response
19
14
from arango .typings import Headers
20
15
16
+ DEFAULT_REQUEST_TIMEOUT = 60
17
+
21
18
22
19
class HTTPClient (ABC ): # pragma: no cover
23
20
"""Abstract base class for HTTP clients."""
@@ -73,23 +70,29 @@ def send_request(
73
70
class DefaultHTTPAdapter (HTTPAdapter ):
74
71
"""Default transport adapter implementation
75
72
73
+ :param connection_timeout: Socket timeout in seconds for each individual connection.
74
+ :type connection_timeout: int | float
76
75
:param pool_connections: The number of urllib3 connection pools to cache.
77
76
:type pool_connections: int
78
77
:param pool_maxsize: The maximum number of connections to save in the pool.
79
78
: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.
81
82
:type pool_timeout: int | float | None
82
83
:param kwargs: Additional keyword arguments passed to the HTTPAdapter constructor.
83
84
:type kwargs: Any
84
85
"""
85
86
86
87
def __init__ (
87
88
self ,
89
+ connection_timeout : Union [int , float ] = DEFAULT_REQUEST_TIMEOUT ,
88
90
pool_connections : int = DEFAULT_POOLSIZE ,
89
91
pool_maxsize : int = DEFAULT_POOLSIZE ,
90
- pool_timeout : Union [int , float , None ] = DEFAULT_POOL_TIMEOUT ,
92
+ pool_timeout : Union [int , float , None ] = None ,
91
93
** kwargs : Any
92
94
) -> None :
95
+ self ._connection_timeout = connection_timeout
93
96
self ._pool_timeout = pool_timeout
94
97
super ().__init__ (
95
98
pool_connections = pool_connections , pool_maxsize = pool_maxsize , ** kwargs
@@ -99,21 +102,28 @@ def __init__(
99
102
def init_poolmanager (
100
103
self , connections , maxsize , block = DEFAULT_POOLBLOCK , ** pool_kwargs
101
104
) -> 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
+ )
109
113
)
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 )
110
120
111
121
112
122
class DefaultHTTPClient (HTTPClient ):
113
123
"""Default HTTP client implementation.
114
124
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
117
127
:param retry_attempts: Number of retry attempts.
118
128
:type retry_attempts: int
119
129
:param backoff_factor: Backoff factor for retry attempts.
@@ -122,18 +132,20 @@ class DefaultHTTPClient(HTTPClient):
122
132
:type pool_connections: int
123
133
:param pool_maxsize: The maximum number of connections to save in the pool.
124
134
: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
127
139
"""
128
140
129
141
def __init__ (
130
142
self ,
131
- request_timeout : int = 60 ,
143
+ request_timeout : Union [ int , float ] = DEFAULT_REQUEST_TIMEOUT ,
132
144
retry_attempts : int = 3 ,
133
145
backoff_factor : float = 1.0 ,
134
146
pool_connections : int = 10 ,
135
147
pool_maxsize : int = 10 ,
136
- pool_timeout : Union [int , float , None ] = DEFAULT_POOL_TIMEOUT ,
148
+ pool_timeout : Union [int , float , None ] = None ,
137
149
) -> None :
138
150
self .request_timeout = request_timeout
139
151
self ._retry_attempts = retry_attempts
@@ -157,6 +169,7 @@ def create_session(self, host: str) -> Session:
157
169
allowed_methods = ["HEAD" , "GET" , "OPTIONS" ],
158
170
)
159
171
http_adapter = DefaultHTTPAdapter (
172
+ connection_timeout = self .request_timeout ,
160
173
pool_connections = self ._pool_connections ,
161
174
pool_maxsize = self ._pool_maxsize ,
162
175
pool_timeout = self ._pool_timeout ,
0 commit comments