clankerdiff-client 0.3.1

Renderer-independent local and remote diff client
Documentation
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use crate::{
    ClientError, ClientOptions, ClientState, ConnectionState, DiffReviewEvent, DiffScope,
    DiffSnapshot, ReconnectPolicy, RemoteError, RepositoryAction, platform,
    protocol::{
        client::{ClientCommand, LocalClientTransport, capabilities},
        server::ServerEvent,
        shared::{Event, LIVE_PROTOCOL_VERSION, RemoteErrorCode},
    },
    transport::{ClientMessageTransport, Decoded, Transport},
};
#[cfg(feature = "websocket")]
use crate::{ConnectionHeader, transport::Reconnecting};
use async_channel::{Receiver, Sender, unbounded};
use futures_util::FutureExt;
use std::{sync::Arc, time::Duration};
use tokio::sync::{oneshot, watch};

const IDLE_TIMEOUT: Duration = Duration::from_secs(45);

type Reply = oneshot::Sender<Result<(), ClientError>>;
type Ready = oneshot::Receiver<Result<(), ClientError>>;

#[derive(Clone)]
pub struct DiffClient {
    commands: Sender<Command>,
    state: watch::Receiver<Arc<ClientState>>,
}

pub struct ClientSubscription {
    state: watch::Receiver<Arc<ClientState>>,
}

impl ClientSubscription {
    #[must_use]
    pub fn latest(&self) -> Arc<ClientState> {
        self.state.borrow().clone()
    }

    pub async fn changed(&mut self) -> Result<Arc<ClientState>, ClientError> {
        self.state
            .changed()
            .await
            .map_err(|_| ClientError::Disconnected)?;
        Ok(self.state.borrow_and_update().clone())
    }

    /// Waits until the published state satisfies `predicate`, or `timeout` elapses.
    ///
    /// # Errors
    ///
    /// Returns [`ClientError::Disconnected`] when the client stops publishing or
    /// the timeout elapses first.
    pub async fn wait_until(
        &mut self,
        timeout: Duration,
        mut predicate: impl FnMut(&ClientState) -> bool,
    ) -> Result<Arc<ClientState>, ClientError> {
        let deadline = platform::sleep(timeout).fuse();
        futures_util::pin_mut!(deadline);
        loop {
            let state = self.latest();
            if predicate(&state) {
                return Ok(state);
            }
            let changed = self.changed().fuse();
            futures_util::pin_mut!(changed);
            futures_util::select! {
                state = changed => { state?; }
                () = deadline => return Err(ClientError::Disconnected),
            }
        }
    }
}

impl DiffClient {
    #[cfg(feature = "websocket")]
    pub async fn connect(url: &str, options: ClientOptions) -> Result<Self, ClientError> {
        Self::connect_with_headers(url, options, Vec::new()).await
    }

    #[cfg(feature = "websocket")]
    pub async fn connect_with_headers(
        url: &str,
        options: ClientOptions,
        headers: Vec<ConnectionHeader>,
    ) -> Result<Self, ClientError> {
        let transport = Reconnecting::connect(url, headers).await?;
        Self::start(transport, options).await
    }

    pub async fn from_transport(
        transport: LocalClientTransport,
        options: ClientOptions,
    ) -> Result<Self, ClientError> {
        Self::start(transport, options).await
    }

    pub async fn from_messages(
        transport: impl ClientMessageTransport,
        options: ClientOptions,
    ) -> Result<Self, ClientError> {
        Self::start(Decoded::new(transport), options).await
    }

    #[must_use]
    pub fn spawn(transport: impl ClientMessageTransport, options: ClientOptions) -> Self {
        Self::start_worker(Decoded::new(transport), options).0
    }

    async fn start(transport: impl Transport, options: ClientOptions) -> Result<Self, ClientError> {
        let (client, ready) = Self::start_worker(transport, options);
        ready.await.map_err(|_| ClientError::Disconnected)??;
        Ok(client)
    }

    fn start_worker(transport: impl Transport, options: ClientOptions) -> (Self, Ready) {
        let (commands, rx) = unbounded();
        let (state_tx, state) = watch::channel(Arc::new(ClientState::default()));
        let (ready_tx, ready_rx) = oneshot::channel();
        platform::spawn(async move {
            Worker {
                commands: rx,
                state_tx,
                options,
                state: WorkerState::default(),
                connection: ConnectionState::Connecting,
                closed: false,
            }
            .run(transport, ready_tx)
            .await;
        });
        (Self { commands, state }, ready_rx)
    }

    #[must_use]
    pub fn state(&self) -> Arc<ClientState> {
        self.state.borrow().clone()
    }

    #[must_use]
    pub fn subscribe(&self) -> ClientSubscription {
        ClientSubscription {
            state: self.state.clone(),
        }
    }

    pub async fn set_scope(&self, scope: DiffScope) -> Result<(), ClientError> {
        self.request(ClientCommand::SetScope(scope)).await
    }

    pub async fn apply(&self, action: RepositoryAction) -> Result<(), ClientError> {
        self.request(ClientCommand::Apply(action)).await
    }

    pub async fn refresh(&self) -> Result<(), ClientError> {
        self.request(ClientCommand::Refresh).await
    }

    pub async fn handle(&self, event: DiffReviewEvent) -> Result<(), ClientError> {
        match self.dispatch(event)? {
            Some(reply) => reply.await.map_err(|_| ClientError::Disconnected)?,
            None => Ok(()),
        }
    }

    pub fn dispatch(
        &self,
        event: DiffReviewEvent,
    ) -> Result<Option<oneshot::Receiver<Result<(), ClientError>>>, ClientError> {
        let request = match event {
            DiffReviewEvent::RepositoryAction(action) => ClientCommand::Apply(action),
            DiffReviewEvent::SetScope(scope) => ClientCommand::SetScope(scope),
            DiffReviewEvent::Refresh => ClientCommand::Refresh,
            DiffReviewEvent::SubmitReview(submission) => ClientCommand::Submit(submission),
            DiffReviewEvent::Cancel => ClientCommand::Cancel,
            DiffReviewEvent::CopyFormattedReview(_) => return Ok(None),
        };
        let (reply, receiver) = oneshot::channel();
        self.commands
            .try_send(Command::Request(request, reply))
            .map_err(|_| ClientError::Disconnected)?;
        Ok(Some(receiver))
    }

    pub async fn close(self) -> Result<(), ClientError> {
        self.command(Command::Close).await
    }

    async fn request(&self, request: ClientCommand) -> Result<(), ClientError> {
        self.command(|reply| Command::Request(request, reply)).await
    }

    async fn command(&self, build: impl FnOnce(Reply) -> Command) -> Result<(), ClientError> {
        let (reply, rx) = oneshot::channel();
        self.commands
            .send(build(reply))
            .await
            .map_err(|_| ClientError::Disconnected)?;
        rx.await.map_err(|_| ClientError::Disconnected)?
    }
}

enum Command {
    Request(ClientCommand, Reply),
    Close(Reply),
}

enum ConnectionInput {
    Command(Option<Command>),
    Event(Result<ServerEvent, ClientError>),
    Idle,
}

struct Pending {
    reply: Reply,
    outcome_unknown_on_disconnect: bool,
}

struct Worker {
    commands: Receiver<Command>,
    state_tx: watch::Sender<Arc<ClientState>>,
    options: ClientOptions,
    state: WorkerState,
    connection: ConnectionState,
    closed: bool,
}

#[derive(Default)]
struct WorkerState {
    snapshot: Option<Arc<DiffSnapshot>>,
    error: Option<RemoteError>,
    pending: Option<Pending>,
    initialized: bool,
}

impl Worker {
    fn publish(&self) {
        let connected = matches!(self.connection, ConnectionState::Connected);
        self.state_tx.send_replace(Arc::new(ClientState {
            capabilities: capabilities(connected, self.state.snapshot.is_some()),
            snapshot: self.state.snapshot.clone(),
            connection: self.connection.clone(),
            error: self.state.error.clone(),
        }));
    }

    async fn run<T: Transport>(mut self, mut transport: T, ready: Reply) {
        let mut ready = Some(ready);
        loop {
            let result = self.connection(&mut transport, &mut ready).await;
            transport.close().await;
            self.abandon_request();
            let retry = !self.closed
                && ready.is_none()
                && transport.reconnects()
                && matches!(self.options.reconnect, ReconnectPolicy::Retry)
                && !is_terminal(&result);
            if let Some(ready) = ready.take() {
                let _ = ready.send(result.clone());
            }
            if !retry {
                self.connection =
                    ConnectionState::Failed(result.err().unwrap_or(ClientError::Disconnected));
                self.publish();
                return;
            }
            self.connection = ConnectionState::Connecting;
            self.publish();
            if !self.reconnect(&mut transport).await {
                break;
            }
        }
        self.connection = ConnectionState::Failed(ClientError::Disconnected);
        self.publish();
    }

    async fn connection<T: Transport>(
        &mut self,
        transport: &mut T,
        ready: &mut Option<Reply>,
    ) -> Result<(), ClientError> {
        self.state.initialized = false;
        transport
            .send(ClientCommand::Initialize {
                protocol_version: LIVE_PROTOCOL_VERSION,
                scope: self.options.scope,
            })
            .await
            .map_err(|_| ClientError::Disconnected)?;
        let idle = platform::sleep(IDLE_TIMEOUT).fuse();
        futures_util::pin_mut!(idle);
        loop {
            let input = {
                let command = async {
                    if self.state.pending.is_none() {
                        self.commands.recv().await.ok()
                    } else {
                        futures_util::future::pending().await
                    }
                }
                .fuse();
                let event = transport.recv().fuse();
                futures_util::pin_mut!(command, event);
                futures_util::select! {
                    command = command => ConnectionInput::Command(command),
                    event = event => ConnectionInput::Event(event),
                    () = idle => ConnectionInput::Idle,
                }
            };
            match input {
                ConnectionInput::Command(None) => {
                    self.closed = true;
                    return Ok(());
                }
                ConnectionInput::Command(Some(Command::Close(reply))) => {
                    self.closed = true;
                    let _ = reply.send(Ok(()));
                    return Ok(());
                }
                ConnectionInput::Command(Some(Command::Request(request, reply))) => {
                    self.request(transport, request, reply).await?;
                }
                ConnectionInput::Event(event) => {
                    self.event(event?)?;
                    if self.state.initialized
                        && let Some(ready) = ready.take()
                    {
                        let _ = ready.send(Ok(()));
                    }
                }
                ConnectionInput::Idle => return Err(ClientError::Disconnected),
            }
            idle.set(platform::sleep(IDLE_TIMEOUT).fuse());
        }
    }

    fn event(&mut self, event: ServerEvent) -> Result<(), ClientError> {
        match event {
            Event::Initialize {
                protocol_version, ..
            } => {
                if protocol_version != LIVE_PROTOCOL_VERSION {
                    return Err(ClientError::Remote(RemoteError::new(
                        RemoteErrorCode::UnsupportedVersion,
                        "unsupported live protocol version",
                    )));
                }
                if std::mem::replace(&mut self.state.initialized, true) {
                    return Err(ClientError::Protocol("repeated initialization".to_owned()));
                }
                self.state.error = None;
                self.publish();
                Ok(())
            }
            Event::Error(error) => Err(ClientError::Remote(error)),
            _ if !self.state.initialized => {
                Err(ClientError::Protocol("expected initialization".to_owned()))
            }
            Event::Document(snapshot) => {
                self.state.snapshot = Some(snapshot);
                self.connection = ConnectionState::Connected;
                self.publish();
                Ok(())
            }
            Event::RequestResult(result) => {
                if let Some(pending) = self.state.pending.take() {
                    let _ = pending.reply.send(result.map_err(ClientError::Remote));
                }
                Ok(())
            }
            Event::Health { error } => {
                if self.state.error != error {
                    self.state.error = error;
                    self.publish();
                }
                Ok(())
            }
        }
    }

    async fn request<T: Transport>(
        &mut self,
        transport: &mut T,
        request: ClientCommand,
        reply: Reply,
    ) -> Result<(), ClientError> {
        if !self.state.initialized {
            let _ = reply.send(Err(ClientError::Disconnected));
            return Ok(());
        }
        let outcome_unknown_on_disconnect = matches!(
            request,
            ClientCommand::Apply(_) | ClientCommand::Submit(_) | ClientCommand::Cancel
        );
        if transport.send(request).await.is_err() {
            let _ = reply.send(Err(ClientError::Disconnected));
            return Err(ClientError::Disconnected);
        }
        self.state.pending = Some(Pending {
            reply,
            outcome_unknown_on_disconnect,
        });
        Ok(())
    }

    fn abandon_request(&mut self) {
        if let Some(pending) = self.state.pending.take() {
            let _ = pending
                .reply
                .send(Err(if pending.outcome_unknown_on_disconnect {
                    ClientError::OutcomeUnknown
                } else {
                    ClientError::Disconnected
                }));
        }
    }

    async fn reconnect<T: Transport>(&mut self, transport: &mut T) -> bool {
        let mut delay = 250;
        loop {
            let wait = delay;
            let connecting = async {
                platform::sleep(platform::reconnect_delay(wait)).await;
                transport.reconnect().await
            }
            .fuse();
            let command = self.commands.recv().fuse();
            futures_util::pin_mut!(connecting, command);
            futures_util::select! {
                next = connecting => match next {
                    Ok(()) => return true,
                    Err(_) => delay = (delay * 2).min(5000),
                },
                command = command => match command {
                    Ok(Command::Request(_, reply)) => { let _ = reply.send(Err(ClientError::Disconnected)); }
                    Ok(Command::Close(reply)) => { self.closed = true; let _ = reply.send(Ok(())); return false; }
                    Err(_) => { self.closed = true; return false; }
                },
            }
        }
    }
}

fn is_terminal(result: &Result<(), ClientError>) -> bool {
    matches!(
        result,
        Err(ClientError::Protocol(_)
            | ClientError::Remote(RemoteError {
                code: RemoteErrorCode::UnsupportedVersion | RemoteErrorCode::Protocol,
                ..
            }))
    )
}