tokio-process-tools 0.9.0

Correctness-focused async subprocess orchestration for Tokio: bounded output, multi-consumer streams, output detection, guaranteed cleanup and graceful termination.
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
use crate::NumBytes;
use crate::output_stream::policy::{
    BestEffortDelivery, Delivery, DeliveryGuarantee, NoReplay, ReliableDelivery, Replay,
    ReplayEnabled, ReplayRetention,
};

/// Default chunk size read from the source stream. 16 kilobytes.
pub const DEFAULT_READ_CHUNK_SIZE: NumBytes = NumBytes(16 * 1024); // 16 kb

/// Default maximum buffered chunks for stdout and stderr streams. 128 slots.
pub const DEFAULT_MAX_BUFFERED_CHUNKS: usize = 128;

pub(crate) fn assert_max_buffered_chunks_non_zero(chunks: usize, parameter_name: &str) {
    assert!(chunks > 0, "{parameter_name} must be greater than zero");
}

/// Shared output stream configuration for all stream backends.
///
/// Backend selection controls whether a stream has one active owner or can fan out to multiple
/// consumers. This configuration controls delivery, replay, and buffering for whichever backend is
/// selected.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StreamConfig<D = BestEffortDelivery, R = NoReplay>
where
    D: Delivery,
    R: Replay,
{
    /// The size of an individual chunk read from the underlying process stream.
    ///
    /// Must be greater than zero. The default is [`crate::DEFAULT_READ_CHUNK_SIZE`].
    pub read_chunk_size: NumBytes,

    /// The number of chunks held by the underlying async channel.
    ///
    /// Must be greater than zero. The default is [`crate::DEFAULT_MAX_BUFFERED_CHUNKS`].
    /// With [`DeliveryGuarantee::ReliableForActiveSubscribers`], it is the maximum unread chunk
    /// lag an active subscriber can have before reading waits.
    pub max_buffered_chunks: usize,

    /// How slow active subscribers affect reading from the underlying stream.
    pub delivery: D,

    /// Whether and how replay history is retained for subscribers that attach after output arrives.
    pub replay: R,
}

impl StreamConfig<BestEffortDelivery, NoReplay> {
    /// Starts building an output stream configuration.
    #[must_use]
    pub fn builder() -> StreamConfigBuilder {
        StreamConfigBuilder
    }
}

/// Initial builder stage that requires selecting delivery behavior.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StreamConfigBuilder;

impl StreamConfigBuilder {
    /// Delivery that keeps reading while slow consumers lag behind.
    ///
    /// Bounded buffers may overflow, so active consumers can observe gaps or dropped output. This
    /// policy avoids applying backpressure for slow consumers, but it is not a blanket throughput
    /// guarantee; backend implementation and workload shape still matter.
    #[must_use]
    pub fn best_effort_delivery(self) -> StreamConfigReplayBuilder<BestEffortDelivery> {
        StreamConfigReplayBuilder {
            delivery: BestEffortDelivery,
        }
    }

    /// Delivery that waits for active subscribers when their buffers are full.
    ///
    /// This applies backpressure to process-output reading so active consumers see all chunks
    /// delivered inside the library. It does not retain output for consumers that attach later;
    /// that is controlled by replay settings.
    #[must_use]
    pub fn reliable_for_active_subscribers(self) -> StreamConfigReplayBuilder<ReliableDelivery> {
        StreamConfigReplayBuilder {
            delivery: ReliableDelivery,
        }
    }
}

/// Builder stage that requires selecting replay behavior.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StreamConfigReplayBuilder<D>
where
    D: Delivery,
{
    delivery: D,
}

impl<D> StreamConfigReplayBuilder<D>
where
    D: Delivery,
{
    /// Disables replay for future subscribers.
    ///
    /// Consumers that attach after output has already arrived start at live output.
    #[must_use]
    pub fn no_replay(self) -> StreamConfigReadChunkSizeBuilder<D, NoReplay> {
        StreamConfigReadChunkSizeBuilder {
            delivery: self.delivery,
            replay: NoReplay,
        }
    }

    /// Keeps the latest number of chunks for future subscribers.
    #[must_use]
    pub fn replay_last_chunks(
        self,
        chunks: usize,
    ) -> StreamConfigReadChunkSizeBuilder<D, ReplayEnabled> {
        let replay_retention = ReplayRetention::LastChunks(chunks);
        replay_retention.assert_non_zero("chunks");
        StreamConfigReadChunkSizeBuilder {
            delivery: self.delivery,
            replay: ReplayEnabled::new(replay_retention),
        }
    }

    /// Keeps whole chunks covering at least the latest number of bytes.
    #[must_use]
    pub fn replay_last_bytes(
        self,
        bytes: NumBytes,
    ) -> StreamConfigReadChunkSizeBuilder<D, ReplayEnabled> {
        let replay_retention = ReplayRetention::LastBytes(bytes);
        replay_retention.assert_non_zero("bytes");
        StreamConfigReadChunkSizeBuilder {
            delivery: self.delivery,
            replay: ReplayEnabled::new(replay_retention),
        }
    }

    /// Retains all output of the stream.
    ///
    /// This can potentially grow massively and could require a lot of memory.
    ///
    /// Make sure to call `seal_replay()` on the stream when all subscribers were created. This
    /// allows the system to free up memory for data already replayed to all subscribers.
    #[must_use]
    pub fn replay_all(self) -> StreamConfigReadChunkSizeBuilder<D, ReplayEnabled> {
        StreamConfigReadChunkSizeBuilder {
            delivery: self.delivery,
            replay: ReplayEnabled::new(ReplayRetention::All),
        }
    }
}

/// Builder stage that requires selecting the read chunk size.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StreamConfigReadChunkSizeBuilder<D, R>
where
    D: Delivery,
    R: Replay,
{
    delivery: D,
    replay: R,
}

impl<D, R> StreamConfigReadChunkSizeBuilder<D, R>
where
    D: Delivery,
    R: Replay,
{
    /// Selects the size of chunks read from the underlying process stream.
    ///
    /// # Panics
    ///
    /// Panics if `read_chunk_size` is zero bytes.
    #[must_use]
    pub fn read_chunk_size(
        self,
        read_chunk_size: NumBytes,
    ) -> StreamConfigMaxBufferedChunksBuilder<D, R> {
        read_chunk_size.assert_non_zero("read_chunk_size");
        StreamConfigMaxBufferedChunksBuilder {
            delivery: self.delivery,
            replay: self.replay,
            read_chunk_size,
        }
    }
}

/// Builder stage that requires selecting the maximum number of buffered chunks.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StreamConfigMaxBufferedChunksBuilder<D, R>
where
    D: Delivery,
    R: Replay,
{
    delivery: D,
    replay: R,
    read_chunk_size: NumBytes,
}

impl<D, R> StreamConfigMaxBufferedChunksBuilder<D, R>
where
    D: Delivery,
    R: Replay,
{
    /// Selects the number of chunks held by the underlying async channel.
    ///
    /// # Panics
    ///
    /// Panics if `max_buffered_chunks` is zero.
    #[must_use]
    pub fn max_buffered_chunks(self, max_buffered_chunks: usize) -> StreamConfigReadyBuilder<D, R> {
        assert_max_buffered_chunks_non_zero(max_buffered_chunks, "max_buffered_chunks");
        StreamConfigReadyBuilder {
            config: StreamConfig {
                read_chunk_size: self.read_chunk_size,
                max_buffered_chunks,
                delivery: self.delivery,
                replay: self.replay,
            },
        }
    }
}

/// Final builder stage for [`StreamConfig`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StreamConfigReadyBuilder<D, R>
where
    D: Delivery,
    R: Replay,
{
    config: StreamConfig<D, R>,
}

impl<D, R> StreamConfigReadyBuilder<D, R>
where
    D: Delivery,
    R: Replay,
{
    /// Builds the configured stream mode.
    #[must_use]
    pub fn build(self) -> StreamConfig<D, R> {
        self.config
    }
}

impl<D, R> StreamConfig<D, R>
where
    D: Delivery,
    R: Replay,
{
    /// Returns the runtime delivery guarantee represented by this configuration.
    #[must_use]
    pub fn delivery_guarantee(self) -> DeliveryGuarantee {
        self.delivery.guarantee()
    }

    /// Returns the replay retention represented by this configuration.
    #[must_use]
    pub fn replay_retention(self) -> Option<ReplayRetention> {
        self.replay.replay_retention()
    }

    /// Returns whether this configuration enables replay-specific APIs.
    #[must_use]
    pub fn replay_enabled(self) -> bool {
        self.replay.replay_enabled()
    }

    pub(crate) fn assert_valid(self, parameter_name: &str) {
        self.read_chunk_size
            .assert_non_zero(&format!("{parameter_name}.read_chunk_size"));
        assert_max_buffered_chunks_non_zero(
            self.max_buffered_chunks,
            &format!("{parameter_name}.max_buffered_chunks"),
        );
        if let Some(replay_retention) = self.replay_retention() {
            replay_retention.assert_non_zero(&format!("{parameter_name}.replay_retention"));
        }
    }
}

impl<D> StreamConfig<D, ReplayEnabled>
where
    D: Delivery,
{
    /// Returns this replay-enabled configuration with custom replay retention.
    #[must_use]
    pub fn with_replay_retention(mut self, replay_retention: ReplayRetention) -> Self {
        replay_retention.assert_non_zero("replay_retention");
        self.replay.replay_retention = replay_retention;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::output_stream::num_bytes::NumBytesExt;
    use crate::{DEFAULT_MAX_BUFFERED_CHUNKS, DEFAULT_READ_CHUNK_SIZE};
    use assertr::prelude::*;

    #[test]
    fn builder_creates_expected_delivery_and_replay_configs() {
        let config: StreamConfig<BestEffortDelivery, NoReplay> = StreamConfig::builder()
            .best_effort_delivery()
            .no_replay()
            .read_chunk_size(DEFAULT_READ_CHUNK_SIZE)
            .max_buffered_chunks(DEFAULT_MAX_BUFFERED_CHUNKS)
            .build();

        assert_that!(config.delivery_guarantee()).is_equal_to(DeliveryGuarantee::BestEffort);
        assert_that!(config.replay_enabled()).is_false();
        assert_that!(config.replay_retention()).is_none();
        assert_that!(config.read_chunk_size).is_equal_to(DEFAULT_READ_CHUNK_SIZE);
        assert_that!(config.max_buffered_chunks).is_equal_to(DEFAULT_MAX_BUFFERED_CHUNKS);

        let config: StreamConfig<ReliableDelivery, NoReplay> = StreamConfig::builder()
            .reliable_for_active_subscribers()
            .no_replay()
            .read_chunk_size(DEFAULT_READ_CHUNK_SIZE)
            .max_buffered_chunks(DEFAULT_MAX_BUFFERED_CHUNKS)
            .build();

        assert_that!(config.delivery_guarantee())
            .is_equal_to(DeliveryGuarantee::ReliableForActiveSubscribers);
        assert_that!(config.replay_enabled()).is_false();
        assert_that!(config.replay_retention()).is_none();
        assert_that!(config.read_chunk_size).is_equal_to(DEFAULT_READ_CHUNK_SIZE);
        assert_that!(config.max_buffered_chunks).is_equal_to(DEFAULT_MAX_BUFFERED_CHUNKS);

        let config: StreamConfig<BestEffortDelivery, ReplayEnabled> = StreamConfig::builder()
            .best_effort_delivery()
            .replay_last_chunks(2)
            .read_chunk_size(DEFAULT_READ_CHUNK_SIZE)
            .max_buffered_chunks(DEFAULT_MAX_BUFFERED_CHUNKS)
            .build();

        assert_that!(config.delivery_guarantee()).is_equal_to(DeliveryGuarantee::BestEffort);
        assert_that!(config.replay_enabled()).is_true();
        assert_that!(config.replay_retention()).is_equal_to(Some(ReplayRetention::LastChunks(2)));
        assert_that!(config.read_chunk_size).is_equal_to(DEFAULT_READ_CHUNK_SIZE);
        assert_that!(config.max_buffered_chunks).is_equal_to(DEFAULT_MAX_BUFFERED_CHUNKS);

        let config: StreamConfig<ReliableDelivery, ReplayEnabled> = StreamConfig::builder()
            .reliable_for_active_subscribers()
            .replay_last_bytes(16.bytes())
            .read_chunk_size(DEFAULT_READ_CHUNK_SIZE)
            .max_buffered_chunks(DEFAULT_MAX_BUFFERED_CHUNKS)
            .build();

        assert_that!(config.delivery_guarantee())
            .is_equal_to(DeliveryGuarantee::ReliableForActiveSubscribers);
        assert_that!(config.replay_enabled()).is_true();
        assert_that!(config.replay_retention())
            .is_equal_to(Some(ReplayRetention::LastBytes(16.bytes())));
        assert_that!(config.read_chunk_size).is_equal_to(DEFAULT_READ_CHUNK_SIZE);
        assert_that!(config.max_buffered_chunks).is_equal_to(DEFAULT_MAX_BUFFERED_CHUNKS);
    }

    #[test]
    fn invalid_configs_panic_with_parameter_names() {
        assert_that_panic_by(|| {
            let _config = StreamConfig::builder()
                .best_effort_delivery()
                .no_replay()
                .read_chunk_size(0.bytes());
        })
        .has_type::<String>()
        .is_equal_to("read_chunk_size must be greater than zero bytes");

        assert_that_panic_by(|| {
            let _config = StreamConfig::builder()
                .best_effort_delivery()
                .no_replay()
                .read_chunk_size(8.bytes())
                .max_buffered_chunks(0);
        })
        .has_type::<String>()
        .is_equal_to("max_buffered_chunks must be greater than zero");

        assert_that_panic_by(|| {
            let _config = StreamConfig::builder()
                .best_effort_delivery()
                .replay_last_chunks(0);
        })
        .has_type::<String>()
        .is_equal_to("chunks must retain at least one chunk");

        assert_that_panic_by(|| {
            let _config = StreamConfig::builder()
                .best_effort_delivery()
                .replay_last_bytes(NumBytes::zero());
        })
        .has_type::<String>()
        .is_equal_to("bytes must retain at least one byte");

        assert_that_panic_by(|| {
            let _replay = ReplayEnabled::new(ReplayRetention::LastChunks(0));
        })
        .has_type::<String>()
        .is_equal_to("replay_retention must retain at least one chunk");

        assert_that_panic_by(|| {
            let config = StreamConfig::builder()
                .best_effort_delivery()
                .replay_all()
                .read_chunk_size(8.bytes())
                .max_buffered_chunks(2)
                .build();

            let _config =
                config.with_replay_retention(ReplayRetention::LastBytes(NumBytes::zero()));
        })
        .has_type::<String>()
        .is_equal_to("replay_retention must retain at least one byte");

        assert_that_panic_by(|| {
            let config = StreamConfig {
                read_chunk_size: 8.bytes(),
                max_buffered_chunks: 2,
                delivery: BestEffortDelivery,
                replay: ReplayEnabled {
                    replay_retention: ReplayRetention::LastBytes(NumBytes::zero()),
                },
            };

            config.assert_valid("options");
        })
        .has_type::<String>()
        .is_equal_to("options.replay_retention must retain at least one byte");
    }

    #[tokio::test]
    async fn one_config_constructs_both_stream_backends() {
        use crate::OutputStream;
        use crate::output_stream::backend::broadcast::BroadcastOutputStream;
        use crate::output_stream::backend::single_subscriber::SingleSubscriberOutputStream;

        let config = StreamConfig::builder()
            .best_effort_delivery()
            .no_replay()
            .read_chunk_size(8.bytes())
            .max_buffered_chunks(2)
            .build();

        let broadcast = BroadcastOutputStream::from_stream(tokio::io::empty(), "stdout", config);
        let single_subscriber =
            SingleSubscriberOutputStream::from_stream(tokio::io::empty(), "stderr", config);

        assert_that!(broadcast.read_chunk_size()).is_equal_to(8.bytes());
        assert_that!(single_subscriber.read_chunk_size()).is_equal_to(8.bytes());
        assert_that!(broadcast.max_buffered_chunks()).is_equal_to(2);
        assert_that!(single_subscriber.max_buffered_chunks()).is_equal_to(2);
    }
}