33
44// use std::time::Duration;
55
6+ use std:: { time:: { Duration , SystemTime , UNIX_EPOCH } , sync:: atomic:: { AtomicU64 , Ordering } } ;
7+
68use bee_message:: milestone:: MilestoneIndex ;
79use bee_tangle:: { storage:: StorageBackend , Tangle } ;
810
911use crate :: { types:: LedgerIndex , workers:: pruning:: config:: PruningConfig } ;
1012
1113const PRUNING_BATCH_SIZE_MAX : u32 = 200 ;
1214
15+ static LAST_PRUNING_BY_SIZE : AtomicU64 = AtomicU64 :: new ( 0 ) ;
16+
1317/// Reasons for skipping pruning.
1418#[ derive( Debug , thiserror:: Error ) ]
1519pub ( crate ) enum PruningSkipReason {
@@ -23,8 +27,8 @@ pub(crate) enum PruningSkipReason {
2327 BelowMilestoneIndexThreshold { reached_in : u32 } ,
2428 #[ error( "Pruning by storage size skipped because current size < {target_size}." ) ]
2529 BelowStorageSizeThreshold { target_size : usize } ,
26- // #[error("Pruning by storage size is skipped because of cooldown.")]
27- // BelowCooldownTimeThreshold { reached_in: Duration } ,
30+ #[ error( "Pruning by storage size is skipped because of cooldown." ) ]
31+ BelowCooldownTimeThreshold ,
2832}
2933
3034pub ( crate ) enum PruningTask {
@@ -62,6 +66,14 @@ pub(crate) fn should_prune<S: StorageBackend>(
6266 ) )
6367 }
6468 } else if config. size ( ) . enabled ( ) {
69+
70+ let last = Duration :: from_secs ( LAST_PRUNING_BY_SIZE . load ( Ordering :: Relaxed ) ) ;
71+ let now = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( ) ;
72+
73+ if now < last + config. size ( ) . cooldown_time ( ) {
74+ return Err ( PruningSkipReason :: BelowCooldownTimeThreshold ) ;
75+ }
76+
6577 let actual_size = {
6678 if let Ok ( size) = storage. size ( ) {
6779 if let Some ( size) = size {
@@ -85,6 +97,9 @@ pub(crate) fn should_prune<S: StorageBackend>(
8597
8698 log:: debug!( "Num bytes to prune: {num_bytes_to_prune}" ) ;
8799
100+ // Store the time we issued a pruning-by-size.
101+ LAST_PRUNING_BY_SIZE . store ( now. as_secs ( ) , Ordering :: Relaxed ) ;
102+
88103 Ok ( PruningTask :: BySize ( num_bytes_to_prune) )
89104 }
90105 } else {
0 commit comments