From 29e7819e764b9395836ae11df7a71388209556f6 Mon Sep 17 00:00:00 2001 From: icycrystal4 Date: Sun, 6 Nov 2022 20:59:38 +0800 Subject: [PATCH] =?UTF-8?q?time:=20=E8=87=AA=E5=AE=9A=E4=B9=89ds::time?= =?UTF-8?q?=EF=BC=8C=E5=90=8E=E7=BB=AD=E6=9B=BF=E6=8D=A2std::time=E3=80=82?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=BD=BF=E7=94=A8std::time?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent/src/main.rs | 2 +- agent/src/prometheus.rs | 4 ++-- agent/src/service.rs | 2 +- context/Cargo.toml | 1 + context/src/lib.rs | 4 ++-- context/src/quadruple.rs | 2 +- discovery/src/cfg.rs | 2 +- discovery/src/dns.rs | 2 +- discovery/src/update.rs | 2 +- ds/src/mem/policy.rs | 4 ++-- endpoint/src/cacheservice/config.rs | 2 +- endpoint/src/cacheservice/topo.rs | 2 +- endpoint/src/lib.rs | 2 +- endpoint/src/phantomservice/config.rs | 2 +- endpoint/src/redisservice/config.rs | 2 +- endpoint/src/redisservice/topo.rs | 2 +- metrics/src/macros.rs | 2 +- metrics/src/register.rs | 2 +- metrics/src/sender.rs | 2 +- metrics/src/types/host.rs | 2 +- metrics/src/types/rtt.rs | 2 +- protocol/src/callback.rs | 2 +- protocol/src/error.rs | 2 +- protocol/src/lib.rs | 2 +- protocol/src/req.rs | 2 +- protocol/src/request.rs | 2 +- rt/src/entry.rs | 2 +- stream/src/builder.rs | 2 +- stream/src/checker.rs | 2 +- stream/src/pipeline.rs | 2 +- stream/src/reconn.rs | 2 +- tests/src/asserts.rs | 4 ++++ tests/src/mem.rs | 2 +- tests/src/ring_buffer.rs | 6 +++--- 34 files changed, 42 insertions(+), 37 deletions(-) create mode 100644 tests/src/asserts.rs diff --git a/agent/src/main.rs b/agent/src/main.rs index d6939422b..948f0f734 100644 --- a/agent/src/main.rs +++ b/agent/src/main.rs @@ -13,7 +13,7 @@ use discovery::*; mod init; use rt::spawn; -use std::time::Duration; +use ds::time::Duration; use protocol::Result; diff --git a/agent/src/prometheus.rs b/agent/src/prometheus.rs index 80f083a5d..19218393d 100644 --- a/agent/src/prometheus.rs +++ b/agent/src/prometheus.rs @@ -19,7 +19,7 @@ pub struct PrometheusMetricsResponse {} use ds::lock::Lock; use lazy_static::lazy_static; -use std::time::Instant; +use ds::time::Instant; lazy_static! { static ref LAST: Lock = Instant::now().into(); @@ -71,7 +71,7 @@ pub(crate) fn register_target(ctx: &context::Context) { port ); let client = reqwest::Client::new(); - let mut interval = tokio::time::interval(std::time::Duration::from_secs(60)); + let mut interval = tokio::time::interval(ds::time::Duration::from_secs(60)); let mut q = vec![("refresh", true)]; loop { let body = body.clone(); diff --git a/agent/src/service.rs b/agent/src/service.rs index 62a74314d..38d6479c3 100644 --- a/agent/src/service.rs +++ b/agent/src/service.rs @@ -2,7 +2,7 @@ use context::Quadruple; use net::Listener; use rt::spawn; use std::sync::Arc; -use std::time::Duration; +use ds::time::Duration; use discovery::TopologyWriteGuard; use ds::chan::Sender; diff --git a/context/Cargo.toml b/context/Cargo.toml index 87d9a267c..0a64f621b 100644 --- a/context/Cargo.toml +++ b/context/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] log = { path = "../log" } +ds = { path = "../ds" } clap = {version = "3.1.1", features = ["derive"] } url = "2.2.2" diff --git a/context/src/lib.rs b/context/src/lib.rs index 512679203..5f14e9234 100644 --- a/context/src/lib.rs +++ b/context/src/lib.rs @@ -134,9 +134,9 @@ impl ContextOption { Ok(()) } - pub fn tick(&self) -> std::time::Duration { + pub fn tick(&self) -> ds::time::Duration { assert!(self.tick_sec >= 1 && self.tick_sec <= 60); - std::time::Duration::from_secs(self.tick_sec as u64) + ds::time::Duration::from_secs(self.tick_sec as u64) } // 如果是以升级模式启动,则会将原有的端口先关闭。 pub fn listeners(&self) -> ListenerIter { diff --git a/context/src/quadruple.rs b/context/src/quadruple.rs index 12c741076..00a9128d5 100644 --- a/context/src/quadruple.rs +++ b/context/src/quadruple.rs @@ -1,5 +1,5 @@ use std::path::Path; -use std::time::{Duration, Instant}; +use ds::time::{Duration, Instant}; #[derive(Debug, Clone, Eq)] pub struct Quadruple { parsed_at: Instant, diff --git a/discovery/src/cfg.rs b/discovery/src/cfg.rs index 07422397e..fb9396d00 100644 --- a/discovery/src/cfg.rs +++ b/discovery/src/cfg.rs @@ -1,6 +1,6 @@ use std::io::{Error, ErrorKind, Result}; use std::path::PathBuf; -use std::time::{Duration, Instant}; +use ds::time::{Duration, Instant}; use tokio::fs::File; use tokio::io::{AsyncReadExt, AsyncWriteExt}; diff --git a/discovery/src/dns.rs b/discovery/src/dns.rs index 5329618a7..6ae7f6208 100644 --- a/discovery/src/dns.rs +++ b/discovery/src/dns.rs @@ -166,7 +166,7 @@ pub async fn start_dns_resolver_refresher() { use std::task::Poll; let noop = noop_waker::noop_waker(); let mut ctx = std::task::Context::from_waker(&noop); - use std::time::{Duration, Instant}; + use ds::time::{Duration, Instant}; const CYCLE: Duration = Duration::from_secs(79); let mut tick = tokio::time::interval(Duration::from_secs(1)); let mut last = Instant::now(); // 上一次刷新的时间 diff --git a/discovery/src/update.rs b/discovery/src/update.rs index e93f61e60..5b91fa479 100644 --- a/discovery/src/update.rs +++ b/discovery/src/update.rs @@ -1,7 +1,7 @@ // 定期更新discovery. use super::{Discover, ServiceId, TopologyWrite}; use ds::chan::Receiver; -use std::time::{Duration, Instant}; +use ds::time::{Duration, Instant}; use tokio::time::interval; use crate::cache::DiscoveryCache; diff --git a/ds/src/mem/policy.rs b/ds/src/mem/policy.rs index 8ab5acf8f..0e5f081c2 100644 --- a/ds/src/mem/policy.rs +++ b/ds/src/mem/policy.rs @@ -1,5 +1,5 @@ const BUF_MIN: usize = 2 * 1024; -use std::time::Instant; +use crate::time::Instant; // 内存需要缩容时的策略 // 为了避免频繁的缩容,需要设置一个最小频繁,通常使用最小间隔时间 pub struct MemPolicy { @@ -108,8 +108,8 @@ impl Display for MemPolicy { #[cfg(debug_assertions)] mod trace { + use crate::time::Instant; use std::fmt::{self, Debug, Formatter}; - use std::time::Instant; pub(super) struct Trace { direction: &'static str, // 方向: true为tx, false为rx. 打日志用 id: usize, diff --git a/endpoint/src/cacheservice/config.rs b/endpoint/src/cacheservice/config.rs index 507017e0b..bcac2754b 100644 --- a/endpoint/src/cacheservice/config.rs +++ b/endpoint/src/cacheservice/config.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; use sharding::hash; -//use std::time::Duration; +//use ds::time::Duration; #[derive(Serialize, Deserialize, Clone, Debug, Default, Hash)] pub struct Namespace { diff --git a/endpoint/src/cacheservice/topo.rs b/endpoint/src/cacheservice/topo.rs index de8d6a1a2..ca65aba74 100644 --- a/endpoint/src/cacheservice/topo.rs +++ b/endpoint/src/cacheservice/topo.rs @@ -3,7 +3,7 @@ use protocol::{Builder, Endpoint, Protocol, Request, Resource, Topology, TryNext use sharding::hash::Hasher; use sharding::Distance; use std::collections::HashMap; -use std::time::Duration; +use ds::time::Duration; use crate::TimeoutAdjust; use stream::Shards; diff --git a/endpoint/src/lib.rs b/endpoint/src/lib.rs index aed988578..2d4bdd7c8 100644 --- a/endpoint/src/lib.rs +++ b/endpoint/src/lib.rs @@ -24,7 +24,7 @@ trait TimeoutAdjust: Sized { } } -use std::time::Duration; +use ds::time::Duration; impl TimeoutAdjust for Duration { fn adjust(&mut self, ms: u32) { if ms > 0 { diff --git a/endpoint/src/phantomservice/config.rs b/endpoint/src/phantomservice/config.rs index ebf8423ff..fa1632ff6 100644 --- a/endpoint/src/phantomservice/config.rs +++ b/endpoint/src/phantomservice/config.rs @@ -1,4 +1,4 @@ -//use std::time::Duration; +//use ds::time::Duration; use serde::{Deserialize, Serialize}; diff --git a/endpoint/src/redisservice/config.rs b/endpoint/src/redisservice/config.rs index 111dfeaa2..ab549d57b 100644 --- a/endpoint/src/redisservice/config.rs +++ b/endpoint/src/redisservice/config.rs @@ -1,5 +1,5 @@ use std::collections::{HashMap, HashSet}; -//use std::time::Duration; +//use ds::time::Duration; use serde::{Deserialize, Serialize}; //use sharding::distribution::{DIST_ABS_MODULA, DIST_MODULA}; diff --git a/endpoint/src/redisservice/topo.rs b/endpoint/src/redisservice/topo.rs index cdc1e17d8..cab5d67da 100644 --- a/endpoint/src/redisservice/topo.rs +++ b/endpoint/src/redisservice/topo.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; -use std::time::Duration; +use ds::time::Duration; use discovery::TopologyWrite; use protocol::{Builder, Endpoint, Protocol, Request, Resource, Single, Topology}; diff --git a/metrics/src/macros.rs b/metrics/src/macros.rs index 36e7fdca1..735ab80ef 100644 --- a/metrics/src/macros.rs +++ b/metrics/src/macros.rs @@ -42,7 +42,7 @@ impl MetricData for T { crate::register_cache(id, self.int()); } } -use std::time::Duration; +use ds::time::Duration; impl MetricData for Duration { #[inline] fn incr_to(self, data: &ItemData) { diff --git a/metrics/src/register.rs b/metrics/src/register.rs index 8a94c8271..59a683da2 100644 --- a/metrics/src/register.rs +++ b/metrics/src/register.rs @@ -168,7 +168,7 @@ pub struct MetricRegister { impl MetricRegister { fn new(rx: Receiver<(Arc, i64)>, metrics: CowWriteHandle) -> Self { - let mut tick = interval(std::time::Duration::from_secs(3)); + let mut tick = interval(ds::time::Duration::from_secs(3)); tick.set_missed_tick_behavior(MissedTickBehavior::Skip); Self { rx, diff --git a/metrics/src/sender.rs b/metrics/src/sender.rs index 109c83fb2..8ba291674 100644 --- a/metrics/src/sender.rs +++ b/metrics/src/sender.rs @@ -2,7 +2,7 @@ //use std::future::Future; //use std::pin::Pin; //use std::task::{Context, Poll}; -//use std::time::{Duration, Instant}; +//use ds::time::{Duration, Instant}; // //use std::task::ready; //use tokio::time::{interval, Interval, MissedTickBehavior}; diff --git a/metrics/src/types/host.rs b/metrics/src/types/host.rs index a009a4560..2b425d944 100644 --- a/metrics/src/types/host.rs +++ b/metrics/src/types/host.rs @@ -2,7 +2,7 @@ use psutil::process::Process; use crate::BASE_PATH; use std::sync::atomic::{AtomicI64, AtomicUsize, Ordering}; -use std::time::Instant; +use ds::time::Instant; static CPU_PERCENT: AtomicUsize = AtomicUsize::new(0); static MEMORY: AtomicI64 = AtomicI64::new(0); diff --git a/metrics/src/types/rtt.rs b/metrics/src/types/rtt.rs index 203e40418..45169fb12 100644 --- a/metrics/src/types/rtt.rs +++ b/metrics/src/types/rtt.rs @@ -1,4 +1,4 @@ -use std::time::Duration; +use ds::time::Duration; use crate::{Id, ItemWriter, NumberInner}; pub const MAX: Duration = Duration::from_millis(30); diff --git a/protocol/src/callback.rs b/protocol/src/callback.rs index 3e6ab9d9d..7bbc9a4de 100644 --- a/protocol/src/callback.rs +++ b/protocol/src/callback.rs @@ -1,6 +1,6 @@ use std::mem::MaybeUninit; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; -use std::time::Instant; +use ds::time::Instant; use ds::AtomicWaker; diff --git a/protocol/src/error.rs b/protocol/src/error.rs index 1449ce1c0..e6fce7be8 100644 --- a/protocol/src/error.rs +++ b/protocol/src/error.rs @@ -1,4 +1,4 @@ -use std::time::Duration; +use ds::time::Duration; #[derive(Debug)] pub enum Error { diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index c4756e8ef..33225df75 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -47,7 +47,7 @@ impl Resource { } } -use std::time::Duration; +use ds::time::Duration; pub trait Builder { fn build(addr: &str, parser: P, rsrc: Resource, service: &str, timeout: Duration) -> E where diff --git a/protocol/src/req.rs b/protocol/src/req.rs index 2fe9cf90e..329dead4c 100644 --- a/protocol/src/req.rs +++ b/protocol/src/req.rs @@ -1,5 +1,5 @@ use std::fmt::{Debug, Display}; -use std::time::Instant; +use ds::time::Instant; use ds::RingSlice; diff --git a/protocol/src/request.rs b/protocol/src/request.rs index cf98d239c..2d45a76dd 100644 --- a/protocol/src/request.rs +++ b/protocol/src/request.rs @@ -7,7 +7,7 @@ pub struct Request { impl crate::Request for Request { #[inline] - fn start_at(&self) -> std::time::Instant { + fn start_at(&self) -> ds::time::Instant { self.ctx().start_at() } diff --git a/rt/src/entry.rs b/rt/src/entry.rs index dc66eb361..0f1b94a60 100644 --- a/rt/src/entry.rs +++ b/rt/src/entry.rs @@ -2,7 +2,7 @@ use std::fmt::Debug; use std::future::Future; use std::pin::Pin; use std::task::{Context, Poll}; -use std::time::{Duration, Instant}; +use ds::time::{Duration, Instant}; use metrics::{Metric, Path}; use std::task::ready; diff --git a/stream/src/builder.rs b/stream/src/builder.rs index 9c2f135a2..18d9cbccc 100644 --- a/stream/src/builder.rs +++ b/stream/src/builder.rs @@ -17,7 +17,7 @@ pub struct BackendBuilder { _marker: std::marker::PhantomData<(P, R)>, } -use std::time::Duration; +use ds::time::Duration; impl protocol::Builder>> for BackendBuilder { fn build( addr: &str, diff --git a/stream/src/checker.rs b/stream/src/checker.rs index 0a96beaec..2a23533da 100644 --- a/stream/src/checker.rs +++ b/stream/src/checker.rs @@ -1,5 +1,5 @@ use std::sync::{atomic::AtomicBool, Arc}; -use std::time::Duration; +use ds::time::Duration; use tokio::net::TcpStream; use tokio::time::timeout; diff --git a/stream/src/pipeline.rs b/stream/src/pipeline.rs index 64e4e18dc..e280e139f 100644 --- a/stream/src/pipeline.rs +++ b/stream/src/pipeline.rs @@ -3,7 +3,7 @@ use std::future::Future; use std::pin::Pin; use std::sync::atomic::{AtomicUsize, Ordering::*}; use std::task::{ready, Context, Poll}; -use std::time::{Duration, Instant}; +use ds::time::{Duration, Instant}; use tokio::io::{AsyncRead, AsyncWrite}; diff --git a/stream/src/reconn.rs b/stream/src/reconn.rs index 988e8d962..b99218d91 100644 --- a/stream/src/reconn.rs +++ b/stream/src/reconn.rs @@ -1,7 +1,7 @@ use metrics::{Metric, Path}; use std::sync::atomic::{AtomicBool, Ordering::Acquire}; use std::sync::Arc; -use std::time::Duration; +use ds::time::Duration; pub(crate) struct ReconnPolicy { single: Arc, conns: usize, diff --git a/tests/src/asserts.rs b/tests/src/asserts.rs new file mode 100644 index 000000000..13bf00335 --- /dev/null +++ b/tests/src/asserts.rs @@ -0,0 +1,4 @@ +#[test] +fn test_assert() { + ds::assert!(1 == 1, "assert true"); +} diff --git a/tests/src/mem.rs b/tests/src/mem.rs index feb973664..5d01a9a6f 100644 --- a/tests/src/mem.rs +++ b/tests/src/mem.rs @@ -63,7 +63,7 @@ fn ring_buffer() { rrb.advance_write(1024); // 等待10ms。(默认是4ms) - std::thread::sleep(std::time::Duration::from_millis(10)); + std::thread::sleep(ds::time::Duration::from_millis(10)); let buf = rrb.as_mut_bytes(); assert_eq!(buf.len(), 1024); rrb.advance_write(1024); diff --git a/tests/src/ring_buffer.rs b/tests/src/ring_buffer.rs index 435d7c155..7260945b3 100644 --- a/tests/src/ring_buffer.rs +++ b/tests/src/ring_buffer.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tests { use ds::{RingBuffer, RingSlice}; - use std::time::{Duration, Instant}; + use ds::time::{Duration, Instant}; fn rnd_bytes(size: usize) -> Vec { let data: Vec = (0..size).map(|_| rand::random::()).collect(); @@ -64,7 +64,7 @@ mod tests { assert_eq!(buf.len(), 0); // 等待10ms。(默认是4ms) - std::thread::sleep(std::time::Duration::from_millis(10)); + std::thread::sleep(ds::time::Duration::from_millis(10)); let buf = rrb.as_mut_bytes(); assert_eq!(buf.len(), 1024); rrb.advance_write(1024); @@ -76,7 +76,7 @@ mod tests { let ins = Instant::now(); loop { rrb.advance_write(0); - std::thread::sleep(std::time::Duration::from_millis(3)); + std::thread::sleep(ds::time::Duration::from_millis(3)); if ins.elapsed() >= Duration::from_secs(70) { break; }