Skip to content

Commit cdf6fcd

Browse files
committed
Move app_context to RemoteSettingsService
1 parent 66eeb4e commit cdf6fcd

File tree

6 files changed

+36
-63
lines changed

6 files changed

+36
-63
lines changed

components/remote_settings/src/client.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ struct CollectionData {
7474
pub struct RemoteSettingsClient<C = ViaductApiClient> {
7575
// This is immutable, so it can be outside the mutex
7676
collection_name: String,
77-
#[cfg(feature = "jexl")]
78-
jexl_filter: JexlFilter,
7977
inner: Mutex<RemoteSettingsClientInner<C>>,
8078
}
8179

8280
struct RemoteSettingsClientInner<C> {
8381
storage: Storage,
8482
api_client: C,
83+
#[cfg(feature = "jexl")]
84+
jexl_filter: JexlFilter,
8585
}
8686

8787
// Add your local packaged data you want to work with here
@@ -123,11 +123,11 @@ impl<C: ApiClient> RemoteSettingsClient<C> {
123123
) -> Self {
124124
Self {
125125
collection_name,
126-
#[cfg(feature = "jexl")]
127-
jexl_filter,
128126
inner: Mutex::new(RemoteSettingsClientInner {
129127
storage,
130128
api_client,
129+
#[cfg(feature = "jexl")]
130+
jexl_filter,
131131
}),
132132
}
133133
}
@@ -149,20 +149,28 @@ impl<C: ApiClient> RemoteSettingsClient<C> {
149149

150150
/// Filters records based on the presence and evaluation of `filter_expression`.
151151
#[cfg(feature = "jexl")]
152-
fn filter_records(&self, records: Vec<RemoteSettingsRecord>) -> Vec<RemoteSettingsRecord> {
152+
fn filter_records(
153+
&self,
154+
records: Vec<RemoteSettingsRecord>,
155+
inner: &RemoteSettingsClientInner<C>,
156+
) -> Vec<RemoteSettingsRecord> {
153157
records
154158
.into_iter()
155159
.filter(|record| match record.fields.get("filter_expression") {
156160
Some(serde_json::Value::String(filter_expr)) => {
157-
self.jexl_filter.evaluate(filter_expr).unwrap_or(false)
161+
inner.jexl_filter.evaluate(filter_expr).unwrap_or(false)
158162
}
159163
_ => true, // Include records without a valid filter expression by default
160164
})
161165
.collect()
162166
}
163167

164168
#[cfg(not(feature = "jexl"))]
165-
fn filter_records(&self, records: Vec<RemoteSettingsRecord>) -> Vec<RemoteSettingsRecord> {
169+
fn filter_records(
170+
&self,
171+
records: Vec<RemoteSettingsRecord>,
172+
_inner: &RemoteSettingsClientInner<C>,
173+
) -> Vec<RemoteSettingsRecord> {
166174
records
167175
}
168176

@@ -199,7 +207,7 @@ impl<C: ApiClient> RemoteSettingsClient<C> {
199207
packaged_data.timestamp,
200208
CollectionMetadata::default(),
201209
)?;
202-
return Ok(Some(self.filter_records(packaged_data.data)));
210+
return Ok(Some(self.filter_records(packaged_data.data, &inner)));
203211
}
204212
}
205213

@@ -210,7 +218,7 @@ impl<C: ApiClient> RemoteSettingsClient<C> {
210218
//
211219
// Note: we should return these even if it's an empty list and `sync_if_empty=true`.
212220
// The "if empty" part refers to the cache being empty, not the list.
213-
(Some(cached_records), _) => Some(self.filter_records(cached_records)),
221+
(Some(cached_records), _) => Some(self.filter_records(cached_records, &inner)),
214222
// Case 3: sync_if_empty=true
215223
(None, true) => {
216224
let changeset = inner.api_client.fetch_changeset(None)?;
@@ -220,7 +228,7 @@ impl<C: ApiClient> RemoteSettingsClient<C> {
220228
changeset.timestamp,
221229
changeset.metadata,
222230
)?;
223-
Some(self.filter_records(changeset.changes))
231+
Some(self.filter_records(changeset.changes, &inner))
224232
}
225233
// Case 4: Nothing to return
226234
(None, false) => None,

components/remote_settings/src/config.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
use url::Url;
1212

13-
use crate::{ApiResult, Error, Result};
13+
use crate::{ApiResult, Error, RemoteSettingsContext, Result};
1414

1515
/// Remote settings configuration
1616
///
@@ -25,6 +25,9 @@ pub struct RemoteSettingsConfig2 {
2525
/// Bucket name to use, defaults to "main". Use "main-preview" for a preview bucket
2626
#[uniffi(default = None)]
2727
pub bucket_name: Option<String>,
28+
/// App context to use for JEXL filtering (when the `jexl` feature is present).
29+
#[uniffi(default = None)]
30+
pub app_context: Option<RemoteSettingsContext>,
2831
}
2932

3033
/// Custom configuration for the client.

components/remote_settings/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,8 @@ impl RemoteSettingsService {
103103

104104
/// Create a new Remote Settings client
105105
#[handle_error(Error)]
106-
pub fn make_client(
107-
&self,
108-
collection_name: String,
109-
app_context: Option<RemoteSettingsContext>,
110-
) -> ApiResult<Arc<RemoteSettingsClient>> {
111-
self.internal.make_client(collection_name, app_context)
106+
pub fn make_client(&self, collection_name: String) -> ApiResult<Arc<RemoteSettingsClient>> {
107+
self.internal.make_client(collection_name)
112108
}
113109

114110
/// Sync collections for all active clients
@@ -212,7 +208,7 @@ impl RemoteSettingsClient {
212208
base_url: Url,
213209
bucket_name: String,
214210
collection_name: String,
215-
#[cfg(feature = "jexl")] context: Option<RemoteSettingsContext>,
211+
#[allow(unused)] context: Option<RemoteSettingsContext>,
216212
storage: Storage,
217213
) -> Result<Self> {
218214
Ok(Self {

components/remote_settings/src/service.rs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct RemoteSettingsServiceInner {
2525
storage_dir: Utf8PathBuf,
2626
base_url: Url,
2727
bucket_name: String,
28+
app_context: Option<RemoteSettingsContext>,
2829
/// Weakrefs for all clients that we've created. Note: this stores the
2930
/// top-level/public `RemoteSettingsClient` structs rather than `client::RemoteSettingsClient`.
3031
/// The reason for this is that we return Arcs to the public struct to the foreign code, so we
@@ -50,18 +51,13 @@ impl RemoteSettingsService {
5051
storage_dir,
5152
base_url,
5253
bucket_name,
54+
app_context: config.app_context,
5355
clients: vec![],
5456
}),
5557
})
5658
}
5759

58-
/// Create a new Remote Settings client
59-
#[cfg(feature = "jexl")]
60-
pub fn make_client(
61-
&self,
62-
collection_name: String,
63-
context: Option<RemoteSettingsContext>,
64-
) -> Result<Arc<RemoteSettingsClient>> {
60+
pub fn make_client(&self, collection_name: String) -> Result<Arc<RemoteSettingsClient>> {
6561
let mut inner = self.inner.lock();
6662
// Allow using in-memory databases for testing of external crates.
6763
let storage = if inner.storage_dir == ":memory:" {
@@ -74,32 +70,7 @@ impl RemoteSettingsService {
7470
inner.base_url.clone(),
7571
inner.bucket_name.clone(),
7672
collection_name.clone(),
77-
context,
78-
storage,
79-
)?);
80-
inner.clients.push(Arc::downgrade(&client));
81-
Ok(client)
82-
}
83-
84-
#[cfg(not(feature = "jexl"))]
85-
pub fn make_client(
86-
&self,
87-
collection_name: String,
88-
#[allow(unused_variables)] context: Option<RemoteSettingsContext>,
89-
) -> Result<Arc<RemoteSettingsClient>> {
90-
let mut inner = self.inner.lock();
91-
// Allow using in-memory databases for testing of external crates.
92-
let storage = if inner.storage_dir == ":memory:" {
93-
Storage::new(Utf8PathBuf::from(
94-
"file:remote-settings-{collection-name}?mode=memory&cache=shared".to_string(),
95-
))?
96-
} else {
97-
Storage::new(inner.storage_dir.join(format!("{collection_name}.sql")))?
98-
};
99-
let client = Arc::new(RemoteSettingsClient::new(
100-
inner.base_url.clone(),
101-
inner.bucket_name.clone(),
102-
collection_name.clone(),
73+
inner.app_context.clone(),
10374
storage,
10475
)?);
10576
inner.clients.push(Arc::downgrade(&client));

components/search/src/selector.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
};
1212
use error_support::handle_error;
1313
use parking_lot::Mutex;
14-
use remote_settings::{RemoteSettingsClient, RemoteSettingsContext, RemoteSettingsService};
14+
use remote_settings::{RemoteSettingsClient, RemoteSettingsService};
1515
use std::sync::Arc;
1616

1717
#[derive(Default)]
@@ -47,11 +47,10 @@ impl SearchEngineSelector {
4747
pub fn use_remote_settings_server(
4848
self: Arc<Self>,
4949
service: &Arc<RemoteSettingsService>,
50-
options: Option<RemoteSettingsContext>,
5150
#[allow(unused_variables)] apply_engine_overrides: bool,
5251
) -> SearchApiResult<()> {
5352
self.0.lock().search_config_client =
54-
Some(service.make_client("search-config-v2".to_string(), options)?);
53+
Some(service.make_client("search-config-v2".to_string())?);
5554
Ok(())
5655
}
5756

@@ -1631,19 +1630,14 @@ mod tests {
16311630
url: mockito::server_url(),
16321631
}),
16331632
bucket_name: Some(String::from("main")),
1633+
app_context: Some(RemoteSettingsContext::default()),
16341634
};
16351635
let service =
16361636
Arc::new(RemoteSettingsService::new(String::from(":memory:"), config).unwrap());
16371637

16381638
let selector = Arc::new(SearchEngineSelector::new());
16391639

1640-
let settings_result = Arc::clone(&selector).use_remote_settings_server(
1641-
&service,
1642-
Some(RemoteSettingsContext {
1643-
..Default::default()
1644-
}),
1645-
false,
1646-
);
1640+
let settings_result = Arc::clone(&selector).use_remote_settings_server(&service, false);
16471641
assert!(
16481642
settings_result.is_ok(),
16491643
"Should have set the client successfully. {:?}",

examples/remote-settings-cli/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ fn build_service(cli: &Cli) -> Result<RemoteSettingsService> {
116116
RemoteSettingsServerArg::Prod => RemoteSettingsServer::Prod,
117117
}),
118118
bucket_name: cli.bucket.clone(),
119+
app_context: None,
119120
};
120121
let storage_dir = cli
121122
.storage_dir
@@ -128,7 +129,7 @@ fn sync(service: RemoteSettingsService, collections: Vec<String>) -> Result<()>
128129
// Create a bunch of clients so that sync() syncs their collections
129130
let _clients = collections
130131
.into_iter()
131-
.map(|collection| Ok(service.make_client(collection, None)?))
132+
.map(|collection| Ok(service.make_client(collection)?))
132133
.collect::<Result<Vec<_>>>()?;
133134
service.sync()?;
134135
Ok(())
@@ -139,7 +140,7 @@ fn get_records(
139140
collection: String,
140141
sync_if_empty: bool,
141142
) -> Result<()> {
142-
let client = service.make_client(collection, None)?;
143+
let client = service.make_client(collection)?;
143144
match client.get_records(sync_if_empty) {
144145
Some(records) => {
145146
for record in records {

0 commit comments

Comments
 (0)