2222import urllib3
2323from urllib3 .util .retry import Retry
2424
25- from kubernetes .client .exceptions import ApiException , ApiValueError
26- from kubernetes .utils .retry import (
25+ from kubernetes .client ._retry import (
2726 is_retry_after_response ,
2827 on_retry_after_error ,
2928 retry_after_backoff ,
3029)
30+ from kubernetes .client .exceptions import ApiException , ApiValueError
3131
3232SUPPORTED_SOCKS_PROXIES = {"socks5" , "socks5h" , "socks4" , "socks4a" }
3333RESTResponseType = urllib3 .HTTPResponse
@@ -225,25 +225,27 @@ def request(
225225 read = _request_timeout [1 ]
226226 )
227227
228- client_go_retries = getattr (self .configuration , 'client_go_retries' , False )
229- request_retries = None
230- if client_go_retries :
231- request_retries = self ._urllib3_retries_without_status (
228+ client_go_read_retries = (
229+ method in ['GET' , 'HEAD' ]
230+ and getattr (self .configuration , 'client_go_retries' , False )
231+ )
232+ read_retries = None
233+ if client_go_read_retries :
234+ read_retries = self ._urllib3_retries_without_status (
232235 getattr (self .configuration , 'retries' , None ))
233236
234- def pool_request (* args , ** kwargs ):
235- if request_retries is not None :
236- kwargs ['retries' ] = request_retries
237- return self .pool_manager .request (* args , ** kwargs )
238-
239237 def read_request (check_retry_status = False ):
240- response = pool_request (
238+ kwargs = {}
239+ if read_retries is not None :
240+ kwargs ['retries' ] = read_retries
241+ response = self .pool_manager .request (
241242 method ,
242243 url ,
243244 fields = {},
244245 timeout = timeout ,
245246 headers = headers ,
246- preload_content = False
247+ preload_content = False ,
248+ ** kwargs
247249 )
248250 if check_retry_status :
249251 self ._raise_retry_after_response (response )
@@ -279,7 +281,7 @@ def read_request(check_retry_status=False):
279281 request_body = None
280282 if body is not None :
281283 request_body = json .dumps (body )
282- r = pool_request (
284+ r = self . pool_manager . request (
283285 method ,
284286 url ,
285287 body = request_body ,
@@ -288,7 +290,7 @@ def read_request(check_retry_status=False):
288290 preload_content = False
289291 )
290292 elif content_type == 'application/x-www-form-urlencoded' :
291- r = pool_request (
293+ r = self . pool_manager . request (
292294 method ,
293295 url ,
294296 fields = post_params ,
@@ -304,7 +306,7 @@ def read_request(check_retry_status=False):
304306 del headers ['Content-Type' ]
305307 # Ensures that dict objects are serialized
306308 post_params = [(a , json .dumps (b )) if isinstance (b , dict ) else (a ,b ) for a , b in post_params ]
307- r = pool_request (
309+ r = self . pool_manager . request (
308310 method ,
309311 url ,
310312 fields = post_params ,
@@ -317,7 +319,7 @@ def read_request(check_retry_status=False):
317319 # other content types than JSON when `body` argument is
318320 # provided in serialized form.
319321 elif isinstance (body , str ) or isinstance (body , bytes ):
320- r = pool_request (
322+ r = self . pool_manager . request (
321323 method ,
322324 url ,
323325 body = body ,
@@ -327,7 +329,7 @@ def read_request(check_retry_status=False):
327329 )
328330 elif headers ['Content-Type' ].startswith ('text/' ) and isinstance (body , bool ):
329331 request_body = "true" if body else "false"
330- r = pool_request (
332+ r = self . pool_manager . request (
331333 method ,
332334 url ,
333335 body = request_body ,
@@ -342,7 +344,7 @@ def read_request(check_retry_status=False):
342344 raise ApiException (status = 0 , reason = msg )
343345 # For `GET`, `HEAD`
344346 else :
345- if client_go_retries :
347+ if client_go_read_retries :
346348 backoff = retry_after_backoff (
347349 getattr (self .configuration , 'retries' , None ),
348350 getattr (self .configuration , 'client_go_retry_backoff' , None ),
@@ -373,12 +375,22 @@ def _raise_retry_after_response(cls, response):
373375 error = cls ._retry_after_error (response )
374376 if not is_retry_after_response (error ):
375377 return
376- try :
377- response .drain_conn ()
378- except Exception :
379- response .release_conn ()
378+ cls ._read_and_close_retry_response (response )
380379 raise error
381380
381+ @staticmethod
382+ def _read_and_close_retry_response (response ):
383+ try :
384+ try :
385+ content_length = int (response .getheaders ().get (
386+ 'Content-Length' , '-1' ))
387+ except (TypeError , ValueError ):
388+ content_length = - 1
389+ if content_length <= 2 << 10 :
390+ response .read (2 << 10 )
391+ finally :
392+ response .close ()
393+
382394 @staticmethod
383395 def _urllib3_retries_without_status (retries ):
384396 if retries is False :
0 commit comments