-
Notifications
You must be signed in to change notification settings - Fork 10
instances.create: expose retry settings, add exponential backoff #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| pricing: Optional[Pricing] = None, | ||
| coupon: Optional[str] = None) -> Instance: | ||
| coupon: Optional[str] = None, | ||
| *, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* is a keyword-only argument marker, probably a good idea to do it for all new arguments.
datacrunch/instances/instances.py
Outdated
| # Wait for instance to enter provisioning state with timeout | ||
| MAX_WAIT_TIME = 60 # Maximum wait time in seconds | ||
| POLL_INTERVAL = 0.5 # Time between status checks | ||
|
|
||
| start_time = time.time() | ||
| interval = min(initial_interval, max_interval) | ||
| deadline = time.monotonic() + max_wait_time | ||
| while True: | ||
| instance = self.get_by_id(id) | ||
| if instance.status != InstanceStatus.ORDERED: | ||
| return instance | ||
|
|
||
| if time.time() - start_time > MAX_WAIT_TIME: | ||
| now = time.monotonic() | ||
| if now >= deadline: | ||
| raise TimeoutError( | ||
| f"Instance {id} did not enter provisioning state within {MAX_WAIT_TIME} seconds") | ||
| f"Instance {id} did not enter provisioning state within {max_wait_time:.1f} seconds") | ||
|
|
||
| time.sleep(POLL_INTERVAL) | ||
| time.sleep(min(interval, deadline - now)) | ||
| interval = min(interval * backoff_coefficient, max_interval) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a big change, but it creates less variables and I guess it's a bit easier to read:
import itertools # ← at the top
# Wait for instance to enter provisioning state with timeout
start_time = time.monotonic()
for i in itertools.count():
instance = self.get_by_id(id)
if instance.status != InstanceStatus.ORDERED:
return instance
time_left = max_wait_time - (time.monotonic() - start_time)
if time_left < 0:
raise TimeoutError(
f"Instance {id} did not enter provisioning state within {max_wait_time:.1f} seconds")
interval = initial_interval * backoff_coefficient ** i
time.sleep(min(interval, max_interval, time_left))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I've used some ideas from your version.
98d562d to
68ef37a
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #48 +/- ##
==========================================
- Coverage 87.98% 87.36% -0.62%
==========================================
Files 21 21
Lines 1124 1148 +24
==========================================
+ Hits 989 1003 +14
- Misses 135 145 +10 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
No description provided.