Skip to content

Commit a8083ff

Browse files
authored
Merge pull request #39 from BinChengZhao/fix/timer-fluctuate
fix: #issule 37 & #issule 38
2 parents 85f8313 + 13f7cea commit a8083ff

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "delay_timer"
3-
version = "0.11.1"
3+
version = "0.11.2"
44
authors = ["binchengZhao <binchengZhao@outlook.com>"]
55
edition = "2018"
66
repository = "https://github.com/BinChengZhao/delay-timer"
@@ -29,7 +29,7 @@ status-report = []
2929
[dependencies]
3030
cron_clock = "0.8.0"
3131
anyhow = "^1.0.31"
32-
rs-snowflake = "0.5.0"
32+
rs-snowflake = "0.6.0"
3333
dashmap = "^4.0.2"
3434
lru = "^0.6.5"
3535
once_cell = "1.9.0"

src/timer/task.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,17 @@ impl<'a> TryFrom<(FrequencyUnify<'a>, ScheduleIteratorTimeZone)> for FrequencyIn
228228
return Err(FrequencyAnalyzeError::DisInitTime);
229229
}
230230

231-
let seconds_state: SecondsState = (timestamp()..).step_by(seconds as usize);
231+
let seconds_state: SecondsState =
232+
((timestamp() + seconds)..).step_by(seconds as usize);
232233
FrequencyInner::SecondsCountDown(1, seconds_state)
233234
}
234235
FrequencyUnify::FrequencySeconds(FrequencySeconds::Repeated(seconds)) => {
235236
if seconds == 0 {
236237
return Err(FrequencyAnalyzeError::DisInitTime);
237238
}
238239

239-
let seconds_state: SecondsState = (timestamp()..).step_by(seconds as usize);
240+
let seconds_state: SecondsState =
241+
((timestamp() + seconds)..).step_by(seconds as usize);
240242

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

248-
let seconds_state: SecondsState = (timestamp()..).step_by(seconds as usize);
250+
let seconds_state: SecondsState =
251+
((timestamp() + seconds)..).step_by(seconds as usize);
249252
FrequencyInner::SecondsCountDown(count_down, seconds_state)
250253
}
251254
};
@@ -1148,7 +1151,7 @@ mod tests {
11481151
.map(|i| {
11491152
debug_assert_eq!(
11501153
task.get_next_exec_timestamp().unwrap(),
1151-
timestamp() + (init_seconds * (i - 1))
1154+
timestamp() + (init_seconds * i)
11521155
);
11531156
})
11541157
.for_each(drop);
@@ -1180,7 +1183,7 @@ mod tests {
11801183
.map(|i| {
11811184
debug_assert_eq!(
11821185
task.get_next_exec_timestamp().unwrap(),
1183-
timestamp() + (init_minutes * (i - 1) * ONE_MINUTE)
1186+
timestamp() + (init_minutes * i * ONE_MINUTE)
11841187
);
11851188
})
11861189
.for_each(drop);
@@ -1201,7 +1204,7 @@ mod tests {
12011204
.map(|i| {
12021205
debug_assert_eq!(
12031206
task.get_next_exec_timestamp().unwrap(),
1204-
timestamp() + (init_hours * (i - 1) * ONE_HOUR)
1207+
timestamp() + (init_hours * i * ONE_HOUR)
12051208
);
12061209
})
12071210
.for_each(drop);
@@ -1222,7 +1225,7 @@ mod tests {
12221225
.map(|i| {
12231226
debug_assert_eq!(
12241227
task.get_next_exec_timestamp().unwrap(),
1225-
timestamp() + (init_days * (i - 1) * ONE_DAY)
1228+
timestamp() + (init_days * i * ONE_DAY)
12261229
);
12271230
})
12281231
.for_each(drop);

src/timer/timer_core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,10 @@ impl Timer {
373373
// when-on-slot61-exec: (task_excute_timestamp - timestamp + next_second_hand) % slot_seed == 61
374374

375375
// Time difference + next second hand % DEFAULT_TIMER_SLOT_COUNT
376-
let step = task_excute_timestamp.checked_sub(timestamp).unwrap_or(1) + next_second_hand;
376+
let step = task_excute_timestamp.checked_sub(timestamp).unwrap_or(1);
377377
let cylinder_line = step / DEFAULT_TIMER_SLOT_COUNT;
378378
task.set_cylinder_line(cylinder_line);
379-
let slot_seed = step % DEFAULT_TIMER_SLOT_COUNT;
379+
let slot_seed = (step + next_second_hand) % DEFAULT_TIMER_SLOT_COUNT;
380380

381381
{
382382
let mut slot_mut = self

0 commit comments

Comments
 (0)