1 from __future__ import absolute_import
2 # The default socket timeout, used by httplib to indicate that no timeout was
3 # specified by the user
4 from socket import _GLOBAL_DEFAULT_TIMEOUT
7 from ..exceptions import TimeoutStateError
9 # A sentinel value to indicate that no timeout was specified by the user in
16 Retrieve the current time. This function is mocked out in unit testing.
21 class Timeout(object):
22 """ Timeout configuration.
24 Timeouts can be defined as a default for a pool::
26 timeout = Timeout(connect=2.0, read=7.0)
27 http = PoolManager(timeout=timeout)
28 response = http.request('GET', 'http://example.com/')
30 Or per-request (which overrides the default for the pool)::
32 response = http.request('GET', 'http://example.com/', timeout=Timeout(10))
34 Timeouts can be disabled by setting all the parameters to ``None``::
36 no_timeout = Timeout(connect=None, read=None)
37 response = http.request('GET', 'http://example.com/, timeout=no_timeout)
41 This combines the connect and read timeouts into one; the read timeout
42 will be set to the time leftover from the connect attempt. In the
43 event that both a connect timeout and a total are specified, or a read
44 timeout and a total are specified, the shorter timeout will be applied.
48 :type total: integer, float, or None
51 The maximum amount of time to wait for a connection attempt to a server
52 to succeed. Omitting the parameter will default the connect timeout to
53 the system default, probably `the global default timeout in socket.py
54 <http://hg.python.org/cpython/file/603b4d593758/Lib/socket.py#l535>`_.
55 None will set an infinite timeout for connection attempts.
57 :type connect: integer, float, or None
60 The maximum amount of time to wait between consecutive
61 read operations for a response from the server. Omitting
62 the parameter will default the read timeout to the system
63 default, probably `the global default timeout in socket.py
64 <http://hg.python.org/cpython/file/603b4d593758/Lib/socket.py#l535>`_.
65 None will set an infinite timeout.
67 :type read: integer, float, or None
71 Many factors can affect the total amount of time for urllib3 to return
74 For example, Python's DNS resolver does not obey the timeout specified
75 on the socket. Other factors that can affect total request time include
76 high CPU load, high swap, the program running at a low priority level,
79 In addition, the read and total timeouts only measure the time between
80 read operations on the socket connecting the client and the server,
81 not the total amount of time for the request to return a complete
82 response. For most requests, the timeout is raised because the server
83 has not sent the first byte in the specified time. This is not always
84 the case; if a server streams one byte every fifteen seconds, a timeout
85 of 20 seconds will not trigger, even though the request will take
86 several minutes to complete.
88 If your goal is to cut off any request after a set amount of wall clock
89 time, consider having a second "watcher" thread to cut off a slow
93 #: A sentinel object representing the default timeout value
94 DEFAULT_TIMEOUT = _GLOBAL_DEFAULT_TIMEOUT
96 def __init__(self, total=None, connect=_Default, read=_Default):
97 self._connect = self._validate_timeout(connect, 'connect')
98 self._read = self._validate_timeout(read, 'read')
99 self.total = self._validate_timeout(total, 'total')
100 self._start_connect = None
103 return '%s(connect=%r, read=%r, total=%r)' % (
104 type(self).__name__, self._connect, self._read, self.total)
107 def _validate_timeout(cls, value, name):
108 """ Check that a timeout attribute is valid.
110 :param value: The timeout value to validate
111 :param name: The name of the timeout attribute to validate. This is
112 used to specify in error messages.
113 :return: The validated and casted version of the given value.
114 :raises ValueError: If the type is not an integer or a float, or if it
115 is a numeric value less than zero.
117 if value is _Default:
118 return cls.DEFAULT_TIMEOUT
120 if value is None or value is cls.DEFAULT_TIMEOUT:
125 except (TypeError, ValueError):
126 raise ValueError("Timeout value %s was %s, but it must be an "
127 "int or float." % (name, value))
131 raise ValueError("Attempted to set %s timeout to %s, but the "
132 "timeout cannot be set to a value less "
133 "than 0." % (name, value))
134 except TypeError: # Python 3
135 raise ValueError("Timeout value %s was %s, but it must be an "
136 "int or float." % (name, value))
141 def from_float(cls, timeout):
142 """ Create a new Timeout from a legacy timeout value.
144 The timeout value used by httplib.py sets the same timeout on the
145 connect(), and recv() socket requests. This creates a :class:`Timeout`
146 object that sets the individual timeouts to the ``timeout`` value
147 passed to this function.
149 :param timeout: The legacy timeout value.
150 :type timeout: integer, float, sentinel default object, or None
151 :return: Timeout object
152 :rtype: :class:`Timeout`
154 return Timeout(read=timeout, connect=timeout)
157 """ Create a copy of the timeout object
159 Timeout properties are stored per-pool but each request needs a fresh
160 Timeout object to ensure each one has its own start/stop configured.
162 :return: a copy of the timeout object
163 :rtype: :class:`Timeout`
165 # We can't use copy.deepcopy because that will also create a new object
166 # for _GLOBAL_DEFAULT_TIMEOUT, which socket.py uses as a sentinel to
167 # detect the user default.
168 return Timeout(connect=self._connect, read=self._read,
171 def start_connect(self):
172 """ Start the timeout clock, used during a connect() attempt
174 :raises urllib3.exceptions.TimeoutStateError: if you attempt
175 to start a timer that has been started already.
177 if self._start_connect is not None:
178 raise TimeoutStateError("Timeout timer has already been started.")
179 self._start_connect = current_time()
180 return self._start_connect
182 def get_connect_duration(self):
183 """ Gets the time elapsed since the call to :meth:`start_connect`.
185 :return: Elapsed time.
187 :raises urllib3.exceptions.TimeoutStateError: if you attempt
188 to get duration for a timer that hasn't been started.
190 if self._start_connect is None:
191 raise TimeoutStateError("Can't get connect duration for timer "
192 "that has not started.")
193 return current_time() - self._start_connect
196 def connect_timeout(self):
197 """ Get the value to use when setting a connection timeout.
199 This will be a positive float or integer, the value None
200 (never timeout), or the default system timeout.
202 :return: Connect timeout.
203 :rtype: int, float, :attr:`Timeout.DEFAULT_TIMEOUT` or None
205 if self.total is None:
208 if self._connect is None or self._connect is self.DEFAULT_TIMEOUT:
211 return min(self._connect, self.total)
214 def read_timeout(self):
215 """ Get the value for the read timeout.
217 This assumes some time has elapsed in the connection timeout and
218 computes the read timeout appropriately.
220 If self.total is set, the read timeout is dependent on the amount of
221 time taken by the connect timeout. If the connection time has not been
222 established, a :exc:`~urllib3.exceptions.TimeoutStateError` will be
225 :return: Value to use for the read timeout.
226 :rtype: int, float, :attr:`Timeout.DEFAULT_TIMEOUT` or None
227 :raises urllib3.exceptions.TimeoutStateError: If :meth:`start_connect`
228 has not yet been called on this object.
230 if (self.total is not None and
231 self.total is not self.DEFAULT_TIMEOUT and
232 self._read is not None and
233 self._read is not self.DEFAULT_TIMEOUT):
234 # In case the connect timeout has not yet been established.
235 if self._start_connect is None:
237 return max(0, min(self.total - self.get_connect_duration(),
239 elif self.total is not None and self.total is not self.DEFAULT_TIMEOUT:
240 return max(0, self.total - self.get_connect_duration())