Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit the number of items to notify each time. #30

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading