Skip to content

Commit 3768208

Browse files
committed
feat(analytics-sdk): swappable installation ID via set_installation_id
1 parent bf9dbfc commit 3768208

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/modrinth-app-analytics-sdk/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name = "modrinth-app-analytics-sdk"
33
edition.workspace = true
44

55
[dependencies]
6+
arc-swap = { workspace = true }
67
chrono = { workspace = true }
78
modrinth-app-analytics-types = { workspace = true }
89
parking_lot = { workspace = true }

‎packages/modrinth-app-analytics-sdk/src/client.rs‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::fmt;
22
use std::sync::Arc;
33
use std::time::Duration;
44

5+
use arc_swap::ArcSwap;
56
use chrono::Utc;
67
use parking_lot::Mutex;
78
use reqwest::Url;
@@ -40,11 +41,7 @@ impl AppAnalytics {
4041
app_version,
4142
}: AppAnalyticsParameters,
4243
) -> AppAnalytics {
43-
assert_eq!(
44-
installation_id.get_version_num(),
45-
4,
46-
"installation_id must be a UUIDv4"
47-
);
44+
assert_installation_id(installation_id);
4845

4946
let (drain_tx, drain_rx) = mpsc::unbounded_channel();
5047

@@ -59,7 +56,7 @@ impl AppAnalytics {
5956
}),
6057
user_agent,
6158
push_url,
62-
installation_id,
59+
installation_id: ArcSwap::from_pointee(installation_id),
6360
queue: EventQueue::new(),
6461
os,
6562
arch,
@@ -119,14 +116,36 @@ impl AppAnalytics {
119116
};
120117
}
121118
}
119+
120+
/// Replaces the installation ID sent with every future batch.
121+
///
122+
/// Events still queued are dropped, so none recorded under the old ID
123+
/// are sent under the new one.
124+
///
125+
/// # Panics
126+
///
127+
/// Panics if `installation_id` is not a UUIDv4.
128+
pub fn set_installation_id(&self, installation_id: Uuid) {
129+
assert_installation_id(installation_id);
130+
self.inner.installation_id.store(Arc::new(installation_id));
131+
self.inner.queue.clear();
132+
}
133+
}
134+
135+
fn assert_installation_id(installation_id: Uuid) {
136+
assert_eq!(
137+
installation_id.get_version_num(),
138+
4,
139+
"installation_id must be a UUIDv4"
140+
);
122141
}
123142

124143
impl fmt::Debug for AppAnalytics {
125144
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126145
f.debug_struct("AppAnalytics")
127146
.field("push_url", &self.inner.push_url)
128147
.field("backend", &self.inner.backend)
129-
.field("installation_id", &self.inner.installation_id)
148+
.field("installation_id", &**self.inner.installation_id.load())
130149
.finish()
131150
}
132151
}
@@ -147,7 +166,7 @@ pub(crate) struct AnalyticsInner {
147166
backend: Mutex<AnalyticsBackend>,
148167
pub(crate) queue: EventQueue,
149168
user_agent: String,
150-
installation_id: Uuid,
169+
installation_id: ArcSwap<Uuid>,
151170
os: AppOs,
152171
arch: AppArch,
153172
app_version: SmolStr,
@@ -172,7 +191,7 @@ impl AnalyticsInner {
172191

173192
let batch = AppEventBatch {
174193
sent_at: Utc::now(),
175-
installation_id: self.installation_id,
194+
installation_id: **self.installation_id.load(),
176195
os: self.os,
177196
arch: self.arch,
178197
app_version: self.app_version.clone(), // should be cheap

0 commit comments

Comments
 (0)