mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#7136 Increase time out when launching from python.
Added truncated exponential backoff policy to have shorter delays initially, and longer delay after repeated failures.
This commit is contained in:
@@ -21,7 +21,7 @@ from Definitions_pb2 import Empty
|
||||
import RiaVersionInfo
|
||||
|
||||
from .project import Project
|
||||
from .retry_policy import FixedRetryPolicy
|
||||
from .retry_policy import ExponentialBackoffRetryPolicy
|
||||
|
||||
class Instance:
|
||||
"""The ResInsight Instance class. Use to launch or find existing ResInsight instances
|
||||
@@ -183,7 +183,7 @@ class Instance:
|
||||
connection_ok = False
|
||||
version_ok = False
|
||||
|
||||
retry_policy = FixedRetryPolicy()
|
||||
retry_policy = ExponentialBackoffRetryPolicy()
|
||||
if self.launched:
|
||||
for num_tries in range(0, retry_policy.num_retries()):
|
||||
connection_ok, version_ok = self.__check_version()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import abc
|
||||
|
||||
import time
|
||||
import random
|
||||
|
||||
|
||||
class RetryPolicy(abc.ABC):
|
||||
@@ -47,3 +48,36 @@ class FixedRetryPolicy(RetryPolicy):
|
||||
|
||||
def num_retries(self):
|
||||
return self.max_num_retries
|
||||
|
||||
|
||||
class ExponentialBackoffRetryPolicy(RetryPolicy):
|
||||
def __init__(self, min_backoff=200, max_backoff=10000, max_num_retries=20):
|
||||
"""
|
||||
Create a truncated exponential backoff policy.
|
||||
See: https://en.wikipedia.org/wiki/Exponential_backoff
|
||||
:param min_backoff: minimum time to sleep in milliseconds.
|
||||
:param max_backoff: maximum time to sleep in milliseconds.
|
||||
:param max_num_retries: max number of retries.
|
||||
"""
|
||||
self.min_backoff = min_backoff
|
||||
self.max_backoff = max_backoff
|
||||
self.max_num_retries = max_num_retries
|
||||
self.multiplier = 2
|
||||
|
||||
def sleep(self, retry_num):
|
||||
# Add a random component to avoid synchronized retries
|
||||
wiggle = random.randint(0, 100)
|
||||
sleep_ms = min(
|
||||
self.min_backoff + self.multiplier ** retry_num + wiggle, self.max_backoff
|
||||
)
|
||||
time.sleep(sleep_ms / 1000)
|
||||
|
||||
def time_out_message(self):
|
||||
return (
|
||||
"Tried {} times with increasing delay (from {} to {} milliseconds).".format(
|
||||
self.max_num_retries, self.min_backoff, self.max_backoff
|
||||
)
|
||||
)
|
||||
|
||||
def num_retries(self):
|
||||
return self.max_num_retries
|
||||
|
||||
Reference in New Issue
Block a user