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
use holochain_tracing::Span;
use std::collections::HashMap;

use crate::{ghost_error::ErrorKind, Backtwrap, GhostError, GhostResult, RequestId, WorkWasDone};

const DEFAULT_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(60000); // TODO - should be 2000 or less but tests currently fail if below that

/// a ghost request callback can be invoked with a response that was injected
/// into the system through the `handle` pathway, or to indicate a failure
/// such as a timeout
#[derive(Debug, Clone)]
pub enum GhostCallbackData<CbData: 'static, E: 'static> {
    Response(Result<CbData, E>),
    Timeout(Backtwrap),
}

/// definition for a ghost request callback
/// note, the callback can be registered as `'static` because the code
/// definition itself doesn't depend on any specific instance lifetime
/// if you want to mutate the state of a struct instance, pass it in
/// with the `handle` or `process` call.
/// (see detach crate for help with self refs)
pub type GhostCallback<UserData, CbData, E> =
    Box<dyn FnOnce(&mut UserData, GhostCallbackData<CbData, E>) -> GhostResult<()> + 'static>;

/// this internal struct helps us keep track of the context and timeout
/// for a callback that was bookmarked in the tracker
struct GhostTrackerEntry<UserData, CbData: 'static, E: 'static> {
    backtrace: Backtwrap,
    expires: std::time::SystemTime,
    cb: GhostCallback<UserData, CbData, E>,
}

#[derive(Debug, Clone)]
pub struct GhostTrackerBuilder {
    request_id_prefix: String,
    default_timeout: std::time::Duration,
}

impl Default for GhostTrackerBuilder {
    fn default() -> Self {
        Self {
            request_id_prefix: "".to_string(),
            default_timeout: DEFAULT_TIMEOUT,
        }
    }
}

impl GhostTrackerBuilder {
    pub fn build<UserData, CbData: 'static, E: 'static>(self) -> GhostTracker<UserData, CbData, E> {
        GhostTracker {
            request_id_prefix: self.request_id_prefix,
            default_timeout: self.default_timeout,
            pending: HashMap::new(),
        }
    }

    pub fn request_id_prefix(mut self, request_id_prefix: &str) -> Self {
        self.request_id_prefix = request_id_prefix.to_string();
        self
    }

    pub fn default_timeout(mut self, default_timeout: std::time::Duration) -> Self {
        self.default_timeout = default_timeout;
        self
    }
}

/// GhostTracker registers callbacks associated with request_ids
/// that can be triggered later when a response comes back indicating that id
pub struct GhostTracker<UserData, CbData: 'static, E: 'static> {
    request_id_prefix: String,
    default_timeout: std::time::Duration,
    pending: HashMap<RequestId, GhostTrackerEntry<UserData, CbData, E>>,
}

#[derive(Debug, Clone)]
pub struct GhostTrackerBookmarkOptions {
    pub timeout: Option<std::time::Duration>,
}

impl Default for GhostTrackerBookmarkOptions {
    fn default() -> Self {
        Self { timeout: None }
    }
}

impl GhostTrackerBookmarkOptions {
    pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }
}

impl<UserData, CbData: 'static, E: 'static> GhostTracker<UserData, CbData, E> {
    /// trigger any periodic or delayed callbacks
    /// also check / cleanup any timeouts
    pub fn process(&mut self, ga: &mut UserData) -> GhostResult<WorkWasDone> {
        let mut expired = Vec::new();

        let now = std::time::SystemTime::now();

        let did_work = !self.pending.is_empty();
        for (request_id, entry) in self.pending.iter() {
            if now > entry.expires {
                expired.push(request_id.clone())
            }
        }

        for request_id in expired {
            match self.pending.remove(&request_id) {
                None => (),
                Some(entry) => {
                    (entry.cb)(ga, GhostCallbackData::Timeout(entry.backtrace))?;
                }
            }
        }

        Ok(did_work.into())
    }

    /// register a callback
    pub fn bookmark(&mut self, span: Span, cb: GhostCallback<UserData, CbData, E>) -> RequestId {
        self.bookmark_options(span, cb, GhostTrackerBookmarkOptions::default())
    }

    /// register a callback, using a specific timeout instead of the default
    pub fn bookmark_options(
        &mut self,
        _span: Span,
        cb: GhostCallback<UserData, CbData, E>,
        options: GhostTrackerBookmarkOptions,
    ) -> RequestId {
        let request_id = RequestId::with_prefix(&self.request_id_prefix);

        let timeout = match options.timeout {
            None => self.default_timeout,
            Some(timeout) => timeout,
        };

        self.pending.insert(
            request_id.clone(),
            GhostTrackerEntry {
                backtrace: Backtwrap::new(),
                expires: std::time::SystemTime::now()
                    .checked_add(timeout)
                    .expect("can add timeout to SystemTime::now()"),
                cb,
            },
        );
        request_id
    }

    /// handle a response
    /// "owner" is meant to be the GhostActor (or other dynamic trait object) that is
    /// tracking for the call back, to get itself back in the callback and to an upcast
    pub fn handle(
        &mut self,
        request_id: RequestId,
        owner: &mut UserData,
        data: Result<CbData, E>,
    ) -> GhostResult<()> {
        match self.pending.remove(&request_id) {
            None => {
                let msg = format!(
                    "{:?} in {:?}",
                    &request_id,
                    self.pending.keys().collect::<Vec<_>>()
                );
                Err(GhostError::new(ErrorKind::RequestIdNotFound(msg)))
            }
            Some(entry) => (entry.cb)(owner, GhostCallbackData::Response(data)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use detach::prelude::*;
    use holochain_tracing::test_span;

    type TestError = String;

    #[derive(Debug)]
    pub struct TestCallbackData(pub String);

    struct TestTrackingActor {
        state: String,
        tracker: Detach<GhostTracker<TestTrackingActor, TestCallbackData, TestError>>,
    }

    impl TestTrackingActor {
        fn new(request_id_prefix: &str) -> Self {
            let tracker_builder =
                GhostTrackerBuilder::default().request_id_prefix(request_id_prefix);
            Self {
                state: "".into(),
                tracker: Detach::new(tracker_builder.build()),
            }
        }
    }

    #[test]
    fn test_ghost_tracker_should_bookmark_and_handle() {
        let mut actor = TestTrackingActor::new("test_request_id_prefix");
        let span = test_span();

        let cb: GhostCallback<TestTrackingActor, TestCallbackData, TestError> =
            Box::new(|me, callback_data| {
                if let GhostCallbackData::Response(Ok(TestCallbackData(payload))) = callback_data {
                    me.state = payload;
                }
                Ok(())
            });

        // lets bookmark a callback that should set our actors state to the value
        // of the callback response
        let req_id = actor.tracker.bookmark(span, cb);

        let _entry = actor.tracker.pending.get(&req_id).unwrap();

        // the state should be empty from the new
        assert_eq!(actor.state, "");
        // after handling
        detach_run!(&mut actor.tracker, |tracker| {
            let result = tracker.handle(
                req_id.clone(),
                &mut actor,
                Ok(TestCallbackData("here's the data!".into())),
            );
            assert_eq!("Ok(())", format!("{:?}", result))
        });
        assert_eq!(actor.state, "here's the data!");

        // try again and this time we should get that the request ID wasn't found
        detach_run!(&mut actor.tracker, |tracker| {
            let result = tracker.handle(
                req_id,
                &mut actor,
                Ok(TestCallbackData("here's the data again!".into())),
            );
            assert_eq!(
                b"Err(GhostError(RequestIdNotFound",
                &format!("{:?}", result).as_bytes()[..32],
            )
        });
    }

    #[test]
    fn test_ghost_tracker_should_timeout() {
        let mut actor = TestTrackingActor::new("test_request_id_prefix");
        let span = test_span();
        let cb: GhostCallback<TestTrackingActor, TestCallbackData, TestError> =
            Box::new(|me, callback_data| {
                // when the timeout happens the callback should get
                // the timeout enum in the callback_data
                match callback_data {
                    GhostCallbackData::Timeout(_) => me.state = "timed_out".into(),
                    _ => assert!(false),
                }
                Ok(())
            });
        let _req_id = actor.tracker.bookmark_options(
            span,
            cb,
            GhostTrackerBookmarkOptions::default().timeout(std::time::Duration::from_millis(1)),
        );
        assert_eq!(actor.tracker.pending.len(), 1);

        // wait 1 ms for the callback to have expired
        std::thread::sleep(std::time::Duration::from_millis(1));
        detach_run!(&mut actor.tracker, |tracker| {
            let result = tracker.process(&mut actor);
            assert_eq!("Ok(WorkWasDone(true))", format!("{:?}", result));
        });
        assert_eq!(actor.state, "timed_out");
        assert_eq!(actor.tracker.pending.len(), 0);
    }
}