Skip to content

Commit 1477061

Browse files
committed
refactor exponential backoff loop
1 parent 68ef37a commit 1477061

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

datacrunch/instances/instances.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import itertools
23
from typing import List, Union, Optional, Dict, Literal
34
from dataclasses import dataclass
45
from dataclasses_json import dataclass_json
@@ -178,9 +179,8 @@ def create(self,
178179
id = self._http_client.post(INSTANCES_ENDPOINT, json=payload).text
179180

180181
# Wait for instance to enter provisioning state with timeout
181-
interval = min(initial_interval, max_interval)
182182
deadline = time.monotonic() + max_wait_time
183-
while True:
183+
for i in itertools.count():
184184
instance = self.get_by_id(id)
185185
if instance.status != InstanceStatus.ORDERED:
186186
return instance
@@ -190,8 +190,8 @@ def create(self,
190190
raise TimeoutError(
191191
f"Instance {id} did not enter provisioning state within {max_wait_time:.1f} seconds")
192192

193-
time.sleep(min(interval, deadline - now))
194-
interval = min(interval * backoff_coefficient, max_interval)
193+
interval = min(initial_interval * backoff_coefficient ** i, max_interval, deadline - now)
194+
time.sleep(interval)
195195

196196
def action(self, id_list: Union[List[str], str], action: str, volume_ids: Optional[List[str]] = None) -> None:
197197
"""Performs an action on one or more instances.

0 commit comments

Comments
 (0)