3636 SansIOHTTPPolicy ,
3737 NetworkTraceLoggingPolicy ,
3838 HTTPPolicy ,
39- RequestHistory
39+ RequestHistory ,
40+ RetryPolicy
4041)
4142from azure .core .exceptions import AzureError , ServiceRequestError , ServiceResponseError
4243
@@ -353,18 +354,61 @@ def on_response(self, request, response):
353354 )
354355
355356
356- class StorageRetryPolicy ( HTTPPolicy ):
357+ class TablesRetryPolicy ( RetryPolicy ):
357358 """
358- The base class for Exponential and Linear retries containing shared code.
359+ A base class for retry policies for the Table Client and Table Service Client
359360 """
361+ def __init__ (
362+ self ,
363+ initial_backoff = 15 , # type: int
364+ increment_base = 3 , # type: int
365+ retry_total = 10 , # type: int
366+ retry_to_secondary = False , # type: bool
367+ random_jitter_range = 3 , # type: int
368+ ** kwargs # type: Any
369+ ):
370+ """
371+ Build a TablesRetryPolicy object.
360372
361- def __init__ (self , ** kwargs ):
362- self .total_retries = kwargs .pop ('retry_total' , 10 )
373+ :param int initial_backoff:
374+ The initial backoff interval, in seconds, for the first retry.
375+ :param int increment_base:
376+ The base, in seconds, to increment the initial_backoff by after the
377+ first retry.
378+ :param int retry_total: total number of retries
379+ :param bool retry_to_secondary:
380+ Whether the request should be retried to secondary, if able. This should
381+ only be enabled of RA-GRS accounts are used and potentially stale data
382+ can be handled.
383+ :param int random_jitter_range:
384+ A number in seconds which indicates a range to jitter/randomize for the back-off interval.
385+ For example, a random_jitter_range of 3 results in the back-off interval x to vary between x+3 and x-3.
386+ """
387+ self .initial_backoff = initial_backoff
388+ self .increment_base = increment_base
389+ self .random_jitter_range = random_jitter_range
390+ self .total_retries = retry_total
363391 self .connect_retries = kwargs .pop ('retry_connect' , 3 )
364392 self .read_retries = kwargs .pop ('retry_read' , 3 )
365393 self .status_retries = kwargs .pop ('retry_status' , 3 )
366- self .retry_to_secondary = kwargs .pop ('retry_to_secondary' , False )
367- super (StorageRetryPolicy , self ).__init__ ()
394+ self .retry_to_secondary = retry_to_secondary
395+ super (TablesRetryPolicy , self ).__init__ (** kwargs )
396+
397+ def get_backoff_time (self , settings ):
398+ """
399+ Calculates how long to sleep before retrying.
400+ :param dict settings:
401+ :keyword callable cls: A custom type or function that will be passed the direct response
402+ :return:
403+ An integer indicating how long to wait before retrying the request,
404+ or None to indicate no retry should be performed.
405+ :rtype: int or None
406+ """
407+ random_generator = random .Random ()
408+ backoff = self .initial_backoff + (0 if settings ['count' ] == 0 else pow (self .increment_base , settings ['count' ]))
409+ random_range_start = backoff - self .random_jitter_range if backoff > self .random_jitter_range else 0
410+ random_range_end = backoff + self .random_jitter_range
411+ return random_generator .uniform (random_range_start , random_range_end )
368412
369413 def _set_next_host_location (self , settings , request ): # pylint: disable=no-self-use
370414 """
@@ -384,7 +428,7 @@ def _set_next_host_location(self, settings, request): # pylint: disable=no-self
384428 updated = url ._replace (netloc = settings ['hosts' ].get (settings ['mode' ]))
385429 request .url = updated .geturl ()
386430
387- def configure_retries (self , request ): # pylint: disable=no-self-use
431+ def configure_retries (self , request ): # pylint: disable=no-self-use, arguments-differ
388432 # type: (...)-> dict
389433 """
390434 :param Any request:
@@ -414,17 +458,8 @@ def configure_retries(self, request): # pylint: disable=no-self-use
414458 'history' : []
415459 }
416460
417- def get_backoff_time (self , settings , ** kwargs ): # pylint: disable=unused-argument,no-self-use
418- """ Formula for computing the current backoff.
419- Should be calculated by child class.
420- :param Any settings:
421- :keyword callable cls: A custom type or function that will be passed the direct response
422- :rtype: float
423- """
424- return 0
425-
426- def sleep (self , settings , transport ):
427- # type: (...)->None
461+ def sleep (self , settings , transport ): # pylint: disable=arguments-differ
462+ # type: (...) -> None
428463 """
429464 :param Any settings:
430465 :param Any transport:
@@ -435,7 +470,7 @@ def sleep(self, settings, transport):
435470 return
436471 transport .sleep (backoff )
437472
438- def increment (self , settings , request , response = None , error = None , ** kwargs ): # pylint:disable=W0613
473+ def increment (self , settings , request , response = None , error = None , ** kwargs ): # pylint:disable=unused-argument, arguments-differ
439474 # type: (...)->None
440475 """Increment the retry counters.
441476
@@ -531,7 +566,7 @@ def send(self, request):
531566 return response
532567
533568
534- class ExponentialRetry (StorageRetryPolicy ):
569+ class ExponentialRetry (TablesRetryPolicy ):
535570 """Exponential retry."""
536571
537572 def __init__ (self , initial_backoff = 15 , increment_base = 3 , retry_total = 3 ,
@@ -565,10 +600,9 @@ def __init__(self, initial_backoff=15, increment_base=3, retry_total=3,
565600 super (ExponentialRetry , self ).__init__ (
566601 retry_total = retry_total , retry_to_secondary = retry_to_secondary , ** kwargs )
567602
568- def get_backoff_time (self , settings , ** kwargs ):
603+ def get_backoff_time (self , settings ):
569604 """
570605 Calculates how long to sleep before retrying.
571- :param **kwargs:
572606 :param dict settings:
573607 :keyword callable cls: A custom type or function that will be passed the direct response
574608 :return:
@@ -583,7 +617,7 @@ def get_backoff_time(self, settings, **kwargs):
583617 return random_generator .uniform (random_range_start , random_range_end )
584618
585619
586- class LinearRetry (StorageRetryPolicy ):
620+ class LinearRetry (TablesRetryPolicy ):
587621 """Linear retry."""
588622
589623 def __init__ (self , backoff = 15 , retry_total = 3 , retry_to_secondary = False , random_jitter_range = 3 , ** kwargs ):
@@ -608,7 +642,7 @@ def __init__(self, backoff=15, retry_total=3, retry_to_secondary=False, random_j
608642 super (LinearRetry , self ).__init__ (
609643 retry_total = retry_total , retry_to_secondary = retry_to_secondary , ** kwargs )
610644
611- def get_backoff_time (self , settings , ** kwargs ):
645+ def get_backoff_time (self , settings ):
612646 """
613647 Calculates how long to sleep before retrying.
614648
0 commit comments