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
//! Watch-related APIs
use std::{pin::Pin, time::Duration};

use crate::{
    model::{Query, Revision, WatchFileResult, WatchRepoResult, Watchable},
    services::{path, status_unwrap},
    Client, Error, RepoClient,
};

use futures::{Stream, StreamExt};
use reqwest::{Method, Request, StatusCode};

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
const DELAY_ON_SUCCESS: Duration = Duration::from_secs(1);
const MAX_FAILED_COUNT: usize = 5; // Max base wait time 2 << 5 = 64 secs
const JITTER_RATE: f32 = 0.2;

async fn request_watch<D: Watchable>(client: &Client, req: Request) -> Result<Option<D>, Error> {
    let resp = client.request(req).await?;
    if resp.status() == StatusCode::NOT_MODIFIED {
        return Ok(None);
    }
    let ok_resp = status_unwrap(resp).await?;
    let result = ok_resp.json().await?;

    Ok(Some(result))
}

fn delay_time_for(failed_count: usize) -> Duration {
    let base_time_ms = (2 << failed_count) * 1000;
    let jitter = (fastrand::f32() * JITTER_RATE * base_time_ms as f32) as u64;

    Duration::from_millis(base_time_ms + jitter)
}

struct WatchState {
    client: Client,
    path: String,
    last_known_revision: Option<Revision>,
    failed_count: usize,
    success_delay: Option<Duration>,
}

fn watch_stream<D: Watchable>(client: Client, path: String) -> impl Stream<Item = D> + Send {
    let init_state = WatchState {
        client,
        path,
        last_known_revision: None,
        failed_count: 0,
        success_delay: None,
    };
    futures::stream::unfold(init_state, |mut state| async move {
        if let Some(d) = state.success_delay.take() {
            tokio::time::sleep(d).await;
        }
        loop {
            let req = match state.client.new_watch_request(
                Method::GET,
                &state.path,
                None,
                state.last_known_revision,
                DEFAULT_TIMEOUT,
            ) {
                Ok(r) => r,
                Err(_) => {
                    return None;
                }
            };

            let resp: Result<Option<D>, _> = request_watch(&state.client, req).await;
            let next_delay = match resp {
                // Send Ok data out
                Ok(Some(watch_result)) => {
                    state.last_known_revision = Some(watch_result.revision());
                    state.failed_count = 0;
                    state.success_delay = Some(DELAY_ON_SUCCESS);

                    return Some((watch_result, state));
                }
                Ok(None) => {
                    state.failed_count = 0;
                    Duration::from_secs(1)
                }
                Err(Error::HttpClient(e)) if e.is_timeout() => Duration::from_secs(1),
                Err(e) => {
                    log::debug!("Request error: {}", e);
                    if state.failed_count < MAX_FAILED_COUNT {
                        state.failed_count += 1;
                    }
                    delay_time_for(state.failed_count)
                }
            };
            // Delay
            tokio::time::sleep(next_delay).await;
        }
    })
}

/// Watch-related APIs
pub trait WatchService {
    /// Returns a stream which output a [`WatchFileResult`] when the result of the
    /// given [`Query`] becomes available or changes
    fn watch_file_stream(
        &self,
        query: &Query,
    ) -> Result<Pin<Box<dyn Stream<Item = WatchFileResult> + Send>>, Error>;

    /// Returns a stream which output a [`WatchRepoResult`] when the repository has a new commit
    /// that contains the changes for the files matched by the given `path_pattern`.
    fn watch_repo_stream(
        &self,
        path_pattern: &str,
    ) -> Result<Pin<Box<dyn Stream<Item = WatchRepoResult> + Send>>, Error>;
}

impl<'a> WatchService for RepoClient<'a> {
    fn watch_file_stream(
        &self,
        query: &Query,
    ) -> Result<Pin<Box<dyn Stream<Item = WatchFileResult> + Send>>, Error> {
        let p = path::content_watch_path(self.project, self.repo, query);

        Ok(watch_stream(self.client.clone(), p).boxed())
    }

    fn watch_repo_stream(
        &self,
        path_pattern: &str,
    ) -> Result<Pin<Box<dyn Stream<Item = WatchRepoResult> + Send>>, Error> {
        let p = path::repo_watch_path(self.project, self.repo, path_pattern);

        Ok(watch_stream(self.client.clone(), p).boxed())
    }
}

#[cfg(test)]
mod test {
    use std::sync::atomic::{AtomicBool, Ordering};

    use super::*;
    use crate::model::{Entry, EntryContent};
    use wiremock::{
        matchers::{header, method, path},
        Mock, MockServer, Respond, ResponseTemplate,
    };

    struct MockResponse {
        first_time: AtomicBool,
    }

    impl Respond for MockResponse {
        fn respond(&self, _req: &wiremock::Request) -> ResponseTemplate {
            if self.first_time.swap(false, Ordering::SeqCst) {
                println!("Called 1");
                ResponseTemplate::new(304).set_delay(Duration::from_millis(100))
            } else {
                println!("Called 2");
                let resp = r#"{
                    "revision":3,
                    "entry":{
                        "path":"/a.json",
                        "type":"JSON",
                        "content": {"a":"b"},
                        "revision":3,
                        "url": "/api/v1/projects/foo/repos/bar/contents/a.json"
                    }
                }"#;
                ResponseTemplate::new(200)
                    .set_delay(Duration::from_millis(100))
                    .set_body_raw(resp, "application/json")
            }
        }
    }

    #[tokio::test]
    async fn test_watch_file() {
        let server = MockServer::start().await;
        let resp = MockResponse {
            first_time: AtomicBool::new(true),
        };

        Mock::given(method("GET"))
            .and(path("/api/v1/projects/foo/repos/bar/contents/a.json"))
            .and(header("if-none-match", "-1"))
            .and(header("prefer", "wait=60"))
            .and(header("Authorization", "Bearer anonymous"))
            .respond_with(resp)
            .expect(2)
            .mount(&server)
            .await;

        let client = Client::new(&server.uri(), None).await.unwrap();
        let stream = client
            .repo("foo", "bar")
            .watch_file_stream(&Query::identity("/a.json").unwrap())
            .unwrap()
            .take_until(tokio::time::sleep(Duration::from_secs(3)));
        tokio::pin!(stream);

        let result = stream.next().await;

        server.reset().await;
        let result = result.unwrap();
        assert_eq!(result.revision, Revision::from(3));
        assert_eq!(
            result.entry,
            Entry {
                path: "/a.json".to_string(),
                content: EntryContent::Json(serde_json::json!({"a":"b"})),
                revision: Revision::from(3),
                url: "/api/v1/projects/foo/repos/bar/contents/a.json".to_string(),
                modified_at: None,
            }
        );
    }
}