starpc 0.49.13

Streaming protobuf RPC framework
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//! RpcStream trait and functions for opening/handling nested RPC streams.

use async_trait::async_trait;
use bytes::Bytes;
use prost::Message;
use std::sync::Arc;

use crate::client::{OpenStream, PacketReceiver};
use crate::error::{Error, Result};
use crate::invoker::Invoker;
use crate::proto::Packet;
use crate::rpc::{PacketWriter, ServerRpc};
use crate::stream::{Context, Stream};

use super::{rpc_stream_packet, RpcAck, RpcStreamInit, RpcStreamPacket};
use super::RpcStreamWriter;

/// RpcStream is a bidirectional stream for RpcStreamPacket messages.
///
/// This trait extends the base Stream trait with typed send/recv operations
/// for RpcStreamPacket messages.
#[async_trait]
pub trait RpcStream: Stream {
    /// Sends an RpcStreamPacket.
    async fn send_packet(&self, packet: &RpcStreamPacket) -> Result<()>;

    /// Receives an RpcStreamPacket.
    async fn recv_packet(&self) -> Result<RpcStreamPacket>;
}

/// Generic RpcStream implementation wrapping any Stream.
pub struct RpcStreamImpl<S: Stream> {
    inner: S,
}

impl<S: Stream> RpcStreamImpl<S> {
    /// Creates a new RpcStreamImpl wrapping the given stream.
    pub fn new(inner: S) -> Self {
        Self { inner }
    }
}

#[async_trait]
impl<S: Stream + Send + Sync> Stream for RpcStreamImpl<S> {
    fn context(&self) -> &Context {
        self.inner.context()
    }

    async fn send_bytes(&self, data: Bytes) -> Result<()> {
        self.inner.send_bytes(data).await
    }

    async fn recv_bytes(&self) -> Result<Bytes> {
        self.inner.recv_bytes().await
    }

    async fn close_send(&self) -> Result<()> {
        self.inner.close_send().await
    }

    async fn close(&self) -> Result<()> {
        self.inner.close().await
    }
}

#[async_trait]
impl<S: Stream + Send + Sync> RpcStream for RpcStreamImpl<S> {
    async fn send_packet(&self, packet: &RpcStreamPacket) -> Result<()> {
        let data = packet.encode_to_vec();
        self.inner.send_bytes(Bytes::from(data)).await
    }

    async fn recv_packet(&self) -> Result<RpcStreamPacket> {
        let data = self.inner.recv_bytes().await?;
        RpcStreamPacket::decode(&data[..]).map_err(Error::InvalidMessage)
    }
}

// Implement RpcStream for Arc<S> where S: RpcStream
#[async_trait]
impl<S: RpcStream + ?Sized + Send + Sync> RpcStream for Arc<S> {
    async fn send_packet(&self, packet: &RpcStreamPacket) -> Result<()> {
        (**self).send_packet(packet).await
    }

    async fn recv_packet(&self) -> Result<RpcStreamPacket> {
        (**self).recv_packet().await
    }
}

/// Getter function to resolve component ID to an invoker.
///
/// # Arguments
/// * `ctx` - Context for the RPC stream
/// * `component_id` - The component ID to look up
/// * `released` - Callback to invoke when the stream is released
///
/// # Returns
/// `Some((invoker, release_fn))` if found, `None` if not found.
pub type RpcStreamGetter = Arc<
    dyn Fn(&Context, &str, Box<dyn FnOnce() + Send>) -> Option<(Arc<dyn Invoker>, Box<dyn FnOnce() + Send>)>
        + Send
        + Sync,
>;

impl RpcStreamPacket {
    /// Creates a new Init packet.
    pub fn new_init(component_id: String) -> Self {
        Self {
            body: Some(rpc_stream_packet::Body::Init(RpcStreamInit { component_id })),
        }
    }

    /// Creates a new Ack packet.
    pub fn new_ack(error: String) -> Self {
        Self {
            body: Some(rpc_stream_packet::Body::Ack(RpcAck { error })),
        }
    }

    /// Creates a new Data packet.
    pub fn new_data(data: impl Into<Vec<u8>>) -> Self {
        Self {
            body: Some(rpc_stream_packet::Body::Data(data.into())),
        }
    }
}

/// Opens an RPC stream with a remote component.
///
/// This function performs the client-side init/ack handshake:
/// 1. Sends RpcStreamInit with the component ID
/// 2. Optionally waits for RpcAck from the server
///
/// # Arguments
/// * `stream` - The underlying RPC stream
/// * `component_id` - The target component ID
/// * `wait_ack` - Whether to wait for acknowledgment
///
/// # Returns
/// Ok(()) on success
pub async fn open_rpc_stream<S: RpcStream + Send + Sync>(
    stream: &S,
    component_id: &str,
    wait_ack: bool,
) -> Result<()> {
    // Send the init packet
    let init_packet = RpcStreamPacket::new_init(component_id.to_string());
    stream.send_packet(&init_packet).await?;

    // Wait for ack if requested
    if wait_ack {
        let ack_packet = stream.recv_packet().await?;
        match ack_packet.body {
            Some(rpc_stream_packet::Body::Ack(ack)) => {
                if !ack.error.is_empty() {
                    return Err(Error::Remote(format!("remote: {}", ack.error)));
                }
            }
            _ => {
                return Err(Error::UnrecognizedPacket);
            }
        }
    }

    Ok(())
}

/// Handles an incoming RPC stream (server side).
///
/// This function handles the server-side of the rpcstream protocol:
/// 1. Receives RpcStreamInit with component ID
/// 2. Looks up the invoker for that component
/// 3. Sends RpcAck (with error if not found)
/// 4. Handles nested RPC calls over the stream
///
/// # Arguments
/// * `stream` - The incoming RPC stream
/// * `getter` - Function to look up invokers by component ID
pub async fn handle_rpc_stream<S: RpcStream + Send + Sync + 'static>(
    stream: Arc<S>,
    getter: RpcStreamGetter,
) -> Result<()> {
    // Read the init packet
    let init_packet = stream.recv_packet().await?;
    let component_id = match init_packet.body {
        Some(rpc_stream_packet::Body::Init(init)) => init.component_id,
        _ => {
            return Err(Error::UnrecognizedPacket);
        }
    };

    let ctx = stream.context().child();

    // Look up the invoker
    let ctx_cancel = ctx.clone();
    let released = Box::new(move || {
        ctx_cancel.cancel();
    });

    let lookup_result = getter(&ctx, &component_id, released);

    // Send ack
    let (invoker, release_fn) = match lookup_result {
        Some((inv, rel)) => {
            // Send success ack
            stream
                .send_packet(&RpcStreamPacket::new_ack(String::new()))
                .await?;
            (inv, Some(rel))
        }
        None => {
            // Send error ack
            let err_msg = format!("no server for component: {}", component_id);
            stream
                .send_packet(&RpcStreamPacket::new_ack(err_msg.clone()))
                .await?;
            return Err(Error::Remote(err_msg));
        }
    };

    // Ensure release is called when we're done
    let _release_guard = scopeguard::guard(release_fn, |rel| {
        if let Some(f) = rel {
            f();
        }
    });

    // Create a writer for the RPC stream
    let writer: Arc<dyn PacketWriter> = Arc::new(RpcStreamWriter::new(stream.clone()));

    // Read and handle packets
    loop {
        let rpc_packet = match stream.recv_packet().await {
            Ok(p) => p,
            Err(Error::StreamClosed) => break,
            Err(e) => return Err(e),
        };

        let packet = match rpc_packet.body {
            Some(rpc_stream_packet::Body::Data(data)) => {
                match Packet::decode(&data[..]) {
                    Ok(p) => p,
                    Err(e) => return Err(Error::InvalidMessage(e)),
                }
            }
            _ => continue, // Ignore non-data packets
        };

        // Handle the packet based on its type
        use crate::proto::packet::Body;
        match packet.body {
            Some(Body::CallStart(call_start)) => {
                let rpc_ctx = ctx.child();
                let rpc = Arc::new(ServerRpc::from_call_start(
                    rpc_ctx,
                    call_start,
                    writer.clone(),
                ));

                let service_id = rpc.service().to_string();
                let method_id = rpc.method().to_string();

                // Spawn a task to handle this RPC
                let invoker_clone = invoker.clone();
                tokio::spawn(async move {
                    let (_found, _result) = invoker_clone
                        .invoke_method(&service_id, &method_id, Box::new(ServerRpcStream { rpc }))
                        .await;
                });
            }
            Some(Body::CallData(_)) | Some(Body::CallCancel(_)) => {
                // These should be routed to an existing RPC
                // In this simplified implementation, they're ignored
            }
            None => {}
        }
    }

    Ok(())
}

/// Wrapper to make ServerRpc implement Stream for use with invoke_method.
struct ServerRpcStream {
    rpc: Arc<ServerRpc>,
}

#[async_trait]
impl Stream for ServerRpcStream {
    fn context(&self) -> &Context {
        self.rpc.context()
    }

    async fn send_bytes(&self, data: Bytes) -> Result<()> {
        crate::stream::Stream::send_bytes(self.rpc.as_ref(), data).await
    }

    async fn recv_bytes(&self) -> Result<Bytes> {
        crate::stream::Stream::recv_bytes(self.rpc.as_ref()).await
    }

    async fn close_send(&self) -> Result<()> {
        crate::stream::Stream::close_send(self.rpc.as_ref()).await
    }

    async fn close(&self) -> Result<()> {
        crate::stream::Stream::close(self.rpc.as_ref()).await
    }
}

/// Creates an OpenStream function using an RPC stream caller.
///
/// This allows creating a Client that operates over an RPC stream,
/// enabling nested RPC calls.
///
/// # Type Parameters
/// * `F` - Async function that creates a new stream
/// * `S` - Stream type
pub fn new_rpc_stream_open_stream<F, Fut, S>(
    caller: F,
    component_id: String,
    wait_ack: bool,
) -> impl OpenStream
where
    F: Fn() -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = Result<S>> + Send + 'static,
    S: Stream + Send + Sync + 'static,
{
    RpcStreamOpener {
        caller: Arc::new(caller),
        component_id,
        wait_ack,
        _phantom: std::marker::PhantomData,
    }
}

struct RpcStreamOpener<F, S> {
    caller: Arc<F>,
    component_id: String,
    wait_ack: bool,
    _phantom: std::marker::PhantomData<S>,
}

#[async_trait]
impl<F, Fut, S> OpenStream for RpcStreamOpener<F, S>
where
    F: Fn() -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = Result<S>> + Send + 'static,
    S: Stream + Send + Sync + 'static,
{
    async fn open_stream(&self) -> Result<(Arc<dyn PacketWriter>, PacketReceiver)> {
        // Open the underlying stream
        let stream = (self.caller)().await?;
        let rpc_stream = Arc::new(RpcStreamImpl::new(stream));

        // Perform the init/ack handshake
        open_rpc_stream(rpc_stream.as_ref(), &self.component_id, self.wait_ack).await?;

        // Create a writer
        let writer: Arc<dyn PacketWriter> = Arc::new(RpcStreamWriter::new(rpc_stream.clone()));

        // Create a channel for incoming packets
        let (tx, rx) = tokio::sync::mpsc::channel(32);

        // Spawn a read pump to convert RpcStreamPacket::Data into Packets
        let stream_clone = rpc_stream.clone();
        tokio::spawn(async move {
            loop {
                match stream_clone.recv_packet().await {
                    Ok(packet) => {
                        if let Some(rpc_stream_packet::Body::Data(data)) = packet.body {
                            match Packet::decode(&data[..]) {
                                Ok(p) => {
                                    if tx.send(p).await.is_err() {
                                        break;
                                    }
                                }
                                Err(_) => break,
                            }
                        }
                    }
                    Err(_) => break,
                }
            }
        });

        Ok((writer, rx))
    }
}

/// Creates a Client that operates over an RPC stream.
///
/// # Arguments
/// * `caller` - Function that opens a new RPC stream
/// * `component_id` - Target component ID
/// * `wait_ack` - Whether to wait for acknowledgment
pub fn new_rpc_stream_client<F, Fut, S>(
    caller: F,
    component_id: String,
    wait_ack: bool,
) -> crate::SrpcClient<impl OpenStream>
where
    F: Fn() -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = Result<S>> + Send + 'static,
    S: Stream + Send + Sync + 'static,
{
    let opener = new_rpc_stream_open_stream(caller, component_id, wait_ack);
    crate::SrpcClient::new(opener)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicBool, Ordering};
    use tokio::sync::Mutex;
    use std::collections::VecDeque;

    struct MockRpcStream {
        ctx: Context,
        send_queue: Mutex<VecDeque<RpcStreamPacket>>,
        recv_queue: Mutex<VecDeque<RpcStreamPacket>>,
        closed: AtomicBool,
    }

    impl MockRpcStream {
        fn new() -> Self {
            Self {
                ctx: Context::new(),
                send_queue: Mutex::new(VecDeque::new()),
                recv_queue: Mutex::new(VecDeque::new()),
                closed: AtomicBool::new(false),
            }
        }

        async fn push_recv(&self, packet: RpcStreamPacket) {
            self.recv_queue.lock().await.push_back(packet);
        }

        async fn pop_sent(&self) -> Option<RpcStreamPacket> {
            self.send_queue.lock().await.pop_front()
        }
    }

    #[async_trait]
    impl Stream for MockRpcStream {
        fn context(&self) -> &Context {
            &self.ctx
        }

        async fn send_bytes(&self, data: Bytes) -> Result<()> {
            let packet = RpcStreamPacket::decode(&data[..]).map_err(Error::InvalidMessage)?;
            self.send_queue.lock().await.push_back(packet);
            Ok(())
        }

        async fn recv_bytes(&self) -> Result<Bytes> {
            loop {
                if let Some(packet) = self.recv_queue.lock().await.pop_front() {
                    return Ok(Bytes::from(packet.encode_to_vec()));
                }
                if self.closed.load(Ordering::SeqCst) {
                    return Err(Error::StreamClosed);
                }
                tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
            }
        }

        async fn close_send(&self) -> Result<()> {
            Ok(())
        }

        async fn close(&self) -> Result<()> {
            self.closed.store(true, Ordering::SeqCst);
            Ok(())
        }
    }

    #[async_trait]
    impl RpcStream for MockRpcStream {
        async fn send_packet(&self, packet: &RpcStreamPacket) -> Result<()> {
            self.send_queue.lock().await.push_back(packet.clone());
            Ok(())
        }

        async fn recv_packet(&self) -> Result<RpcStreamPacket> {
            loop {
                if let Some(packet) = self.recv_queue.lock().await.pop_front() {
                    return Ok(packet);
                }
                if self.closed.load(Ordering::SeqCst) {
                    return Err(Error::StreamClosed);
                }
                tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
            }
        }
    }

    #[tokio::test]
    async fn test_open_rpc_stream_no_ack() {
        let stream = MockRpcStream::new();

        let result = open_rpc_stream(&stream, "test-component", false).await;
        assert!(result.is_ok());

        // Check that init was sent
        let sent = stream.pop_sent().await.unwrap();
        match sent.body {
            Some(rpc_stream_packet::Body::Init(init)) => {
                assert_eq!(init.component_id, "test-component");
            }
            _ => panic!("Expected Init packet"),
        }
    }

    #[tokio::test]
    async fn test_open_rpc_stream_with_ack() {
        let stream = MockRpcStream::new();

        // Pre-queue an ack response
        stream
            .push_recv(RpcStreamPacket::new_ack(String::new()))
            .await;

        let result = open_rpc_stream(&stream, "test-component", true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_open_rpc_stream_with_error_ack() {
        let stream = MockRpcStream::new();

        // Pre-queue an error ack response
        stream
            .push_recv(RpcStreamPacket::new_ack("component not found".to_string()))
            .await;

        let result = open_rpc_stream(&stream, "test-component", true).await;
        assert!(matches!(result, Err(Error::Remote(_))));
    }
}