Skip to content

Commit

Permalink
Merge pull request #39 from BinChengZhao/fix/timer-fluctuate
Browse files Browse the repository at this point in the history
fix: #issule 37 & #issule 38
  • Loading branch information
BinChengZhao authored May 9, 2022
2 parents 85f8313 + 13f7cea commit a8083ff
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "delay_timer"
version = "0.11.1"
version = "0.11.2"
authors = ["binchengZhao <[email protected]>"]
edition = "2018"
repository = "https://github.com/BinChengZhao/delay-timer"
Expand Down Expand Up @@ -29,7 +29,7 @@ status-report = []
[dependencies]
cron_clock = "0.8.0"
anyhow = "^1.0.31"
rs-snowflake = "0.5.0"
rs-snowflake = "0.6.0"
dashmap = "^4.0.2"
lru = "^0.6.5"
once_cell = "1.9.0"
Expand Down
17 changes: 10 additions & 7 deletions src/timer/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,17 @@ impl<'a> TryFrom<(FrequencyUnify<'a>, ScheduleIteratorTimeZone)> for FrequencyIn
return Err(FrequencyAnalyzeError::DisInitTime);
}

let seconds_state: SecondsState = (timestamp()..).step_by(seconds as usize);
let seconds_state: SecondsState =
((timestamp() + seconds)..).step_by(seconds as usize);
FrequencyInner::SecondsCountDown(1, seconds_state)
}
FrequencyUnify::FrequencySeconds(FrequencySeconds::Repeated(seconds)) => {
if seconds == 0 {
return Err(FrequencyAnalyzeError::DisInitTime);
}

let seconds_state: SecondsState = (timestamp()..).step_by(seconds as usize);
let seconds_state: SecondsState =
((timestamp() + seconds)..).step_by(seconds as usize);

FrequencyInner::SecondsRepeated(seconds_state)
}
Expand All @@ -245,7 +247,8 @@ impl<'a> TryFrom<(FrequencyUnify<'a>, ScheduleIteratorTimeZone)> for FrequencyIn
return Err(FrequencyAnalyzeError::DisInitTime);
}

let seconds_state: SecondsState = (timestamp()..).step_by(seconds as usize);
let seconds_state: SecondsState =
((timestamp() + seconds)..).step_by(seconds as usize);
FrequencyInner::SecondsCountDown(count_down, seconds_state)
}
};
Expand Down Expand Up @@ -1148,7 +1151,7 @@ mod tests {
.map(|i| {
debug_assert_eq!(
task.get_next_exec_timestamp().unwrap(),
timestamp() + (init_seconds * (i - 1))
timestamp() + (init_seconds * i)
);
})
.for_each(drop);
Expand Down Expand Up @@ -1180,7 +1183,7 @@ mod tests {
.map(|i| {
debug_assert_eq!(
task.get_next_exec_timestamp().unwrap(),
timestamp() + (init_minutes * (i - 1) * ONE_MINUTE)
timestamp() + (init_minutes * i * ONE_MINUTE)
);
})
.for_each(drop);
Expand All @@ -1201,7 +1204,7 @@ mod tests {
.map(|i| {
debug_assert_eq!(
task.get_next_exec_timestamp().unwrap(),
timestamp() + (init_hours * (i - 1) * ONE_HOUR)
timestamp() + (init_hours * i * ONE_HOUR)
);
})
.for_each(drop);
Expand All @@ -1222,7 +1225,7 @@ mod tests {
.map(|i| {
debug_assert_eq!(
task.get_next_exec_timestamp().unwrap(),
timestamp() + (init_days * (i - 1) * ONE_DAY)
timestamp() + (init_days * i * ONE_DAY)
);
})
.for_each(drop);
Expand Down
4 changes: 2 additions & 2 deletions src/timer/timer_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,10 @@ impl Timer {
// when-on-slot61-exec: (task_excute_timestamp - timestamp + next_second_hand) % slot_seed == 61

// Time difference + next second hand % DEFAULT_TIMER_SLOT_COUNT
let step = task_excute_timestamp.checked_sub(timestamp).unwrap_or(1) + next_second_hand;
let step = task_excute_timestamp.checked_sub(timestamp).unwrap_or(1);
let cylinder_line = step / DEFAULT_TIMER_SLOT_COUNT;
task.set_cylinder_line(cylinder_line);
let slot_seed = step % DEFAULT_TIMER_SLOT_COUNT;
let slot_seed = (step + next_second_hand) % DEFAULT_TIMER_SLOT_COUNT;

{
let mut slot_mut = self
Expand Down

0 comments on commit a8083ff

Please sign in to comment.