1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
// Copyright 2020 Palantir Technologies, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! A simple wrapper around a value that changes over time. //! //! A `Refreshable` provides access to both the current value and also ways to be notified of changes made in the //! future. Users can *subscribe* to the refreshable, registering a callback which is invoked whenever the value //! changes. Additionally, users can *map* a refreshable of one type to a refreshable of another type, with the new //! refreshable being updated based on changes to the original refreshable. For example, a caching component of a //! service may map a `Refreshable<ServiceConfiguration>` down to a `Refreshable<CacheConfiguration>` so it can //! subscribe specifically to only the configuration changes that matter to it. //! //! A `Subscription` is returned when subscribing to a refreshable which acts as a guard type, unregistering the //! subscription when dropped. If you intend the subscription to last for the lifetime of the refreshable, you can use //! the `Subscription::leak` method to allow the `Subscription` to fall out of scope without unregistering. //! //! A `RefreshHandle` is returned when creating a new `Refreshable` which is used to update its value. Subscriptions //! are fallible, and all errors encountered when running subscriptions in response to an update are reported through //! the `RefreshHandle::refresh` method. //! //! # Examples //! //! ``` //! use refreshable::Refreshable; //! //! #[derive(PartialEq)] //! struct ServiceConfiguration { //! cache: CacheConfiguration, //! some_other_thing: u32, //! } //! //! #[derive(PartialEq, Clone)] //! struct CacheConfiguration { //! size: usize, //! } //! //! let initial_config = ServiceConfiguration { //! cache: CacheConfiguration { //! size: 10, //! }, //! some_other_thing: 5, //! }; //! let (refreshable, mut handle) = Refreshable::new(initial_config); //! //! let cache_refreshable = refreshable.map(|config| config.cache.clone()); //! //! let subscription = cache_refreshable.subscribe(|cache| { //! if cache.size == 0 { //! Err("cache size must be positive") //! } else { //! println!("new cache size is {}", cache.size); //! Ok(()) //! } //! }).unwrap(); //! //! let new_config = ServiceConfiguration { //! cache: CacheConfiguration { //! size: 20, //! }, //! some_other_thing: 5, //! }; //! // "new cache size is 20" is printed. //! handle.refresh(new_config).unwrap(); //! //! let new_config = ServiceConfiguration { //! cache: CacheConfiguration { //! size: 20, //! }, //! some_other_thing: 10, //! }; //! // nothing is printed since the cache configuration did not change. //! handle.refresh(new_config).unwrap(); //! //! drop(subscription); //! let new_config = ServiceConfiguration { //! cache: CacheConfiguration { //! size: 0, //! }, //! some_other_thing: 10, //! }; //! // nothing is printed since the the cache subscription was dropped. //! handle.refresh(new_config).unwrap(); //! ``` #![doc(html_root_url = "https://docs.rs/refreshable/1")] #![warn(clippy::all, missing_docs)] use arc_swap::ArcSwap; use parking_lot::Mutex; use std::collections::HashMap; use std::marker::PhantomData; use std::ops::Deref; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::Arc; #[cfg(test)] mod test; struct RawCallback<F: ?Sized> { _cleanup: Option<Arc<dyn Drop + Sync + Send>>, // We need to run failing callbacks again on a refresh even if the value didn't change. Otherwise, you can "lose" // errors when the refreshable update to the same value. ok: AtomicBool, callback: F, } type Callback<T, E> = RawCallback<dyn Fn(&T, &mut Vec<E>) + Sync + Send>; struct Shared<T, E> { value: ArcSwap<T>, // This guards subscription registration against concurrent refreshes. If that happened, the subscription callback // could miss updates that happen after the subscription is called with the "current" value but before it is // inserted in the callbacks map. update_lock: Mutex<()>, #[allow(clippy::type_complexity)] callbacks: Mutex<Arc<HashMap<u64, Arc<Callback<T, E>>>>>, } /// A wrapper around a live-refreshable value. pub struct Refreshable<T, E> { shared: Arc<Shared<T, E>>, next_id: AtomicU64, // This is used to unsubscribe a mapped refreshable from its parent refreshable. A copy of the Arc is held in the // refreshable itself, along with every subscription of the mapped refreshable. The inner dyn Drop is a Subscription // type. cleanup: Option<Arc<dyn Drop + Sync + Send>>, } impl<T, E> Refreshable<T, E> where T: PartialEq + 'static + Sync + Send, E: 'static, { /// Creates a new `Refreshable` with an initial value, returning it along with a `RefreshHandle` used to update it /// with new values. pub fn new(value: T) -> (Refreshable<T, E>, RefreshHandle<T, E>) { let shared = Arc::new(Shared { value: ArcSwap::new(Arc::new(value)), update_lock: Mutex::new(()), callbacks: Mutex::new(Arc::new(HashMap::new())), }); ( Refreshable { shared: shared.clone(), next_id: AtomicU64::new(0), cleanup: None, }, RefreshHandle { shared }, ) } /// Returns a guard type providing access to a snapshot of the refreshable's current value. #[inline] pub fn get(&self) -> Guard<'_, T> { Guard { inner: self.shared.value.load(), _p: PhantomData, } } /// Subscribes to the refreshable. /// /// The callback will be invoked every time the refreshable's value changes, and is also called synchronously when /// this method is called with the current value. If the callback returns `Ok`, a `Subscription` object is returned /// that will unsubscribe from the refreshable when it drops. If the callback returns `Err`, this method will return /// the error and the callback will *not* be invoked on updates to the value. pub fn subscribe<F>(&self, callback: F) -> Result<Subscription<T, E>, E> where F: Fn(&T) -> Result<(), E> + 'static + Sync + Send, { let _guard = self.shared.update_lock.lock(); callback(&self.get())?; let subscription = self.subscribe_raw(move |value, errors| { if let Err(e) = callback(value) { errors.push(e); } }); Ok(subscription) } /// Subscribes to the refreshable with an infallible callback. /// /// This is a convenience method to simplify subscription when the callback can never fail. pub fn subscribe_ok<F>(&self, callback: F) -> Subscription<T, E> where F: Fn(&T) + 'static + Sync + Send, { self.subscribe(move |value| { callback(value); Ok(()) }) .ok() .unwrap() } fn subscribe_raw<F>(&self, callback: F) -> Subscription<T, E> where F: Fn(&T, &mut Vec<E>) + 'static + Sync + Send, { let id = self.next_id.fetch_add(1, Ordering::Relaxed); let callback = Arc::new(RawCallback { _cleanup: self.cleanup.clone(), ok: AtomicBool::new(true), callback, }); Arc::make_mut(&mut *self.shared.callbacks.lock()).insert(id, callback); Subscription { shared: self.shared.clone(), id, live: true, } } /// Creates a new refreshable from this one by applying a mapping function to the value. /// /// This can be used to narrow the scope of the refreshable value. Updates to the initial refreshable value will /// propagate to the mapped refreshable value, but the mapped refreshable's subscriptions will only be invoked if /// the mapped value actually changed. pub fn map<F, R>(&self, map: F) -> Refreshable<R, E> where F: Fn(&T) -> R + 'static + Sync + Send, R: PartialEq + 'static + Sync + Send, { let _guard = self.shared.update_lock.lock(); let (mut refreshable, handle) = Refreshable::new(map(&self.get())); let subscription = self.subscribe_raw(move |value, errors| handle.refresh_raw(map(value), errors)); refreshable.cleanup = Some(Arc::new(subscription)); refreshable } } /// A subscription to a `Refreshable` value. /// /// The associated subscription is unregistered when this value is dropped, unless the `Subscription::leak` method is /// used. #[must_use = "the associated subscription is unregistered when this value is dropped"] pub struct Subscription<T, E> { shared: Arc<Shared<T, E>>, id: u64, live: bool, } impl<T, E> Drop for Subscription<T, E> { fn drop(&mut self) { if self.live { Arc::make_mut(&mut *self.shared.callbacks.lock()).remove(&self.id); } } } impl<T, E> Subscription<T, E> { /// Destroys the guard without unregistering its associated subscription. pub fn leak(mut self) { self.live = false; } } /// A handle that can update the value associated with a refreshable. pub struct RefreshHandle<T, E> { shared: Arc<Shared<T, E>>, } impl<T, E> RefreshHandle<T, E> where T: PartialEq + 'static + Sync + Send, { /// Updates the refreshable's value. /// /// If the new value is equal to the refreshable's current value, the method returns immediately. Otherwise, it /// runs all registered subscriptions, collecting any errors and returning them all when finished. // NB: It's important that this takes &mut self. That way, all the way down through the tree of mapped refreshables, // we don't need to worry about concurrent refreshes. refresh_raw below only takes &self to work more easily with // the map implementation, but that's only triggered by a call to refresh at the root refreshable. pub fn refresh(&mut self, new_value: T) -> Result<(), Vec<E>> { let mut errors = vec![]; self.refresh_raw(new_value, &mut errors); if errors.is_empty() { Ok(()) } else { Err(errors) } } fn refresh_raw(&self, new_value: T, errors: &mut Vec<E>) { // We could avoid updating the inner value when it hasn't changed but the complexity doesn't seem worth it. let value_changed = new_value != **self.shared.value.load(); let guard = self.shared.update_lock.lock(); self.shared.value.store(Arc::new(new_value)); let value = self.shared.value.load(); let callbacks = self.shared.callbacks.lock().clone(); drop(guard); for callback in callbacks.values() { if value_changed || !callback.ok.load(Ordering::SeqCst) { let nerrors = errors.len(); (callback.callback)(&value, errors); callback.ok.store(errors.len() == nerrors, Ordering::SeqCst); } } } } /// A guard type providing access to a snapshot of a refreshable's current value. pub struct Guard<'a, T> { inner: arc_swap::Guard<Arc<T>>, // the arc_swap guard doesn't borrow from its ArcSwap, but we don't want to expose that fact in the public API _p: PhantomData<&'a ()>, } impl<T> Deref for Guard<'_, T> { type Target = T; #[inline] fn deref(&self) -> &T { &*self.inner } }