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

refactor: minor cleanup after #618 #621

Merged
merged 3 commits into from
Jan 8, 2025
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
13 changes: 8 additions & 5 deletions server/src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,22 @@ pub(crate) fn name_match_filter(name_prefix: String) -> FeatureFilter {
Box::new(move |f| f.name.starts_with(&name_prefix))
}

pub(crate) fn project_filter(token: &EdgeToken) -> FeatureFilter {
let token = token.clone();
pub(crate) fn project_filter_from_projects(projects: Vec<String>) -> FeatureFilter {
Box::new(move |feature| {
if let Some(feature_project) = &feature.project {
token.projects.is_empty()
|| token.projects.contains(&"*".to_string())
|| token.projects.contains(feature_project)
projects.is_empty()
|| projects.contains(&"*".to_string())
|| projects.contains(feature_project)
} else {
false
}
})
}

pub(crate) fn project_filter(token: &EdgeToken) -> FeatureFilter {
project_filter_from_projects(token.projects.clone())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
29 changes: 11 additions & 18 deletions server/src/http/broadcaster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use unleash_types::client_features::{ClientFeatures, Query};
use crate::{
error::EdgeError,
feature_cache::{FeatureCache, UpdateType},
filters::{filter_client_features, name_prefix_filter, FeatureFilter, FeatureFilterSet},
filters::{
filter_client_features, name_prefix_filter, project_filter_from_projects, FeatureFilterSet,
},
types::{EdgeJsonResult, EdgeResult, EdgeToken},
};

Expand Down Expand Up @@ -124,20 +126,23 @@ impl Broadcaster {
async fn heartbeat(&self) {
let mut active_connections = 0i64;
for mut group in self.active_connections.iter_mut() {
let clients = std::mem::take(&mut group.clients);
let ok_clients = &mut group.clients;
let mut ok_clients = Vec::new();

for ClientData { token, sender } in clients {
for ClientData { token, sender } in &group.clients {
if sender
.send(sse::Event::Comment("keep-alive".into()))
.await
.is_ok()
{
ok_clients.push(ClientData { token, sender });
ok_clients.push(ClientData {
token: token.clone(),
sender: sender.clone(),
});
}
}

active_connections += ok_clients.len() as i64;
group.clients = ok_clients;
}
CONNECTED_STREAMING_CLIENTS.set(active_connections)
}
Expand Down Expand Up @@ -191,7 +196,7 @@ impl Broadcaster {
} else {
FeatureFilterSet::default()
}
.with_filter(project_filter(query.projects.clone()));
.with_filter(project_filter_from_projects(query.projects.clone()));
filter_set
}

Expand Down Expand Up @@ -257,18 +262,6 @@ impl Broadcaster {
}
}

fn project_filter(projects: Vec<String>) -> FeatureFilter {
Box::new(move |feature| {
if let Some(feature_project) = &feature.project {
projects.is_empty()
|| projects.contains(&"*".to_string())
|| projects.contains(feature_project)
} else {
false
}
})
}

#[cfg(test)]
mod test {
use tokio::time::timeout;
Expand Down
Loading