-
Notifications
You must be signed in to change notification settings - Fork 176
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
fix: time difference calculations for new_tps #2655
Conversation
WalkthroughOhayo! This pull request introduces a modification in the Changes
Assessment against linked issues
Possibly related PRs
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
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.
Actionable comments posted: 1
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (1)
crates/torii/core/src/executor.rs
(2 hunks)
🔇 Additional comments (1)
crates/torii/core/src/executor.rs (1)
4-4
: LGTM! Necessary imports added.
The addition of SystemTime
and UNIX_EPOCH
imports is appropriate for the time-based calculations.
let current_time = | ||
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); | ||
|
||
num_transactions / (current_time - cursor_timestamp) |
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.
Ohayo sensei! The time calculation needs safer handling.
While the change addresses the overflow panic, there are several concerns with the current implementation:
- Using
unwrap()
onduration_since
could panic if the system clock is set before UNIX_EPOCH - The time difference could still be zero if system time equals cursor_timestamp
- The calculation might be incorrect if system time is less than cursor_timestamp
Here's a safer implementation:
- let current_time =
- SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
-
- num_transactions / (current_time - cursor_timestamp)
+ let current_time = SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .unwrap_or_default()
+ .as_secs();
+
+ if current_time <= cursor_timestamp {
+ // Fallback to 1 second if timestamps are invalid
+ num_transactions
+ } else {
+ num_transactions / (current_time - cursor_timestamp)
+ }
This implementation:
- Uses
unwrap_or_default()
to handle duration_since errors - Handles the case where system time ≤ cursor_timestamp
- Provides a reasonable fallback for TPS calculation
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
let current_time = | |
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); | |
num_transactions / (current_time - cursor_timestamp) | |
let current_time = SystemTime::now() | |
.duration_since(UNIX_EPOCH) | |
.unwrap_or_default() | |
.as_secs(); | |
if current_time <= cursor_timestamp { | |
// Fallback to 1 second if timestamps are invalid | |
num_transactions | |
} else { | |
num_transactions / (current_time - cursor_timestamp) | |
} |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2655 +/- ##
==========================================
- Coverage 57.46% 57.43% -0.03%
==========================================
Files 400 400
Lines 49963 49965 +2
==========================================
- Hits 28709 28698 -11
- Misses 21254 21267 +13 ☔ View full report in Codecov by Sentry. |
Description
Fixes panic
attempt to subtract with overflow
Related issue
Closes #2654
Summary by CodeRabbit
New Features
Bug Fixes