Skip to content

Commit

Permalink
simplify some types
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheartman committed Dec 11, 2024
1 parent 481acce commit 057f174
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 35 deletions.
35 changes: 1 addition & 34 deletions server/src/client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub async fn stream_features(
filter_query: Query<FeatureFilters>,
req: HttpRequest,
) -> EdgeResult<impl Responder> {
let (validated_token, filter_set, query) =
let (validated_token, _filter_set, query) =
get_feature_filter(&edge_token, &token_cache, filter_query.clone())?;
match req.app_data::<Data<FeatureRefresher>>() {
Some(refresher) => {
Expand Down Expand Up @@ -83,9 +83,6 @@ pub async fn post_features(
resolve_features(edge_token, features_cache, token_cache, filter_query, req).await
}

/// this was extracted from resolve_features because it gives us the necessary
/// values to filter flags and construct client feature responses later. It's a
/// standalone function and can be moved to a different file if necessary.
fn get_feature_filter(
edge_token: &EdgeToken,
token_cache: &Data<DashMap<String, EdgeToken>>,
Expand Down Expand Up @@ -119,36 +116,6 @@ fn get_feature_filter(
Ok((validated_token, filter_set, query))
}

/// This is the second half of resolve_features. The idea was that you don't
/// need to do the extraction work twice. The broadcaster shold be able to do
/// something like the Some arm of the match here, except we'll know that we
/// already have the refresher.
#[cfg(feature = "streaming")]
async fn resolve_features_2(
query: unleash_types::client_features::Query,
validated_token: EdgeToken,
filter_set: FeatureFilterSet,
features_cache: Data<DashMap<String, ClientFeatures>>,
req: HttpRequest,
) -> EdgeJsonResult<ClientFeatures> {
let client_features = match req.app_data::<Data<FeatureRefresher>>() {
Some(refresher) => {
refresher
.features_for_filter(validated_token.clone(), &filter_set)
.await
}
None => features_cache
.get(&cache_key(&validated_token))
.map(|client_features| filter_client_features(&client_features, &filter_set))
.ok_or(EdgeError::ClientCacheError),
}?;

Ok(Json(ClientFeatures {
query: Some(query),
..client_features
}))
}

/// This is the same as it always was, except I extracted bits of it.
async fn resolve_features(
edge_token: EdgeToken,
Expand Down
12 changes: 12 additions & 0 deletions server/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ use std::error::Error;
use std::fmt::{Display, Formatter};

use actix_web::{http::StatusCode, HttpResponseBuilder, ResponseError};
#[cfg(feature = "streaming")]
use actix_web_lab::sse::Event;
use serde::Serialize;
use serde_json::json;
#[cfg(feature = "streaming")]
use tokio::sync::mpsc::error::SendError;
use tracing::debug;

use crate::types::{EdgeToken, Status, UnleashBadRequest};
Expand Down Expand Up @@ -291,5 +295,13 @@ impl From<serde_json::Error> for EdgeError {
}
}

#[cfg(feature = "streaming")]
impl From<SendError<Event>> for EdgeError {
// todo: create better enum representation. use this is placeholder
fn from(_value: SendError<Event>) -> Self {
EdgeError::TlsError
}
}

#[cfg(test)]
mod tests {}
2 changes: 1 addition & 1 deletion server/src/http/broadcaster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Broadcaster {
.event("unleash-connected")
.into(),
)
.await;
.await?;

self.active_connections
.entry(QueryWrapper { query })
Expand Down

0 comments on commit 057f174

Please sign in to comment.