Skip to content

Commit 436cadd

Browse files
authored
fix(server): improve caching accuracy of Date header (#3887)
`CachedDate` gets updated every second, but the current logic doesn't take into account milliseconds - if the application starts at `12:00:00.600`, hyper will report `date: ...T120000` up until `12:00:01.599`, which is overzealous. We can sidestep this by subtracing the nanoseconds part (which includes milliseconds) from the time passed to `CachedDate::update()`.
1 parent e11b2ad commit 436cadd

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/common/date.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cell::RefCell;
22
use std::fmt::{self, Write};
33
use std::str;
4-
use std::time::{Duration, SystemTime};
4+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
55

66
#[cfg(feature = "http2")]
77
use http::header::HeaderValue;
@@ -68,8 +68,13 @@ impl CachedDate {
6868
}
6969

7070
fn update(&mut self, now: SystemTime) {
71+
let nanos = now
72+
.duration_since(UNIX_EPOCH)
73+
.unwrap_or_default()
74+
.subsec_nanos();
75+
7176
self.render(now);
72-
self.next_update = now + Duration::new(1, 0);
77+
self.next_update = now + Duration::new(1, 0) - Duration::from_nanos(nanos as u64);
7378
}
7479

7580
fn render(&mut self, now: SystemTime) {

0 commit comments

Comments
 (0)