fix: use monotonic clock for elapsed time and handle nil max_elapsed_time#122
Merged
fix: use monotonic clock for elapsed time and handle nil max_elapsed_time#122
Conversation
…time - Replace Time.now with Process.clock_gettime(CLOCK_MONOTONIC) so retry timing is immune to wall-clock adjustments (NTP, manual changes). - Guard can_retry? against max_elapsed_time: nil, which previously raised NoMethodError on the comparison path. - Remove dead '* 1.0' in ExponentialBackoff#randomize (rand_factor is already a float). - Add regression tests for both fixes.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the reliability and configurability of Retriable’s retry loop by switching elapsed-time tracking to a monotonic clock and making max_elapsed_time nil-safe to avoid runtime crashes during retries.
Changes:
- Use
Process.clock_gettime(Process::CLOCK_MONOTONIC)instead ofTime.nowfor elapsed-time measurement. - Treat
max_elapsed_time: nilas “no elapsed-time limit” in retry eligibility checks. - Simplify exponential backoff randomization math by removing a float-coercion multiplier.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| spec/retriable_spec.rb | Adds regression specs for max_elapsed_time: nil and monotonic clock usage. |
| lib/retriable/exponential_backoff.rb | Removes * 1.0 from randomization calculation and minor expression tweak. |
| lib/retriable.rb | Switches elapsed-time tracking to monotonic clock; makes can_retry? nil-safe for max_elapsed_time. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
… max_elapsed_time
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Monotonic clock: Replace
Time.nowwithProcess.clock_gettime(CLOCK_MONOTONIC)for elapsed-time tracking in the retry loop. Wall-clock time (Time.now) is susceptible to NTP adjustments and manual system clock changes, which can cause retries to stop early or run too long. Monotonic clock is the correct primitive for measuring durations.Nil-safe
max_elapsed_time: Guardcan_retry?so thatmax_elapsed_time: nilmeans "no time limit" instead of raisingNoMethodErroron the<=comparison. This is a runtime crash that only manifests after a failure+retry, making it easy to miss in testing.Remove dead
* 1.0: Therandomizemethod inExponentialBackoffmultiplied by1.0unnecessarily —rand_factoris already a float.Testing
max_elapsed_time: nilretries up totrieslimit, one verifyingProcess.clock_gettime(CLOCK_MONOTONIC)is called during retries.