From a599e65484b95debc885101e82d24ea457e3eeb1 Mon Sep 17 00:00:00 2001 From: Rui Fan <1996fanrui@gmail.com> Date: Sat, 10 Aug 2024 13:36:57 +0800 Subject: [PATCH 1/2] chore: optimize the magic number to Option and extract common code --- src/append/rolling_file/rolling.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/append/rolling_file/rolling.rs b/src/append/rolling_file/rolling.rs index d50da15..8a333b8 100644 --- a/src/append/rolling_file/rolling.rs +++ b/src/append/rolling_file/rolling.rs @@ -166,7 +166,7 @@ struct State { current_date: OffsetDateTime, current_count: usize, current_filesize: usize, - next_date: usize, + next_date_timestamp: Option, max_size: usize, max_files: Option, } @@ -183,10 +183,7 @@ impl State { ) -> anyhow::Result<(Self, RwLock)> { let log_dir = dir.as_ref().to_path_buf(); let date_format = rotation.date_format(); - let next_date = rotation - .next_date(&now) - .map(|date| date.unix_timestamp() as usize) - .unwrap_or(0); + let next_date_timestamp = rotation.next_date_timestamp(&now); let current_date = now; let current_count = 0; @@ -200,7 +197,7 @@ impl State { current_date, current_count, current_filesize, - next_date, + next_date_timestamp, rotation, max_size, max_files, @@ -320,12 +317,9 @@ impl State { } fn should_rollover_on_date(&self, date: OffsetDateTime) -> bool { - let next_date = self.next_date; - if next_date == 0 { - false - } else { - date.unix_timestamp() as usize >= next_date - } + self.next_date_timestamp + .map(|ts| date.unix_timestamp() as usize >= ts) + .unwrap_or(false) } fn should_rollover_on_size(&self) -> bool { @@ -342,11 +336,7 @@ impl State { self.current_date = now; self.current_count = 0; self.current_filesize = 0; - self.next_date = self - .rotation - .next_date(&now) - .map(|date| date.unix_timestamp() as usize) - .unwrap_or(0); + self.next_date_timestamp = self.rotation.next_date_timestamp(&now); } } @@ -364,7 +354,7 @@ pub enum Rotation { } impl Rotation { - fn next_date(&self, current_date: &OffsetDateTime) -> Option { + fn next_date_timestamp(&self, current_date: &OffsetDateTime) -> Option { let next_date = match *self { Rotation::Minutely => *current_date + Duration::minutes(1), Rotation::Hourly => *current_date + Duration::hours(1), @@ -372,7 +362,7 @@ impl Rotation { Rotation::Never => return None, }; - Some(self.round_date(&next_date)) + Some(self.round_date(&next_date).unix_timestamp() as usize) } fn round_date(&self, date: &OffsetDateTime) -> OffsetDateTime { From 2894986e323d5a4f9036fc79723c916f07948b60 Mon Sep 17 00:00:00 2001 From: tison Date: Sat, 10 Aug 2024 14:53:49 +0800 Subject: [PATCH 2/2] tidy code Signed-off-by: tison --- src/append/rolling_file/rolling.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/append/rolling_file/rolling.rs b/src/append/rolling_file/rolling.rs index 8a333b8..2f9fcdf 100644 --- a/src/append/rolling_file/rolling.rs +++ b/src/append/rolling_file/rolling.rs @@ -318,8 +318,7 @@ impl State { fn should_rollover_on_date(&self, date: OffsetDateTime) -> bool { self.next_date_timestamp - .map(|ts| date.unix_timestamp() as usize >= ts) - .unwrap_or(false) + .is_some_and(|ts| date.unix_timestamp() as usize >= ts) } fn should_rollover_on_size(&self) -> bool {