Skip to content

Commit

Permalink
Limit the number of items to notify each time. (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
RinChanNOWWW authored Jan 22, 2024
1 parent bb99483 commit a3c283f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ async fn run<T: Notifier>(source: SourcePtr, mut notifier: T) {
let mut last_update = Local::now();
let interval = source.interval();
let retry_config = ConstantBuilder::default();
let each_notify = notifier.num_items_each_notify();

loop {
tokio::time::sleep(interval).await;
Expand All @@ -122,7 +123,13 @@ async fn run<T: Notifier>(source: SourcePtr, mut notifier: T) {
});

// notify
notifier.notify(&source.name(), new_items.clone()).await?;
if each_notify == 0 {
notifier.notify(&source.name(), new_items.clone()).await?;
} else {
for chunk in new_items.chunks(each_notify) {
notifier.notify(&source.name(), chunk.to_vec()).await?;
}
}
}
};

Expand Down
7 changes: 7 additions & 0 deletions src/notifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ use crate::Result;
#[async_trait::async_trait]
pub trait Notifier: Sync + Send + Clone {
async fn notify(&mut self, source: &str, items: Vec<Item>) -> Result<()>;

/// The number of items to be notified each time.
///
/// If it is 0, all items will be notified at once.
fn num_items_each_notify(&self) -> usize {
0
}
}
4 changes: 4 additions & 0 deletions src/notifier/qq_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ impl Notifier for QQGuildNotifier {
}
Ok(())
}

fn num_items_each_notify(&self) -> usize {
5
}
}

impl QQGuildNotifier {
Expand Down

0 comments on commit a3c283f

Please sign in to comment.