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
use std::future::Future; use tokio::sync::mpsc; use crate::shard::InternalMessage; use crate::{ServiceData, UpstreamError}; struct DataCommitGuard<Key, Data> { key: Option<Key>, sender: mpsc::UnboundedSender<InternalMessage<Key, Data>>, } #[must_use = "the data commit request must be resolved or rejected, otherwise the operation will be considered aborted."] pub struct DataCommitRequest<'a, Key, Data> { data: &'a mut Data, commit_guard: DataCommitGuard<Key, Data>, } impl<'a, Key, Data> DataCommitRequest<'a, Key, Data> { pub(crate) fn new( sender: mpsc::UnboundedSender<InternalMessage<Key, Data>>, key: Key, data: &'a mut Data, ) -> Self { Self { commit_guard: DataCommitGuard { key: Some(key), sender, }, data, } } pub fn data_mut(&mut self) -> &mut Data { self.data } pub fn data(&self) -> &Data { self.data } pub fn key(&self) -> &Key { self.commit_guard.key() } pub fn into_processing(self) -> ProcessingDataCommitRequest<Key, Data> { ProcessingDataCommitRequest { drop_guard: self.commit_guard, } } pub fn resolve(self) { self.into_processing().resolve(); } pub fn reject<E: Into<UpstreamError>>(self, error: E) { self.into_processing().reject(error); } } impl<Key: Send + 'static, Data: ServiceData> DataCommitRequest<'_, Key, Data> { pub fn spawn<F: Future<Output = Result<(), UpstreamError>> + Send + 'static>(self, fut: F) { self.into_processing().spawn(fut); } } impl<Key, Data: Clone> DataCommitRequest<'_, Key, Data> { pub fn into_owned(self) -> OwnedDataCommitRequest<Key, Data> { OwnedDataCommitRequest { data: self.data.clone(), commit_guard: self.commit_guard, } } } #[must_use = "the data commit request must be resolved or rejected, otherwise the operation will be considered aborted."] pub struct OwnedDataCommitRequest<Key, Data> { data: Data, commit_guard: DataCommitGuard<Key, Data>, } impl<Key, Data> OwnedDataCommitRequest<Key, Data> { pub fn into_inner(self) -> (Data, ProcessingDataCommitRequest<Key, Data>) { ( self.data, ProcessingDataCommitRequest { drop_guard: self.commit_guard, }, ) } pub fn data(&self) -> &Data { &self.data } pub fn resolve(self) { self.commit_guard.resolve(); } pub fn reject<E: Into<UpstreamError>>(self, error: E) { self.commit_guard.reject(error); } } impl<Key: Send + 'static, Data: ServiceData> OwnedDataCommitRequest<Key, Data> { pub fn spawn< R: Future<Output = Result<(), UpstreamError>> + Send + 'static, F: FnOnce(&Key, Data) -> R + Send + 'static, >( self, func: F, ) { tokio::spawn(async move { let drop_guard = self.commit_guard; let fut = (func)(drop_guard.key.as_ref().unwrap(), self.data); match fut.await { Ok(()) => drop_guard.resolve(), Err(err) => drop_guard.reject(err), }; }); } } #[must_use = "the data commit request must be resolved or rejected, otherwise the operation will be considered aborted."] pub struct ProcessingDataCommitRequest<Key, Data> { drop_guard: DataCommitGuard<Key, Data>, } impl<Key, Data> ProcessingDataCommitRequest<Key, Data> { pub fn resolve(self) { self.drop_guard.resolve(); } pub fn reject<E: Into<UpstreamError>>(self, error: E) { self.drop_guard.reject(error); } } impl<Key: Send + 'static, Data: ServiceData> ProcessingDataCommitRequest<Key, Data> { pub fn spawn<F: Future<Output = Result<(), UpstreamError>> + Send + 'static>(self, fut: F) { tokio::spawn(async move { match fut.await { Ok(()) => self.resolve(), Err(err) => self.reject(err), }; }); } } impl<Key, Data> DataCommitGuard<Key, Data> { fn key(&self) -> &Key { self.key.as_ref().unwrap() } fn resolve(mut self) { let key = self .key .take() .expect("invariant: key must be present, unless dropped."); self.sender .send(InternalMessage::DataCommitResult(key, Ok(()))) .ok(); } fn reject<E: Into<UpstreamError>>(mut self, error: E) { let key = self .key .take() .expect("invariant: key must be present, unless dropped."); self.sender .send(InternalMessage::DataCommitResult(key, Err(error.into()))) .ok(); } } impl<Key, Data> Drop for DataCommitGuard<Key, Data> { fn drop(&mut self) { if let Some(key) = self.key.take() { self.sender .send(InternalMessage::DataCommitResult( key, Err(UpstreamError::OperationAborted), )) .ok(); } } }