Skip to content

Commit

Permalink
time: 自定义ds::time,后续替换std::time。默认使用std::time
Browse files Browse the repository at this point in the history
  • Loading branch information
icycrystal4 committed Nov 6, 2022
1 parent aa01852 commit 29e7819
Show file tree
Hide file tree
Showing 34 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use discovery::*;
mod init;

use rt::spawn;
use std::time::Duration;
use ds::time::Duration;

use protocol::Result;

Expand Down
4 changes: 2 additions & 2 deletions agent/src/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> = Instant::now().into();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion agent/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"

[dependencies]
log = { path = "../log" }
ds = { path = "../ds" }

clap = {version = "3.1.1", features = ["derive"] }
url = "2.2.2"
Expand Down
4 changes: 2 additions & 2 deletions context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion context/src/quadruple.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion discovery/src/cfg.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion discovery/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(); // 上一次刷新的时间
Expand Down
2 changes: 1 addition & 1 deletion discovery/src/update.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions ds/src/mem/policy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const BUF_MIN: usize = 2 * 1024;
use std::time::Instant;
use crate::time::Instant;
// 内存需要缩容时的策略
// 为了避免频繁的缩容,需要设置一个最小频繁,通常使用最小间隔时间
pub struct MemPolicy {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion endpoint/src/cacheservice/config.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion endpoint/src/cacheservice/topo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion endpoint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion endpoint/src/phantomservice/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//use std::time::Duration;
//use ds::time::Duration;

use serde::{Deserialize, Serialize};

Expand Down
2 changes: 1 addition & 1 deletion endpoint/src/redisservice/config.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion endpoint/src/redisservice/topo.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion metrics/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<T: ToNumber> 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) {
Expand Down
2 changes: 1 addition & 1 deletion metrics/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub struct MetricRegister {

impl MetricRegister {
fn new(rx: Receiver<(Arc<Id>, i64)>, metrics: CowWriteHandle<Metrics>) -> 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,
Expand Down
2 changes: 1 addition & 1 deletion metrics/src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion metrics/src/types/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion metrics/src/types/rtt.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/callback.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion protocol/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use ds::time::Duration;

#[derive(Debug)]
pub enum Error {
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Resource {
}
}

use std::time::Duration;
use ds::time::Duration;
pub trait Builder<P, R, E> {
fn build(addr: &str, parser: P, rsrc: Resource, service: &str, timeout: Duration) -> E
where
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/req.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt::{Debug, Display};
use std::time::Instant;
use ds::time::Instant;

use ds::RingSlice;

Expand Down
2 changes: 1 addition & 1 deletion protocol/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion rt/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion stream/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct BackendBuilder<P, R> {
_marker: std::marker::PhantomData<(P, R)>,
}

use std::time::Duration;
use ds::time::Duration;
impl<P: Protocol, R: Request> protocol::Builder<P, R, Arc<Backend<R>>> for BackendBuilder<P, R> {
fn build(
addr: &str,
Expand Down
2 changes: 1 addition & 1 deletion stream/src/checker.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion stream/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion stream/src/reconn.rs
Original file line number Diff line number Diff line change
@@ -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<AtomicBool>,
conns: usize,
Expand Down
4 changes: 4 additions & 0 deletions tests/src/asserts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn test_assert() {
ds::assert!(1 == 1, "assert true");
}
2 changes: 1 addition & 1 deletion tests/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/src/ring_buffer.rs
Original file line number Diff line number Diff line change
@@ -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<u8> {
let data: Vec<u8> = (0..size).map(|_| rand::random::<u8>()).collect();
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down

0 comments on commit 29e7819

Please sign in to comment.