Skip to content

Commit

Permalink
Merge pull request #22 from negezor/replace-chrono-deprecate
Browse files Browse the repository at this point in the history
Replace deprecated timestamp_nanos_opt method
  • Loading branch information
bahlo authored Oct 8, 2024
2 parents 2f2b9de + a9adc26 commit 7547d61
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ impl<'a> Builder<'a> {
return Err(Error::StartTimeAheadOfCurrentTime(start_time));
}

to_sonyflake_time(start_time)
to_sonyflake_time(start_time)?
} else {
to_sonyflake_time(Utc.with_ymd_and_hms(2014, 9, 1, 0, 0, 0).unwrap())
to_sonyflake_time(Utc.with_ymd_and_hms(2014, 9, 1, 0, 0, 0).unwrap())?
};

let machine_id = if let Some(machine_id) = self.machine_id {
Expand Down
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: 17 additions & 9 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,17 +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() / 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)
- Duration::from_nanos((Utc::now().timestamp_nanos() % SONYFLAKE_TIME_UNIT) as u64)
fn sleep_time(overtime: i64) -> Result<Duration, Error> {
Ok(Duration::from_millis(overtime as u64 * 10)
- Duration::from_nanos(
(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 7547d61

Please sign in to comment.