Skip to content

Commit

Permalink
feat: update edge to new delta format
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaanus committed Jan 30, 2025
1 parent be47a4c commit 5b9c284
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 47 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ tokio-stream = { version = "0.1.17" }
tracing = { version = "0.1.41", features = ["log"] }
tracing-subscriber = { version = "0.3.19", features = ["json", "env-filter"] }
ulid = "1.1.4"
unleash-types = { version = "0.15.4", features = ["openapi", "hashes"] }
unleash-yggdrasil = { version = "0.14.6" }
unleash-types = { version = "0.15.5", features = ["openapi", "hashes"] }
unleash-yggdrasil = { version = "0.16.0" }
utoipa = { version = "5.3.1", features = ["actix_extras", "chrono"] }
utoipa-swagger-ui = { version = "9.0.0", features = ["actix-web"] }
[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions server/src/feature_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ impl FeatureCache {
pub fn apply_delta(&self, key: String, delta: &ClientFeaturesDelta) {
let client_features = ClientFeatures {
version : 2,
features : delta.updated.clone(),
segments: delta.segments.clone(),
features : vec![],
segments: Some(vec![]),
query: None,
meta: None,
};
Expand All @@ -80,7 +80,7 @@ impl FeatureCache {
.and_modify(|existing_features| {
existing_features.modify_in_place(delta);
})
.or_insert(client_features);
.or_insert(client_features.modify_and_copy(delta));
self.send_full_update(key);
}

Expand Down
83 changes: 47 additions & 36 deletions server/src/http/refresher/delta_refresher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ impl FeatureRefresher {
delta: ClientFeaturesDelta,
etag: Option<EntityTag>,
) {
let updated_len = delta.updated.len();
let removed_len = delta.removed.len();
let updated_len = delta.events.len();

debug!(
"Got updated client features delta. Updating features with {etag:?}, updated {updated_len}, removed {removed_len}"
"Got updated client features delta. Updating features with {etag:?}, events count {updated_len}"
);

let key = cache_key(refresh_token);
Expand Down Expand Up @@ -129,9 +128,7 @@ mod tests {
use crate::http::refresher::feature_refresher::FeatureRefresher;
use crate::http::unleash_client::{ClientMetaInformation, UnleashClient};
use crate::types::EdgeToken;
use unleash_types::client_features::{
ClientFeature, ClientFeatures, ClientFeaturesDelta, Constraint, Operator, Segment,
};
use unleash_types::client_features::{ClientFeature, ClientFeatures, ClientFeaturesDelta, Constraint, DeltaEvent, Operator, Segment};
use unleash_yggdrasil::EngineState;

#[actix_web::test]
Expand Down Expand Up @@ -201,45 +198,59 @@ mod tests {
fn revision(revision_id: u32) -> ClientFeaturesDelta {
match revision_id {
1 => ClientFeaturesDelta {
updated: vec![
ClientFeature {
name: "test1".into(),
feature_type: Some("release".into()),
..Default::default()
events: vec![
DeltaEvent::FeatureUpdated {
event_id: 1,
feature: ClientFeature {
name: "test1".into(),
feature_type: Some("release".into()),
..Default::default()
},
},
ClientFeature {
name: "test2".into(),
feature_type: Some("release".into()),
..Default::default()
DeltaEvent::FeatureUpdated {
event_id: 1,
feature: ClientFeature {
name: "test2".into(),
feature_type: Some("release".into()),
..Default::default()
},
},
DeltaEvent::SegmentUpdated {
event_id: 1,
segment: Segment {
id: 1,
constraints: vec![Constraint {
context_name: "userId".into(),
operator: Operator::In,
case_insensitive: false,
inverted: false,
values: Some(vec!["7".into()]),
value: None,
}],
},
},
],
removed: vec![],
segments: Some(vec![Segment {
id: 1,
constraints: vec![Constraint {
context_name: "userId".into(),
operator: Operator::In,
case_insensitive: false,
inverted: false,
values: Some(vec!["7".into()]),
value: None,
}],
}]),
revision_id: 1,
},
_ => ClientFeaturesDelta {
updated: vec![ClientFeature {
name: "test1".into(),
feature_type: Some("release".into()),
..Default::default()
}],
removed: vec!["test2".to_string()],
segments: None,
revision_id: 2,
events: vec![
DeltaEvent::FeatureUpdated {
event_id: 2,
feature: ClientFeature {
name: "test1".into(),
feature_type: Some("release".into()),
..Default::default()
},
},
DeltaEvent::FeatureRemoved {
event_id: 2,
feature_name: "test2".to_string(),
},
],
},
}
}


async fn return_client_features_delta(etag_header: Option<String>) -> HttpResponse {
match etag_header {
Some(value) => match value.as_str() {
Expand Down
13 changes: 11 additions & 2 deletions server/src/http/refresher/feature_refresher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use futures::TryStreamExt;
use json_structural_diff::JsonDiff;
use reqwest::StatusCode;
use tracing::{debug, info, warn};
use unleash_types::client_features::{ClientFeatures};
use unleash_types::client_features::{ClientFeatures, DeltaEvent};
use unleash_types::client_metrics::{ClientApplication, MetricsMetadata};
use unleash_yggdrasil::EngineState;

Expand Down Expand Up @@ -411,7 +411,16 @@ impl FeatureRefresher {
if let Some(client_features) = self.features_cache.get(&key).as_ref() {
if let Ok(ClientFeaturesDeltaResponse::Updated(delta_features, _etag)) = delta_result {
let c_features = &client_features.features;
let d_features = delta_features.updated;
let d_features = delta_features
.events
.iter()
.find_map(|event| {
if let DeltaEvent::Hydration { features, .. } = event {
Some(features)
} else {
None
}
});

let delta_json = serde_json::to_value(d_features).unwrap();
let client_json = serde_json::to_value(c_features).unwrap();
Expand Down

0 comments on commit 5b9c284

Please sign in to comment.