@@ -123,7 +123,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
123
123
# Return False to re-raise any potential exceptions
124
124
return False
125
125
126
- def close (self ):
126
+ async def close (self ):
127
127
"""
128
128
Close all pooled connections and disable the pool.
129
129
"""
@@ -248,7 +248,7 @@ def _new_conn(self):
248
248
** self .conn_kw )
249
249
return conn
250
250
251
- def _get_conn (self , timeout = None ):
251
+ async def _get_conn (self , timeout = None ):
252
252
"""
253
253
Get a connection. Will return a pooled connection if one is available.
254
254
@@ -277,11 +277,11 @@ def _get_conn(self, timeout=None):
277
277
# If this is a persistent connection, check if it got disconnected
278
278
if conn and is_connection_dropped (conn ):
279
279
log .debug ("Resetting dropped connection: %s" , self .host )
280
- conn .close ()
280
+ await conn .close ()
281
281
282
282
return conn or self ._new_conn ()
283
283
284
- def _put_conn (self , conn ):
284
+ async def _put_conn (self , conn ):
285
285
"""
286
286
Put a connection back into the pool.
287
287
@@ -309,13 +309,13 @@ def _put_conn(self, conn):
309
309
310
310
# Connection never got put back into the pool, close it.
311
311
if conn :
312
- conn .close ()
312
+ await conn .close ()
313
313
314
- def _start_conn (self , conn , connect_timeout ):
314
+ async def _start_conn (self , conn , connect_timeout ):
315
315
"""
316
316
Called right before a request is made, after the socket is created.
317
317
"""
318
- conn .connect (connect_timeout = connect_timeout )
318
+ await conn .connect (connect_timeout = connect_timeout )
319
319
320
320
def _get_timeout (self , timeout ):
321
321
""" Helper that always returns a :class:`urllib3.util.Timeout` """
@@ -347,8 +347,8 @@ def _raise_timeout(self, err, url, timeout_value):
347
347
if 'timed out' in str (err ) or 'did not complete (read)' in str (err ): # Python 2.6
348
348
raise ReadTimeoutError (self , url , "Read timed out. (read timeout=%s)" % timeout_value )
349
349
350
- def _make_request (self , conn , method , url , timeout = _Default , body = None ,
351
- headers = None ):
350
+ async def _make_request (
351
+ self , conn , method , url , timeout = _Default , body = None , headers = None ):
352
352
"""
353
353
Perform a request on a given urllib connection object taken from our
354
354
pool.
@@ -370,7 +370,7 @@ def _make_request(self, conn, method, url, timeout=_Default, body=None,
370
370
371
371
# Trigger any extra validation we need to do.
372
372
try :
373
- self ._start_conn (conn , timeout_obj .connect_timeout )
373
+ await self ._start_conn (conn , timeout_obj .connect_timeout )
374
374
except (SocketTimeout , BaseSSLError ) as e :
375
375
# Py2 raises this as a BaseSSLError, Py3 raises it as socket timeout.
376
376
self ._raise_timeout (err = e , url = url , timeout_value = conn .timeout )
@@ -405,7 +405,8 @@ def _make_request(self, conn, method, url, timeout=_Default, body=None,
405
405
406
406
# Receive the response from the server
407
407
try :
408
- response = conn .send_request (request , read_timeout = read_timeout )
408
+ response = await conn .send_request (
409
+ request , read_timeout = read_timeout )
409
410
except (SocketTimeout , BaseSSLError , SocketError ) as e :
410
411
self ._raise_timeout (err = e , url = url , timeout_value = read_timeout )
411
412
raise
@@ -420,7 +421,7 @@ def _make_request(self, conn, method, url, timeout=_Default, body=None,
420
421
def _absolute_url (self , path ):
421
422
return Url (scheme = self .scheme , host = self .host , port = self .port , path = path ).url
422
423
423
- def close (self ):
424
+ async def close (self ):
424
425
"""
425
426
Close all pooled connections and disable the pool.
426
427
"""
@@ -431,7 +432,7 @@ def close(self):
431
432
while True :
432
433
conn = old_pool .get (block = False )
433
434
if conn :
434
- conn .close ()
435
+ await conn .close ()
435
436
436
437
except queue .Empty :
437
438
pass # Done.
@@ -457,8 +458,9 @@ def is_same_host(self, url):
457
458
458
459
return (scheme , host , port ) == (self .scheme , self .host , self .port )
459
460
460
- def urlopen (self , method , url , body = None , headers = None , retries = None ,
461
- timeout = _Default , pool_timeout = None , body_pos = None , ** response_kw ):
461
+ async def urlopen (self , method , url , body = None , headers = None , retries = None ,
462
+ timeout = _Default , pool_timeout = None , body_pos = None ,
463
+ ** response_kw ):
462
464
"""
463
465
Get a connection from the pool and perform an HTTP request. This is the
464
466
lowest level call for making a request, so you'll need to specify all
@@ -554,14 +556,15 @@ def urlopen(self, method, url, body=None, headers=None, retries=None,
554
556
try :
555
557
# Request a connection from the queue.
556
558
timeout_obj = self ._get_timeout (timeout )
557
- conn = self ._get_conn (timeout = pool_timeout )
559
+ conn = await self ._get_conn (timeout = pool_timeout )
558
560
559
561
conn .timeout = timeout_obj .connect_timeout
560
562
561
563
# Make the request on the base connection object.
562
- base_response = self ._make_request (conn , method , url ,
563
- timeout = timeout_obj ,
564
- body = body , headers = headers )
564
+ base_response = await self ._make_request (conn , method , url ,
565
+ timeout = timeout_obj ,
566
+ body = body ,
567
+ headers = headers )
565
568
566
569
# Pass method to Response for length checking
567
570
response_kw ['request_method' ] = method
@@ -615,22 +618,23 @@ def urlopen(self, method, url, body=None, headers=None, retries=None,
615
618
# to throw the connection away unless explicitly told not to.
616
619
# Close the connection, set the variable to None, and make sure
617
620
# we put the None back in the pool to avoid leaking it.
618
- conn = conn and conn .close ()
621
+ conn = conn and await conn .close ()
619
622
release_this_conn = True
620
623
621
624
if release_this_conn :
622
625
# Put the connection back to be reused. If the connection is
623
626
# expired then it will be None, which will get replaced with a
624
627
# fresh connection during _get_conn.
625
- self ._put_conn (conn )
628
+ await self ._put_conn (conn )
626
629
627
630
if not conn :
628
631
# Try again
629
632
log .warning ("Retrying (%r) after connection "
630
633
"broken by '%r': %s" , retries , err , url )
631
- return self .urlopen (method , url , body , headers , retries ,
632
- timeout = timeout , pool_timeout = pool_timeout ,
633
- body_pos = body_pos , ** response_kw )
634
+ return await self .urlopen (method , url , body , headers , retries ,
635
+ timeout = timeout ,
636
+ pool_timeout = pool_timeout ,
637
+ body_pos = body_pos , ** response_kw )
634
638
635
639
# Check if we should retry the HTTP response.
636
640
has_retry_after = bool (response .getheader ('Retry-After' ))
@@ -646,7 +650,7 @@ def urlopen(self, method, url, body=None, headers=None, retries=None,
646
650
return response
647
651
retries .sleep (response )
648
652
log .debug ("Retry: %s" , url )
649
- return self .urlopen (
653
+ return await self .urlopen (
650
654
method , url , body , headers ,
651
655
retries = retries , timeout = timeout , pool_timeout = pool_timeout ,
652
656
body_pos = body_pos , ** response_kw )
0 commit comments