Skip to content

Commit

Permalink
Use serde_millis where appropriate instead of builtin serde
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Jan 29, 2018
1 parent 8e4dd5a commit 8beffe5
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 40 deletions.
5 changes: 2 additions & 3 deletions src/daemon/elect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::time::SystemTime;
use std::collections::{HashMap};

use crossbeam::sync::ArcCell;
use serde_millis;

pub use self::machine::Epoch;
pub use self::network::spawn_election;
pub use self::settings::peers_refresh;
pub use self::state::ElectionState;
use id::Id;
use peer::Peer;
use frontend::serialize::{serialize_timestamp};

mod action;
mod info;
Expand All @@ -28,8 +28,7 @@ mod encode;

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ScheduleStamp {
#[serde(serialize_with="serialize_timestamp",
deserialize_with="deserialize_timestamp")]
#[serde(with="serde_millis")]
pub timestamp: SystemTime,
pub hash: String,
pub origin: Id,
Expand Down
6 changes: 3 additions & 3 deletions src/daemon/elect/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::Instant;
use id::Id;
use elect::Epoch;
use elect::machine::Machine;
use frontend::serialize::{serialize_opt_timestamp, serialize_timestamp};
use serde_millis;

/// This is same as elect::machine::Machine, but for easier publishing to
/// API
Expand All @@ -23,13 +23,13 @@ pub struct ElectionState {
/// Current epoch (for debugging)
pub epoch: Epoch,
/// Current timeout (for debugging), JSON-friendly, in milliseconds
#[serde(serialize_with="serialize_timestamp")]
#[serde(with="serde_millis")]
pub deadline: SystemTime,
/// Last known timestamp when cluster was known to be stable
/// the `ElectionState::from` timestamp returns it either None or now
/// depending on whether cluster is table. And `shared` module keeps track
/// of the last one
#[serde(serialize_with="serialize_opt_timestamp")]
#[serde(with="serde_millis")]
pub last_stable_timestamp: Option<SystemTime>,
}

Expand Down
6 changes: 3 additions & 3 deletions src/daemon/frontend/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use futures::future::{FutureResult, ok, Future};
use gron::json_to_gron;
use serde::Serialize;
use serde_json::{Value, to_writer, to_writer_pretty, to_value};
use serde_millis;
use tk_http::Status::{self, NotFound};
use tk_http::Status::{TooManyRequests, ServiceUnavailable};
use tk_http::server::{Codec as CodecTrait};
Expand All @@ -16,7 +17,6 @@ use elect::{ElectionState};
use fetch;
use frontend::error_page::{error_page};
use frontend::routing::{ApiRoute, Format};
use frontend::serialize::serialize_opt_timestamp;
use frontend::to_json::ToJson;
use frontend::{reply, read_json};
use id::Id;
Expand Down Expand Up @@ -159,11 +159,11 @@ pub fn serve<S: 'static>(state: &SharedState, route: &ApiRoute, format: Format)
name: &'a str,
hostname: &'a str,
peers: usize,
#[serde(serialize_with="serialize_opt_timestamp")]
#[serde(with="serde_millis")]
peers_timestamp: Option<SystemTime>,
leader: Option<LeaderInfo<'a>>,
roles: usize,
#[serde(serialize_with="serialize_opt_timestamp")]
#[serde(with="serde_millis")]
last_stable_timestamp: Option<SystemTime>,
num_errors: usize,
errors: &'a HashMap<&'static str, String>,
Expand Down
29 changes: 0 additions & 29 deletions src/daemon/frontend/serialize.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,6 @@
use std::time::{SystemTime, Duration, UNIX_EPOCH};

use serde::Serializer;


fn tstamp_to_ms(tm: SystemTime) -> u64 {
let ts = tm.duration_since(UNIX_EPOCH)
.expect("timestamp is always after unix epoch");
return ts.as_secs()*1000 + (ts.subsec_nanos() / 1000000) as u64;
}

pub fn ms_to_system_time(ms: u64) -> SystemTime {
UNIX_EPOCH + Duration::from_millis(ms)
}

pub fn serialize_timestamp<S>(tm: &SystemTime, ser: S)
-> Result<S::Ok, S::Error>
where S: Serializer
{
ser.serialize_u64(tstamp_to_ms(*tm))
}

pub fn serialize_opt_timestamp<S>(tm: &Option<SystemTime>, ser: S)
-> Result<S::Ok, S::Error>
where S: Serializer
{
match *tm {
Some(ts) => {
ser.serialize_some(&tstamp_to_ms(ts))
}
None => {
ser.serialize_none()
}
}
}
4 changes: 2 additions & 2 deletions src/daemon/scheduler/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use std::time::{Duration, SystemTime, Instant};

use serde;
use serde_json::{Value as Json};
use serde_millis;
use inotify::{Inotify};
use scan_dir::ScanDir;
use libcantal::{Counter, Integer};
use frontend::serialize::{serialize_timestamp};

use config;
use hash::hash;
Expand All @@ -34,7 +34,7 @@ pub struct Parent(Arc<Schedule>);

#[derive(Serialize, Debug)]
pub struct SchedulerInput {
#[serde(serialize_with="serialize_timestamp")]
#[serde(with="serde_millis")]
now: SystemTime,
current_host: String,
current_id: Id,
Expand Down

0 comments on commit 8beffe5

Please sign in to comment.