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
use std::future::Future;
use std::sync::Arc;
use tokio::sync::{mpsc, Mutex};
use tokio::task::{JoinSet, LocalSet};

use crate::shard::InternalJoinSetResult;
use crate::{ServiceData, UpstreamError};

struct DataCommitGuard<'a, Key: Send + 'static, Data: ServiceData> {
    key: Option<Key>,
    internal_join_set: &'a mut JoinSet<InternalJoinSetResult<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: Send + 'static, Data: ServiceData> {
    data: &'a mut Data,
    commit_guard: DataCommitGuard<'a, Key, Data>,
}

impl<'a, Key: Send, Data: ServiceData> DataCommitRequest<'a, Key, Data> {
    pub(crate) fn new(
        key: Key,
        data: &'a mut Data,
        internal_join_set: &'a mut JoinSet<InternalJoinSetResult<Key, Data>>,
    ) -> Self {
        Self {
            commit_guard: DataCommitGuard {
                key: Some(key),
                internal_join_set,
            },
            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<'a, 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<'a, Key: Send + 'static, Data: ServiceData> DataCommitRequest<'a, Key, Data> {
    pub fn spawn<F: Future<Output = Result<(), UpstreamError>> + Send + 'static>(self, fut: F) {
        self.into_processing().spawn(fut);
    }
}

impl<'a, Key: Send, Data: ServiceData + Clone> DataCommitRequest<'a, Key, Data> {
    pub fn into_owned(self) -> OwnedDataCommitRequest<'a, 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<'a, Key: Send + 'static, Data: ServiceData> {
    data: Data,
    commit_guard: DataCommitGuard<'a, Key, Data>,
}

impl<'a, Key: Send, Data: ServiceData> OwnedDataCommitRequest<'a, Key, Data> {
    pub fn into_inner(self) -> (Data, ProcessingDataCommitRequest<'a, 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<'a, Key: Send + 'static, Data: ServiceData> OwnedDataCommitRequest<'a, Key, Data> {
    pub fn spawn<
        R: Future<Output = Result<(), UpstreamError>> + Send + 'static,
        F: FnOnce(&Key, Data) -> R + Send + 'static,
    >(
        mut self,
        func: F,
    ) {
        let key = self.commit_guard.take_key();
        self.commit_guard.internal_join_set.spawn(async move {
            let fut = (func)(&key, self.data);
            match fut.await {
                Ok(()) => InternalJoinSetResult::DataCommitResult(key, Ok(())),
                Err(err) => InternalJoinSetResult::DataCommitResult(key, Err(err)),
            }
        });
    }
}

#[must_use = "the data commit request must be resolved or rejected, otherwise the operation will be considered aborted."]
pub struct ProcessingDataCommitRequest<'a, Key: Send + 'static, Data: ServiceData> {
    drop_guard: DataCommitGuard<'a, Key, Data>,
}

impl<'a, Key: Send, Data: ServiceData> ProcessingDataCommitRequest<'a, 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<'a, Key: Send + 'static, Data: ServiceData> ProcessingDataCommitRequest<'a, Key, Data> {
    pub fn spawn<F: Future<Output = Result<(), UpstreamError>> + Send + 'static>(mut self, fut: F) {
        let key = self.drop_guard.take_key();
        self.drop_guard.internal_join_set.spawn(async move {
            match fut.await {
                Ok(()) => InternalJoinSetResult::DataCommitResult(key, Ok(())),
                Err(err) => InternalJoinSetResult::DataCommitResult(key, Err(err)),
            }
        });
    }
}

impl<'a, Key: Send, Data: ServiceData> DataCommitGuard<'a, Key, Data> {
    fn key(&self) -> &Key {
        self.key.as_ref().unwrap()
    }

    fn take_key(&mut self) -> Key {
        self.key
            .take()
            .expect("invariant: key must be present, unless dropped.")
    }

    fn emit_result_async(&mut self, result: InternalJoinSetResult<Key, Data>) {
        self.internal_join_set.spawn(async move { result });
    }

    fn resolve(mut self) {
        let key = self.take_key();
        self.emit_result_async(InternalJoinSetResult::DataCommitResult(key, Ok(())));
    }

    fn reject<E: Into<UpstreamError>>(mut self, error: E) {
        let key = self.take_key();
        self.emit_result_async(InternalJoinSetResult::DataCommitResult(
            key,
            Err(error.into()),
        ));
    }
}

impl<Key: Send, Data: ServiceData> Drop for DataCommitGuard<'_, Key, Data> {
    fn drop(&mut self) {
        if let Some(key) = self.key.take() {
            self.emit_result_async(InternalJoinSetResult::DataCommitResult(
                key,
                Err(UpstreamError::OperationAborted),
            ));
        }
    }
}