Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated timestamp_nanos_opt method #22

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading