Skip to content

Commit

Permalink
Hook filter evaluation into session execution.
Browse files Browse the repository at this point in the history
  • Loading branch information
panhania committed Dec 15, 2023
1 parent 453c1ed commit c7a3ca4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/rrg/src/request/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Filter {
/// Verifies whether the given message passes the filter.
///
/// The message passes the filter if passes any of its conditions.
fn eval_message(
pub fn eval_message(
&self,
message: &dyn protobuf::MessageDyn,
) -> Result<bool, Error> {
Expand Down
30 changes: 30 additions & 0 deletions crates/rrg/src/session/fleetspeak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ impl FleetspeakSession {

Ok(())
}

/// Evaluates filters on the given reply.
///
/// This function returns a boolean indicating whether the reply passes the
/// filters.
///
/// # Errors
///
/// The function will return an error if the filter cannot be evaluated on
/// the given message (e.g. the specified field is not available on the
/// message or there was a type issue).
fn eval_filters<I>(&self, reply: &crate::response::PreparedReply<I>) -> crate::session::Result<bool>
where
I: crate::response::Item,
{
for filter in &self.filters {
let item_proto = reply.item_proto();
if !filter.eval_message(item_proto)? {
log::debug!("result '{item_proto}' filtered out by: {filter}");
return Ok(false);
}
}

Ok(true)
}
}

impl crate::session::Session for FleetspeakSession {
Expand All @@ -124,6 +149,11 @@ impl crate::session::Session for FleetspeakSession {
{
let reply = self.response_builder.reply(item).prepare();

if !self.eval_filters(&reply)? {
self.filtered_out_results += 1;
return Ok(());
}

self.network_bytes_sent += reply.send_unaccounted() as u64;
self.check_network_bytes_limit()?;

Expand Down

0 comments on commit c7a3ca4

Please sign in to comment.