rmux-server 0.1.0

Tokio daemon and request dispatcher for the RMUX terminal multiplexer.
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
use std::collections::HashMap;
use std::time::Instant;

use rmux_core::events::{
    OutputCursor, OutputCursorItem, OutputGap, PaneOutputSubscriptionKey, SubscriptionLimitError,
    SubscriptionLimits, SubscriptionRegistry,
};
use rmux_proto::{
    ErrorResponse, PaneOutputCursor, PaneOutputCursorRequest, PaneOutputCursorResponse,
    PaneOutputEvent, PaneOutputLagNotice, PaneOutputLagResponse, PaneOutputSubscriptionId,
    PaneOutputSubscriptionStart, PaneRecentOutput, Response, RmuxError, SubscribePaneOutputRequest,
    SubscribePaneOutputResponse, UnsubscribePaneOutputRequest, UnsubscribePaneOutputResponse,
    DEFAULT_MAX_FRAME_LENGTH,
};

use crate::pane_io::PaneOutputReceiver;

use super::RequestHandler;

// Keep lag diagnostics well below the detached RPC frame cap after bincode
// overhead and the rest of the response envelope are added.
const MAX_LAG_RECENT_BYTES: usize = DEFAULT_MAX_FRAME_LENGTH / 16;

pub(crate) struct OutputSubscriptionState {
    registry: SubscriptionRegistry,
    receivers: HashMap<PaneOutputSubscriptionId, PaneOutputReceiver>,
}

impl std::fmt::Debug for OutputSubscriptionState {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("OutputSubscriptionState")
            .field("registry", &self.registry)
            .field("receiver_count", &self.receivers.len())
            .finish()
    }
}

impl OutputSubscriptionState {
    pub(crate) fn new(limits: SubscriptionLimits) -> Self {
        Self {
            registry: SubscriptionRegistry::new(limits),
            receivers: HashMap::new(),
        }
    }

    fn limits(&self) -> SubscriptionLimits {
        self.registry.limits()
    }

    fn cleanup_stale(&mut self, now: Instant) {
        for record in self.registry.cleanup_stale(now) {
            self.receivers.remove(&record.id());
        }
    }

    fn remove_connection(&mut self, connection_id: u64) {
        for record in self.registry.remove_connection(connection_id) {
            self.receivers.remove(&record.id());
        }
    }

    fn remove_pane(&mut self, pane: &PaneOutputSubscriptionKey) {
        for record in self.registry.remove_pane(pane) {
            self.receivers.remove(&record.id());
        }
    }
}

impl RequestHandler {
    pub(in crate::handler) async fn handle_subscribe_pane_output(
        &self,
        connection_id: u64,
        request: SubscribePaneOutputRequest,
    ) -> Response {
        let now = Instant::now();
        let (subscription_id, pane_id, cursor) = {
            let state = self.state.lock().await;
            let pane_key = match state.pane_output_subscription_key_for_target(&request.target) {
                Ok(key) => key,
                Err(error) => return Response::Error(ErrorResponse { error }),
            };
            let output = match state.pane_output_for_target(
                request.target.session_name(),
                request.target.window_index(),
                request.target.pane_index(),
            ) {
                Ok(output) => output,
                Err(error) => return Response::Error(ErrorResponse { error }),
            };
            let receiver = match request.start {
                PaneOutputSubscriptionStart::Now => output.subscribe(),
                PaneOutputSubscriptionStart::Oldest => output.subscribe_from_oldest(),
            };

            let mut subscriptions = self
                .subscriptions
                .lock()
                .expect("subscription registry mutex must not be poisoned");
            subscriptions.cleanup_stale(now);
            let record =
                match subscriptions
                    .registry
                    .subscribe(connection_id, pane_key.clone(), now)
                {
                    Ok(record) => record,
                    Err(error) => {
                        return Response::Error(ErrorResponse {
                            error: subscription_limit_error(error),
                        });
                    }
                };
            let cursor = cursor_dto(receiver.cursor());
            let subscription_id = record.id();
            subscriptions.receivers.insert(record.id(), receiver);
            (subscription_id, pane_key.pane_id(), cursor)
        };

        Response::SubscribePaneOutput(SubscribePaneOutputResponse {
            subscription_id,
            target: request.target,
            pane_id,
            cursor,
        })
    }

    pub(in crate::handler) async fn handle_unsubscribe_pane_output(
        &self,
        connection_id: u64,
        request: UnsubscribePaneOutputRequest,
    ) -> Response {
        let now = Instant::now();
        let mut subscriptions = self
            .subscriptions
            .lock()
            .expect("subscription registry mutex must not be poisoned");
        subscriptions.cleanup_stale(now);

        let Some(record) = subscriptions.registry.get(request.subscription_id).cloned() else {
            return Response::UnsubscribePaneOutput(UnsubscribePaneOutputResponse {
                subscription_id: request.subscription_id,
                removed: false,
            });
        };
        if record.connection_id() != connection_id {
            return Response::Error(ErrorResponse {
                error: RmuxError::Server("subscription is not owned by this connection".to_owned()),
            });
        }

        let removed = subscriptions
            .registry
            .unsubscribe(request.subscription_id)
            .is_some();
        subscriptions.receivers.remove(&request.subscription_id);
        Response::UnsubscribePaneOutput(UnsubscribePaneOutputResponse {
            subscription_id: request.subscription_id,
            removed,
        })
    }

    pub(in crate::handler) async fn handle_pane_output_cursor(
        &self,
        connection_id: u64,
        request: PaneOutputCursorRequest,
    ) -> Response {
        let now = Instant::now();
        let mut subscriptions = self
            .subscriptions
            .lock()
            .expect("subscription registry mutex must not be poisoned");
        subscriptions.cleanup_stale(now);
        let limit =
            match cursor_event_limit(request.max_events, subscriptions.limits().batch_events()) {
                Ok(limit) => limit,
                Err(error) => return Response::Error(ErrorResponse { error }),
            };

        let Some(record) = subscriptions.registry.get(request.subscription_id).cloned() else {
            return Response::Error(ErrorResponse {
                error: RmuxError::Server("subscription not found".to_owned()),
            });
        };
        if record.connection_id() != connection_id {
            return Response::Error(ErrorResponse {
                error: RmuxError::Server("subscription is not owned by this connection".to_owned()),
            });
        }
        let _ = subscriptions.registry.touch(request.subscription_id, now);

        let Some(receiver) = subscriptions.receivers.get_mut(&request.subscription_id) else {
            let _ = subscriptions.registry.unsubscribe(request.subscription_id);
            return Response::Error(ErrorResponse {
                error: RmuxError::Server("subscription receiver not found".to_owned()),
            });
        };

        let items = receiver.try_recv_batch(limit);
        let mut events = Vec::new();
        for item in items {
            match item {
                OutputCursorItem::Event(event) => {
                    events.push(PaneOutputEvent {
                        sequence: event.sequence(),
                        bytes: event.into_bytes(),
                    });
                }
                OutputCursorItem::Gap(gap) => {
                    let cursor = cursor_dto(receiver.cursor());
                    return Response::PaneOutputLag(PaneOutputLagResponse {
                        subscription_id: request.subscription_id,
                        cursor,
                        lag: lag_dto(&gap),
                    });
                }
            }
        }

        Response::PaneOutputCursor(PaneOutputCursorResponse {
            subscription_id: request.subscription_id,
            cursor: cursor_dto(receiver.cursor()),
            limited: events.len() == limit,
            events,
        })
    }

    pub(crate) async fn cleanup_connection_subscriptions(&self, connection_id: u64) {
        let mut subscriptions = self
            .subscriptions
            .lock()
            .expect("subscription registry mutex must not be poisoned");
        subscriptions.remove_connection(connection_id);
    }

    pub(crate) async fn cleanup_pane_output_subscriptions(
        &self,
        panes: &[PaneOutputSubscriptionKey],
    ) {
        let mut subscriptions = self
            .subscriptions
            .lock()
            .expect("subscription registry mutex must not be poisoned");
        for pane in panes {
            subscriptions.remove_pane(pane);
        }
    }
}

fn cursor_event_limit(requested: Option<u16>, default: usize) -> Result<usize, RmuxError> {
    match requested {
        Some(0) => Err(RmuxError::Server(
            "pane output cursor max_events must be greater than zero".to_owned(),
        )),
        Some(value) => Ok(usize::from(value).min(default)),
        None => Ok(default),
    }
}

fn cursor_dto(cursor: &OutputCursor) -> PaneOutputCursor {
    PaneOutputCursor {
        next_sequence: cursor.next_sequence(),
        missed_events: cursor.missed_events(),
    }
}

fn lag_dto(gap: &OutputGap) -> PaneOutputLagNotice {
    let recent = gap.recent_snapshot();
    let mut recent_bytes = recent.bytes().to_vec();
    let truncated = recent_bytes.len() > MAX_LAG_RECENT_BYTES;
    if truncated {
        recent_bytes = recent_bytes[recent_bytes.len() - MAX_LAG_RECENT_BYTES..].to_vec();
    }
    PaneOutputLagNotice {
        expected_sequence: gap.expected_sequence(),
        resume_sequence: gap.resume_sequence(),
        missed_events: gap.missed_events(),
        newest_sequence: gap.newest_sequence(),
        recent: PaneRecentOutput {
            bytes: recent_bytes,
            oldest_sequence: if truncated {
                None
            } else {
                recent.oldest_sequence()
            },
            newest_sequence: recent.newest_sequence(),
        },
    }
}

fn subscription_limit_error(error: SubscriptionLimitError) -> RmuxError {
    match error {
        SubscriptionLimitError::PerConnection { limit } => RmuxError::Server(format!(
            "pane output subscription limit exceeded for connection (limit {limit})"
        )),
        SubscriptionLimitError::PerPane { limit } => RmuxError::Server(format!(
            "pane output subscription limit exceeded for pane (limit {limit})"
        )),
    }
}

#[cfg(test)]
mod tests {
    use std::time::Instant;

    use rmux_core::events::{
        OutputCursor, OutputCursorItem, OutputRing, PaneOutputSubscriptionKey,
    };
    use rmux_proto::{PaneId, PaneOutputCursorRequest, Response, SessionName};

    use crate::pane_io::pane_output_channel_with_limits;

    use super::{lag_dto, RequestHandler, MAX_LAG_RECENT_BYTES};

    #[test]
    fn lag_dto_carries_recent_output_without_replaying_missed_bytes() {
        let mut ring = OutputRing::new(1, 8);
        let mut cursor = OutputCursor::new(0);
        ring.push(b"abcdef".to_vec());
        ring.push(b"ghijk".to_vec());

        let Some(OutputCursorItem::Gap(gap)) = ring.poll_cursor(&mut cursor) else {
            panic!("cursor should lag after output ring rotation");
        };
        let notice = lag_dto(&gap);

        assert_eq!(notice.expected_sequence, 0);
        assert_eq!(notice.resume_sequence, 1);
        assert_eq!(notice.missed_events, 1);
        assert_eq!(notice.newest_sequence, 1);
        assert_eq!(notice.recent.bytes, b"defghijk");
        assert_eq!(notice.recent.oldest_sequence, Some(0));
        assert_eq!(notice.recent.newest_sequence, Some(1));

        let Some(OutputCursorItem::Event(event)) = ring.poll_cursor(&mut cursor) else {
            panic!("cursor should resume at the oldest retained output event");
        };
        assert_eq!(event.sequence(), notice.resume_sequence);
        assert_eq!(event.bytes(), b"ghijk");
        assert_ne!(event.bytes(), notice.recent.bytes.as_slice());
    }

    #[test]
    fn lag_dto_trims_recent_hint_under_detached_frame_limit() {
        let mut ring = OutputRing::new(1, MAX_LAG_RECENT_BYTES + 16);
        let mut cursor = OutputCursor::new(0);
        ring.push(vec![b'a'; 16]);
        ring.push(vec![b'b'; MAX_LAG_RECENT_BYTES + 16]);

        let Some(OutputCursorItem::Gap(gap)) = ring.poll_cursor(&mut cursor) else {
            panic!("cursor should lag after output ring rotation");
        };
        let notice = lag_dto(&gap);

        assert_eq!(notice.recent.bytes.len(), MAX_LAG_RECENT_BYTES);
        assert!(notice.recent.bytes.iter().all(|byte| *byte == b'b'));
        assert_eq!(notice.recent.oldest_sequence, None);
        assert_eq!(notice.recent.newest_sequence, Some(1));
        assert_eq!(notice.resume_sequence, 1);
    }

    #[tokio::test]
    async fn cursor_handler_trims_lag_recent_hint_for_subscription_response() {
        let handler = RequestHandler::new();
        let connection_id = 77;
        let sender = pane_output_channel_with_limits(1, MAX_LAG_RECENT_BYTES + 32);
        let receiver = sender.subscribe();
        sender.send(vec![b'a'; 32]);
        sender.send(vec![b'b'; MAX_LAG_RECENT_BYTES + 32]);

        let subscription_id = {
            let mut subscriptions = handler
                .subscriptions
                .lock()
                .expect("subscription registry mutex must not be poisoned");
            let record = subscriptions
                .registry
                .subscribe(
                    connection_id,
                    PaneOutputSubscriptionKey::new(
                        SessionName::new("runtime").expect("valid session name"),
                        PaneId::new(1),
                    ),
                    Instant::now(),
                )
                .expect("subscription is within limits");
            let subscription_id = record.id();
            subscriptions.receivers.insert(subscription_id, receiver);
            subscription_id
        };

        let response = handler
            .handle_pane_output_cursor(
                connection_id,
                PaneOutputCursorRequest {
                    subscription_id,
                    max_events: Some(8),
                },
            )
            .await;
        let Response::PaneOutputLag(lag) = response else {
            panic!("lagged subscription should produce a lag response");
        };

        assert_eq!(lag.subscription_id, subscription_id);
        assert_eq!(lag.cursor.next_sequence, 1);
        assert_eq!(lag.cursor.missed_events, 1);
        assert_eq!(lag.lag.expected_sequence, 0);
        assert_eq!(lag.lag.resume_sequence, 1);
        assert_eq!(lag.lag.missed_events, 1);
        assert_eq!(lag.lag.recent.bytes.len(), MAX_LAG_RECENT_BYTES);
        assert!(lag.lag.recent.bytes.iter().all(|byte| *byte == b'b'));
        assert_eq!(lag.lag.recent.oldest_sequence, None);
        assert_eq!(lag.lag.recent.newest_sequence, Some(1));
    }
}