-
Notifications
You must be signed in to change notification settings - Fork 1k
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
refactor: Replace Instant with SystemTime for Records and related code #5856
base: master
Are you sure you want to change the base?
Conversation
And related code
@@ -43,7 +43,6 @@ use libp2p_swarm::{ | |||
}; | |||
use thiserror::Error; | |||
use tracing::Level; | |||
use web_time::Instant; |
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.
To my knowledge, SystemTime
is not available on wasm targets (it would panic when being used at runtime). In this case, we should use web_time::SystemTime
in its place, no?
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.
yeh that sounds like a better idea.
Thanks for the PR. Left a small comment. |
sorry, forgot to add the Cargo lock file in the last commit. |
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.
Hi Rishi, thanks for this!
it seems web_stime::SystemTime
also doesn't impl Serialize
.
Reading serde-rs/serde#1375 and not being familiar with the need use-case, do we need to Serialize
Record::expires
and ProviderRecord::expires
if not, we could use skip_serialize
. If we do, I think I'd rather go serde-rs/serde#1375 (comment).
Hmm, if a record is serialized and then kicked off memory then its expiry timestamp would be lost and when its bought back after deserialization, the expiry timestamp would be set to the default value, which breaks the intended behvaious that rely on the expiry field of the records (eg. garbage collecting expired records) |
However, I do think Chrono's UTC timestamp would give us wasam friendly timestamp which is also serializable |
I would be wrong but it does look like it impl |
Description
This PR refactors the record storage code to use
web_time::SystemTime
instead ofInstant
in record types (e.g. inProviderRecord
andRecord
). The primary motivation is thatInstant
isn’t serializable—making it unsuitable for persistent storage—whereasSystemTime
can be serialized by converting it to seconds since the Unix epoch. This approach was suggested in #4817While the non-monotonic nature of
SystemTime
(i.e. it can fail due to system clock adjustments) is a known trade-off, this is less problematic over short durations.Notes & Open Questions
SystemTime
is that its API returns aResult
when performing operations like duration calculations. In practice, this means there are edge cases where computing a duration might yield an error (for example, if the system clock has been adjusted unexpectedly). In contrast, Chrono’sDateTime
subtraction always produces a signedTimeDelta
(positive or negative) without error. I am open to rewriting this PR using Chrono if the reviewers agree with the rationale.SystemTime
operations.Change Checklist