@@ -2,6 +2,7 @@ use std::fmt;
22use std:: sync:: Arc ;
33use std:: time:: Duration ;
44
5+ use arc_swap:: ArcSwap ;
56use chrono:: Utc ;
67use parking_lot:: Mutex ;
78use 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
124143impl 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