Skip to content

Commit 4ba28b8

Browse files
authored
chore: don't use Utc::now() (#12)
1 parent be0dc25 commit 4ba28b8

File tree

10 files changed

+69
-68
lines changed

10 files changed

+69
-68
lines changed

Cargo.lock

Lines changed: 2 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ denokv_proto = { version = "0.2.0", path = "./proto" }
1414
anyhow = "1"
1515
async-trait = "0.1"
1616
bytes = "1"
17-
chrono = { version = "0.4", features = ["serde"] }
17+
chrono = { version = "0.4", default-features = false, features = ["std", "serde"] }
1818
futures = "0.3.28"
1919
log = "0.4.20"
2020
num-bigint = "0.4"

proto/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
2+
13
mod codec;
24
mod interface;
35
mod protobuf;

remote/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "denokv_remote"
33
description = "Remote (KV Connect) backend for Deno KV"
4-
version = "0.2.2"
4+
version = "0.2.3"
55
edition.workspace = true
66
license.workspace = true
77
repository.workspace = true

remote/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
2+
3+
mod time;
4+
15
use std::ops::Sub;
26
use std::sync::Arc;
37
use std::time::Duration;
@@ -25,6 +29,7 @@ use log::warn;
2529
use rand::Rng;
2630
use reqwest::StatusCode;
2731
use serde::Deserialize;
32+
use time::utc_now;
2833
use tokio::sync::watch;
2934
use tokio::task::JoinHandle;
3035
use url::Url;
@@ -231,15 +236,15 @@ async fn metadata_refresh_task(
231236
match res {
232237
RetryableResult::Ok(metadata) => {
233238
attempts = 0;
234-
let expires_in = metadata.expires_at.signed_duration_since(Utc::now());
239+
let expires_in = metadata.expires_at.signed_duration_since(utc_now());
235240

236241
if tx.send(MetadataState::Ok(Arc::new(metadata))).is_err() {
237242
// The receiver has been dropped, so we can stop now.
238243
return;
239244
}
240245

241-
// Sleep until the token expires, minus a 10 minute buffer, but at minimum
242-
// one minute.
246+
// Sleep until the token expires, minus a 10 minute buffer, but at
247+
// minimum one minute.
243248
let sleep_time = expires_in
244249
.sub(chrono::Duration::seconds(10))
245250
.to_std()

remote/time.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
2+
3+
/// Identical to chrono::Utc::now() but without the system "clock"
4+
/// feature flag.
5+
///
6+
/// The "clock" feature flag pulls in the "iana-time-zone" crate
7+
/// which links to macOS's "CoreFoundation" framework which increases
8+
/// startup time for the CLI.
9+
pub fn utc_now() -> chrono::DateTime<chrono::Utc> {
10+
let now = std::time::SystemTime::now()
11+
.duration_since(std::time::UNIX_EPOCH)
12+
.expect("system time before Unix epoch");
13+
let naive = chrono::NaiveDateTime::from_timestamp_opt(
14+
now.as_secs() as i64,
15+
now.subsec_nanos(),
16+
)
17+
.unwrap();
18+
chrono::DateTime::from_naive_utc_and_offset(naive, chrono::Utc)
19+
}

sqlite/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "denokv_sqlite"
33
description = "SQLite storage backend for Deno KV"
4-
version = "0.2.0"
4+
version = "0.2.1"
55
edition.workspace = true
66
license.workspace = true
77
repository.workspace = true

sqlite/backend.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
2+
13
use std::collections::HashSet;
24
use std::sync::Arc;
35
use std::time::Duration;
@@ -24,6 +26,8 @@ use rusqlite::OptionalExtension;
2426
use rusqlite::Transaction;
2527
use uuid::Uuid;
2628

29+
use crate::time::utc_now;
30+
2731
const STATEMENT_INC_AND_GET_DATA_VERSION: &str =
2832
"update data_version set version = version + ? where k = 0 returning version";
2933
const STATEMENT_KV_RANGE_SCAN: &str =
@@ -343,7 +347,7 @@ impl SqliteBackend {
343347

344348
pub fn queue_running_keepalive(&mut self) -> Result<(), anyhow::Error> {
345349
let running_messages = self.messages_running.clone();
346-
let now = Utc::now();
350+
let now = utc_now();
347351
self.run_tx(|tx, _| {
348352
let mut update_deadline_stmt =
349353
tx.prepare_cached(STATEMENT_QUEUE_UPDATE_RUNNING_DEADLINE)?;
@@ -360,7 +364,7 @@ impl SqliteBackend {
360364
}
361365

362366
pub fn queue_cleanup(&mut self) -> Result<(), anyhow::Error> {
363-
let now = Utc::now();
367+
let now = utc_now();
364368
let queue_dequeue_waker = self.dequeue_notify.clone();
365369
loop {
366370
let done = self.run_tx(|tx, rng| {
@@ -389,7 +393,7 @@ impl SqliteBackend {
389393
&mut self,
390394
) -> Result<(Option<DequeuedMessage>, Option<DateTime<Utc>>), anyhow::Error>
391395
{
392-
let now = Utc::now();
396+
let now = utc_now();
393397

394398
let can_dispatch = self.messages_running.len() < DISPATCH_CONCURRENCY_LIMIT;
395399

@@ -476,7 +480,7 @@ impl SqliteBackend {
476480
id: &QueueMessageId,
477481
success: bool,
478482
) -> Result<(), anyhow::Error> {
479-
let now = Utc::now();
483+
let now = utc_now();
480484
let requeued = self.run_tx(|tx, rng| {
481485
let requeued = if success {
482486
let changed = tx
@@ -498,7 +502,7 @@ impl SqliteBackend {
498502
pub fn collect_expired(
499503
&mut self,
500504
) -> Result<Option<DateTime<Utc>>, anyhow::Error> {
501-
let now = Utc::now();
505+
let now = utc_now();
502506
self.run_tx(|tx, _| {
503507
tx.prepare_cached(STATEMENT_DELETE_ALL_EXPIRED)?
504508
.execute(params![now.timestamp_millis()])?;

sqlite/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
2+
13
mod backend;
4+
mod time;
25

36
use std::collections::VecDeque;
47
use std::ops::Add;
@@ -24,6 +27,7 @@ use denokv_proto::SnapshotReadOptions;
2427
use futures::future::Either;
2528
use rand::RngCore;
2629
pub use rusqlite::Connection;
30+
use time::utc_now;
2731
use tokio::select;
2832
use tokio::sync::oneshot;
2933
use tokio::sync::Notify;
@@ -109,7 +113,7 @@ async fn sqlite_thread(
109113
dequeue_notify: Arc<Notify>,
110114
mut request_rx: tokio::sync::mpsc::Receiver<SqliteRequest>,
111115
) {
112-
let start = Utc::now();
116+
let start = utc_now();
113117
if let Err(err) = backend.queue_cleanup() {
114118
panic!("KV queue cleanup failed: {err}");
115119
}
@@ -138,7 +142,7 @@ async fn sqlite_thread(
138142
EXPIRY_JITTER,
139143
);
140144
loop {
141-
let now = Utc::now();
145+
let now = utc_now();
142146
let mut closest_deadline = queue_cleanup_deadline
143147
.min(queue_keepalive_deadline)
144148
.min(expiry_deadline);
@@ -245,7 +249,7 @@ fn compute_deadline_with_max_and_jitter(
245249
max: Duration,
246250
jitter_duration: Duration,
247251
) -> DateTime<Utc> {
248-
let fallback = Utc::now() + max;
252+
let fallback = utc_now() + max;
249253
next_ready.unwrap_or(fallback).min(fallback)
250254
+ duration_with_total_jitter(jitter_duration)
251255
}

sqlite/time.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
2+
3+
/// Identical to chrono::Utc::now() but without the system "clock"
4+
/// feature flag.
5+
///
6+
/// The "clock" feature flag pulls in the "iana-time-zone" crate
7+
/// which links to macOS's "CoreFoundation" framework which increases
8+
/// startup time for the CLI.
9+
pub fn utc_now() -> chrono::DateTime<chrono::Utc> {
10+
let now = std::time::SystemTime::now()
11+
.duration_since(std::time::UNIX_EPOCH)
12+
.expect("system time before Unix epoch");
13+
let naive = chrono::NaiveDateTime::from_timestamp_opt(
14+
now.as_secs() as i64,
15+
now.subsec_nanos(),
16+
)
17+
.unwrap();
18+
chrono::DateTime::from_naive_utc_and_offset(naive, chrono::Utc)
19+
}

0 commit comments

Comments
 (0)