lsdocs-rpc 0.1.0

MessagePack RPC envelope, framing, codec, peer and transports.
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! The symmetric RPC endpoint.

use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;

use bytes::Bytes;
use futures_util::{SinkExt, StreamExt};
use lsdocs_protocol::{CancelParams, Notification, Request, msgpack, notification::Cancel};
use serde::{Serialize, de::DeserializeOwned};
use tokio::sync::{Notify, Semaphore, mpsc, oneshot};
use tokio::task::{AbortHandle, JoinSet};
use tokio_util::codec::{FramedRead, FramedWrite, LengthDelimitedCodec};

use crate::message::{self, CodecError, Message};
use crate::transport::{self, BoxWriter, Transport};
use crate::{Error, ErrorCode, RpcError};

type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
type RequestHandler =
    Arc<dyn Fn(Bytes, Peer) -> BoxFuture<Result<Vec<u8>, RpcError>> + Send + Sync>;
type NotificationHandler = Arc<dyn Fn(Bytes, &Peer) + Send + Sync>;

/// Settings for a connection.
#[derive(Debug, Clone)]
pub struct Options {
    /// Largest frame accepted or sent, in bytes.
    pub max_frame_len: usize,
    /// How long an outgoing request waits for its response before failing
    /// with [`Error::Timeout`] and being cancelled. `None` waits forever.
    pub request_timeout: Option<Duration>,
    /// Most incoming requests handled at once; further ones wait. `None` is
    /// unlimited. Notifications are not limited.
    pub max_concurrency: Option<usize>,
    /// Outgoing frames queued before senders wait.
    pub send_queue: usize,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            max_frame_len: transport::DEFAULT_MAX_FRAME_LEN,
            request_timeout: None,
            max_concurrency: None,
            send_queue: 64,
        }
    }
}

/// Handlers for incoming requests and notifications, keyed by method.
///
/// A request with no handler is answered with
/// [`ErrorCode::METHOD_NOT_FOUND`]; a notification with no handler is
/// ignored. `$/cancel` is handled by the connection itself.
#[derive(Clone, Default)]
pub struct Router {
    requests: HashMap<String, RequestHandler>,
    notifications: HashMap<String, NotificationHandler>,
}

impl std::fmt::Debug for Router {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Router")
            .field("requests", &self.requests.keys().collect::<Vec<_>>())
            .field(
                "notifications",
                &self.notifications.keys().collect::<Vec<_>>(),
            )
            .finish()
    }
}

impl Router {
    /// An empty router.
    pub fn new() -> Self {
        Self::default()
    }

    /// Handles request `R`. The handler gets the decoded params and a
    /// [`Peer`] for calling back the other side. Params that fail to decode
    /// are answered with [`ErrorCode::INVALID_PARAMS`] without calling it.
    pub fn request<R, F, Fut>(mut self, handler: F) -> Self
    where
        R: Request,
        R::Params: Send + 'static,
        F: Fn(R::Params, Peer) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<R::Result, RpcError>> + Send + 'static,
    {
        let handler: RequestHandler = Arc::new(
            move |raw: Bytes, peer: Peer| match msgpack::decode::<R::Params>(&raw) {
                Ok(params) => {
                    let fut = handler(params, peer);
                    Box::pin(async move {
                        let result = fut.await?;
                        msgpack::encode(&result).map_err(|e| {
                            RpcError::internal(format!("failed to encode result: {e}"))
                        })
                    }) as BoxFuture<_>
                }
                Err(e) => Box::pin(std::future::ready(Err(RpcError::new(
                    ErrorCode::INVALID_PARAMS,
                    format!("invalid params for `{}`: {e}", R::METHOD),
                )))),
            },
        );
        self.requests.insert(R::METHOD.to_owned(), handler);
        self
    }

    /// Handles notification `N`. Notification handlers run one at a time, in
    /// arrival order, on the connection's task, so they should be quick;
    /// spawn a task for slow work. Params that fail to decode are logged and
    /// dropped.
    pub fn notification<N, F>(mut self, handler: F) -> Self
    where
        N: Notification,
        F: Fn(N::Params, &Peer) + Send + Sync + 'static,
    {
        let handler: NotificationHandler =
            Arc::new(
                move |raw: Bytes, peer: &Peer| match msgpack::decode::<N::Params>(&raw) {
                    Ok(params) => handler(params, peer),
                    Err(e) => tracing::warn!(
                        method = N::METHOD,
                        "dropping notification with invalid params: {e}"
                    ),
                },
            );
        self.notifications.insert(N::METHOD.to_owned(), handler);
        self
    }
}

/// One end of a connection. Cheap to clone; all clones share the
/// connection.
#[derive(Clone)]
pub struct Peer {
    inner: Arc<Inner>,
}

struct Inner {
    /// Queue to the writer task; `None` once the connection is shutting down.
    tx: Mutex<Option<mpsc::Sender<Bytes>>>,
    /// Outgoing requests awaiting a response.
    pending: Mutex<HashMap<u32, oneshot::Sender<Result<Bytes, RpcError>>>>,
    next_id: AtomicU32,
    closing: AtomicBool,
    close: Notify,
    options: Options,
}

impl std::fmt::Debug for Peer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Peer")
            .field("closing", &self.inner.closing.load(Ordering::Relaxed))
            .finish_non_exhaustive()
    }
}

/// Creates a connection over `transport`. Returns the [`Peer`] for sending
/// and the [`Connection`] that must be run (usually spawned) for anything to
/// happen.
pub fn connect(transport: Transport, router: Router, options: Options) -> (Peer, Connection) {
    let (tx, rx) = mpsc::channel(options.send_queue.max(1));
    let peer = Peer {
        inner: Arc::new(Inner {
            tx: Mutex::new(Some(tx)),
            pending: Mutex::new(HashMap::new()),
            next_id: AtomicU32::new(1),
            closing: AtomicBool::new(false),
            close: Notify::new(),
            options,
        }),
    };
    let connection = Connection {
        peer: peer.clone(),
        transport,
        router,
        rx,
    };
    (peer, connection)
}

impl Peer {
    /// Sends request `R` and waits for its result.
    pub async fn request<R: Request>(&self, params: &R::Params) -> Result<R::Result, Error> {
        self.call(R::METHOD, params).await
    }

    /// Sends a request by method name and waits for its result.
    ///
    /// If the returned future is dropped before the response arrives, or
    /// [`Options::request_timeout`] passes, `$/cancel` is sent for it.
    pub async fn call<P, T>(&self, method: &str, params: &P) -> Result<T, Error>
    where
        P: Serialize + ?Sized,
        T: DeserializeOwned,
    {
        let id = self.inner.next_id.fetch_add(1, Ordering::Relaxed);
        let frame = message::encode_request(id, method, params)?;
        let (tx, rx) = oneshot::channel();
        self.inner
            .pending
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .insert(id, tx);
        let mut guard = CancelOnDrop {
            peer: self,
            id,
            armed: true,
        };
        if self.inner.closing.load(Ordering::SeqCst) {
            return Err(Error::Closed);
        }
        if let Err(e) = self.send(frame).await {
            // Never sent, so there is nothing to cancel.
            guard.armed = false;
            return Err(e);
        }
        let response = match self.inner.options.request_timeout {
            Some(limit) => match tokio::time::timeout(limit, rx).await {
                Ok(response) => response,
                Err(_) => return Err(Error::Timeout),
            },
            None => rx.await,
        };
        guard.armed = false;
        let bytes = response
            .map_err(|_| Error::Closed)?
            .map_err(Error::Remote)?;
        msgpack::decode(&bytes)
            .map_err(|e| Error::Codec(CodecError(format!("invalid result for `{method}`: {e}"))))
    }

    /// Sends notification `N`.
    pub async fn notify<N: Notification>(&self, params: &N::Params) -> Result<(), Error> {
        self.notify_raw(N::METHOD, params).await
    }

    /// Sends a notification by method name.
    pub async fn notify_raw<P: Serialize + ?Sized>(
        &self,
        method: &str,
        params: &P,
    ) -> Result<(), Error> {
        self.send(message::encode_notification(method, params)?)
            .await
    }

    /// Asks the connection to stop reading and shut down gracefully. In-flight
    /// incoming requests still finish and are answered; outgoing requests
    /// still waiting fail with [`Error::Closed`].
    pub fn close(&self) {
        self.inner.closing.store(true, Ordering::SeqCst);
        self.inner.close.notify_one();
    }

    /// Whether the connection has started shutting down.
    pub fn is_closed(&self) -> bool {
        self.inner.closing.load(Ordering::SeqCst)
    }

    async fn send(&self, frame: Bytes) -> Result<(), Error> {
        let limit = self.inner.options.max_frame_len;
        if frame.len() > limit {
            return Err(Error::Codec(CodecError(format!(
                "message of {} bytes exceeds the {limit} byte frame limit",
                frame.len()
            ))));
        }
        let tx = self
            .inner
            .tx
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .clone()
            .ok_or(Error::Closed)?;
        tx.send(frame).await.map_err(|_| Error::Closed)
    }

    fn try_send(&self, frame: Bytes) {
        if let Some(tx) = self
            .inner
            .tx
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .as_ref()
        {
            let _ = tx.try_send(frame);
        }
    }
}

/// Cancels an outgoing request that is abandoned before its response.
struct CancelOnDrop<'a> {
    peer: &'a Peer,
    id: u32,
    armed: bool,
}

impl Drop for CancelOnDrop<'_> {
    fn drop(&mut self) {
        let was_pending = self
            .peer
            .inner
            .pending
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .remove(&self.id)
            .is_some();
        if self.armed
            && was_pending
            && let Ok(frame) =
                message::encode_notification(Cancel::METHOD, &CancelParams { id: self.id })
        {
            self.peer.try_send(frame);
        }
    }
}

/// Drives a connection: reads and dispatches incoming messages and writes
/// outgoing ones. Created by [`connect`].
pub struct Connection {
    peer: Peer,
    transport: Transport,
    router: Router,
    rx: mpsc::Receiver<Bytes>,
}

impl std::fmt::Debug for Connection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Connection").finish_non_exhaustive()
    }
}

/// Bookkeeping for incoming requests being handled.
struct Running {
    tasks: JoinSet<()>,
    by_request: HashMap<u32, AbortHandle>,
    by_task: HashMap<tokio::task::Id, u32>,
}

impl Connection {
    /// Runs until the other side closes the stream, the transport fails, or
    /// [`Peer::close`] is called. Then waits for in-flight incoming requests
    /// to finish and flushes everything queued before returning.
    pub async fn run(self) -> Result<(), Error> {
        let Connection {
            peer,
            transport,
            router,
            rx,
        } = self;
        let options = peer.inner.options.clone();
        let mut reader = FramedRead::new(transport.reader, transport::codec(options.max_frame_len));
        let writer = FramedWrite::new(transport.writer, transport::codec(options.max_frame_len));
        let write_task = tokio::spawn(write_loop(writer, rx, peer.clone()));
        let limit = options
            .max_concurrency
            .map(|n| Arc::new(Semaphore::new(n.max(1))));
        let mut running = Running {
            tasks: JoinSet::new(),
            by_request: HashMap::new(),
            by_task: HashMap::new(),
        };

        let read_result = loop {
            tokio::select! {
                frame = reader.next() => match frame {
                    None => break Ok(()),
                    Some(Err(e)) => break Err(e),
                    Some(Ok(frame)) => {
                        let frame = frame.freeze();
                        crate::trace::frame("recv", &frame);
                        dispatch(frame, &peer, &router, &limit, &mut running).await;
                    }
                },
                Some(joined) = running.tasks.join_next_with_id(), if !running.tasks.is_empty() => {
                    let task = match joined {
                        Ok((task, ())) => task,
                        Err(e) => e.id(),
                    };
                    if let Some(id) = running.by_task.remove(&task) {
                        running.by_request.remove(&id);
                    }
                }
                () = peer.inner.close.notified() => break Ok(()),
            }
        };

        // Stop new outgoing requests, then fail the ones still waiting.
        peer.inner.closing.store(true, Ordering::SeqCst);
        peer.inner
            .pending
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .clear();
        // Let in-flight handlers finish and queue their responses.
        while running.tasks.join_next().await.is_some() {}
        // Closing the queue lets the writer drain it and shut down.
        drop(
            peer.inner
                .tx
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .take(),
        );
        let write_result = write_task
            .await
            .map_err(|e| Error::Io(std::io::Error::other(e)))?;

        read_result.map_err(Error::Io)?;
        write_result.map_err(Error::Io)
    }
}

async fn dispatch(
    frame: Bytes,
    peer: &Peer,
    router: &Router,
    limit: &Option<Arc<Semaphore>>,
    running: &mut Running,
) {
    let message = match message::decode(frame.clone()) {
        Ok(message) => message,
        Err(e) => {
            tracing::warn!("dropping malformed message: {e}");
            if let Some(id) = message::peek_request_id(&frame) {
                let error = RpcError::new(ErrorCode::INVALID_REQUEST, e.to_string());
                let _ = peer.send(message::encode_error(id, &error)).await;
            }
            return;
        }
    };
    match message {
        Message::Request { id, method, params } => {
            let Some(handler) = router.requests.get(&method) else {
                let error = RpcError::new(
                    ErrorCode::METHOD_NOT_FOUND,
                    format!("no handler for `{method}`"),
                );
                let _ = peer.send(message::encode_error(id, &error)).await;
                return;
            };
            let fut = handler(params, peer.clone());
            let responder = peer.clone();
            let limit = limit.clone();
            let handle = running.tasks.spawn(async move {
                // The semaphore is never closed; if it were, the limit
                // would just stop applying.
                let _permit = match limit {
                    Some(limit) => limit.acquire_owned().await.ok(),
                    None => None,
                };
                let frame = match fut.await {
                    Ok(result) => message::encode_response_raw(id, &result),
                    Err(error) => message::encode_error(id, &error),
                };
                if let Err(Error::Codec(e)) = responder.send(frame).await {
                    // The result was too large to send; say so instead.
                    let error = RpcError::internal(e.to_string());
                    let _ = responder.send(message::encode_error(id, &error)).await;
                }
            });
            running.by_task.insert(handle.id(), id);
            running.by_request.insert(id, handle);
        }
        Message::Notification { method, params } if method == Cancel::METHOD => {
            let Ok(CancelParams { id }) = msgpack::decode(&params) else {
                tracing::warn!("dropping `$/cancel` with invalid params");
                return;
            };
            if let Some(handle) = running.by_request.remove(&id) {
                handle.abort();
                let error = RpcError::new(ErrorCode::REQUEST_CANCELLED, "request cancelled");
                let _ = peer.send(message::encode_error(id, &error)).await;
            }
        }
        Message::Notification { method, params } => match router.notifications.get(&method) {
            Some(handler) => handler(params, peer),
            None => tracing::debug!(%method, "ignoring notification with no handler"),
        },
        Message::Response { id, result } => {
            let waiter = peer
                .inner
                .pending
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .remove(&id);
            match waiter {
                Some(waiter) => {
                    let _ = waiter.send(result);
                }
                None => tracing::debug!(id, "ignoring response to unknown or abandoned request"),
            }
        }
    }
}

/// Writes queued frames until the queue closes. If writing fails, the whole
/// connection is closed so nothing waits on a response that cannot come.
async fn write_loop(
    mut sink: FramedWrite<BoxWriter, LengthDelimitedCodec>,
    mut rx: mpsc::Receiver<Bytes>,
    peer: Peer,
) -> std::io::Result<()> {
    let result = async {
        while let Some(frame) = rx.recv().await {
            crate::trace::frame("send", &frame);
            sink.send(frame).await?;
        }
        SinkExt::<Bytes>::close(&mut sink).await
    }
    .await;
    if result.is_err() {
        peer.close();
    }
    result
}