Skip to content

Commit

Permalink
Add FailedToGetCurrentTime error
Browse files Browse the repository at this point in the history
  • Loading branch information
bahlo committed Oct 8, 2024
1 parent ef89c29 commit e25bcb6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ pub enum Error {
NoPrivateIPv4,
#[error("mutex is poisoned (i.e. a panic happened while it was locked)")]
MutexPoisoned,
#[error("failed to get current time")]
FailedToGetCurrentTime,
}
26 changes: 16 additions & 10 deletions src/sonyflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Sonyflake {
pub fn next_id(&self) -> Result<u64, Error> {
let mut internals = self.0.internals.lock().map_err(|_| Error::MutexPoisoned)?;

let current = current_elapsed_time(self.0.start_time);
let current = current_elapsed_time(self.0.start_time)?;
if internals.elapsed_time < current {
internals.elapsed_time = current;
internals.sequence = 0;
Expand All @@ -66,7 +66,7 @@ impl Sonyflake {
if internals.sequence == 0 {
internals.elapsed_time += 1;
let overtime = internals.elapsed_time - current;
thread::sleep(sleep_time(overtime));
thread::sleep(sleep_time(overtime)?);
}
}

Expand All @@ -91,19 +91,25 @@ impl Clone for Sonyflake {

const SONYFLAKE_TIME_UNIT: i64 = 10_000_000; // nanoseconds, i.e. 10msec

pub(crate) fn to_sonyflake_time(time: DateTime<Utc>) -> i64 {
time.timestamp_nanos_opt().unwrap() / SONYFLAKE_TIME_UNIT
pub(crate) fn to_sonyflake_time(time: DateTime<Utc>) -> Result<i64, Error> {
Ok(time
.timestamp_nanos_opt()
.ok_or(Error::FailedToGetCurrentTime)?
/ SONYFLAKE_TIME_UNIT)
}

fn current_elapsed_time(start_time: i64) -> i64 {
to_sonyflake_time(Utc::now()) - start_time
fn current_elapsed_time(start_time: i64) -> Result<i64, Error> {
Ok(to_sonyflake_time(Utc::now())? - start_time)
}

fn sleep_time(overtime: i64) -> Duration {
Duration::from_millis(overtime as u64 * 10)
fn sleep_time(overtime: i64) -> Result<Duration, Error> {
Ok(Duration::from_millis(overtime as u64 * 10)
- Duration::from_nanos(
(Utc::now().timestamp_nanos_opt().unwrap() % SONYFLAKE_TIME_UNIT) as u64,
)
(Utc::now()
.timestamp_nanos_opt()
.ok_or(Error::FailedToGetCurrentTime)?
% SONYFLAKE_TIME_UNIT) as u64,
))
}

pub struct DecomposedSonyflake {
Expand Down
6 changes: 3 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ fn test_once() -> Result<(), BoxDynError> {
#[test]
fn test_run_for_10s() -> Result<(), BoxDynError> {
let now = Utc::now();
let start_time = to_sonyflake_time(now);
let start_time = to_sonyflake_time(now)?;
let sf = Sonyflake::builder().start_time(now).finalize()?;

let mut last_id: u64 = 0;
let mut max_sequence: u64 = 0;

let machine_id = lower_16_bit_private_ip()? as u64;

let initial = to_sonyflake_time(Utc::now());
let initial = to_sonyflake_time(Utc::now())?;
let mut current = initial.clone();
while current - initial < 1000 {
let id = sf.next_id()?;
Expand All @@ -71,7 +71,7 @@ fn test_run_for_10s() -> Result<(), BoxDynError> {
}
last_id = id;

current = to_sonyflake_time(Utc::now());
current = to_sonyflake_time(Utc::now())?;

let actual_msb = parts.msb;
if actual_msb != 0 {
Expand Down

0 comments on commit e25bcb6

Please sign in to comment.