-
Notifications
You must be signed in to change notification settings - Fork 37
Remove chrono dependency and use transitive time dependency #63
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
base: main
Are you sure you want to change the base?
Conversation
Hi thanks for the PR. chrono might indeed be a bit unnecessary here. But before I remove it I would like to understand why you got so many extra indirect dependencies, because I don't see them in
|
I've fixed the conflicts in this PR. I've also used this in production for 1 cert renewal period (60 days) with no issues. I'm pretty sure the code as given is correct by this point. |
a9704ac
to
f8eee23
Compare
Closing for now, but thanks for the PR. chrono and time seem to be equally popular atm and the benefit of switching to time isn't obvious to me. If time gains significantly more popularity (which does seem somewhat likely), the maintenance situation changes, or some other more obvious benefit can be demonstrated, I'll reconsider. |
The benefit is just reducing dependencies, since there is no obvious need to use |
On current main
This seems no worse than However, on closer inspection it seems that rcgen also uses |
Yeah, the idea was to use the transitive pre-existing |
.max(chrono::Duration::zero()) | ||
.to_std() | ||
.unwrap_or_default(); | ||
let wait_duration = (validity[1] - (validity[1] - validity[0]) / 3i32 - OffsetDateTime::now_utc()).unsigned_abs(); |
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.
Let's make this a bit safer and more obvious while we are at it. Something like this:
let [valid_from, valid_until] = validity;
let total_valid_duration = valid_until.saturating_sub(valid_from).max(Duration::ZERO);
let renew_at = valid_until.saturating_sub(total_valid_duration/3);
let wait_duration = renew_at.saturating_sub(OffsetDateTime::now_utc());
self.wait = Some(Timer::after(wait_duration.abs()));
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.
As far as I know, this doesn't actually work because OffsetDateTime::saturating_sub
takes a Duration
but both arguments are OffsetDateTime
. The only way to get dt - dt
to produce a Duration
without using Sub
seems to be either reimplementing the logic within Sub
or just working with "instants" (i.e. converting the dt to nanoseconds since the UNIX epoch) and doing the subtraction that way. Note that this approach does not produce a Duration
but an i128
instead.
Another consideration that might make it easier to work with is whether nanosecond precision is necessary, since by using the regular second precision unix_timestamp
might be easier than dealing with an i128
.
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.
Working with i64 (seconds) for timestamps and durations seems fine.
It's a bit annoying that time
doesn't provide any safe "time between" function... If lhs > rhs
and the offsets are the same lhs - rhs
may be safe. But I find that difficult to confirm looking at the implementation.
The more I think about it, it seems like it may actually be easiest not to use time
here, convert to u64
immediately and use saturating operations on that.
While adding this as a dependency to my project, I noticed my dependency tree kind of blew up due to the inclusion of
chrono
(it added unrelated things related to wasm and Android, for example). In an effort to reduce my dependency count I decided to port over the limitedchrono
functionality in this repository to use the pre-existing transitivetime
dependency fromx509-parser
instead.I admit to not testing this code, but a look at the documentation, source code of both of these libraries, and running some
cargo check
to ensure this still compiles I believe the changes here are correct.