jetstream_rpc/
lib.rs

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
use jetstream_wireformat::{
    wire_format_extensions::AsyncWireFormatExt, WireFormat,
};
use std::fmt::Display;
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

/// A trait representing a message that can be encoded and decoded.
pub trait Message: WireFormat + Send + Sync {}

/// Defines the request and response types for the JetStream protocol.
pub trait Protocol {
    type Request: Message;
    type Response: Message;
}

#[derive(Debug)]
pub enum Error {
    WireFormat,
    Io(std::io::Error),
    Anyhow(anyhow::Error),
    Quic(s2n_quic::connection::Error),
    Custom(Box<dyn std::error::Error + Send + Sync>),
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::Io(e)
    }
}

impl From<anyhow::Error> for Error {
    fn from(e: anyhow::Error) -> Self {
        Error::Anyhow(e)
    }
}

impl From<s2n_quic::connection::Error> for Error {
    fn from(e: s2n_quic::connection::Error) -> Self {
        Error::Quic(e)
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Error::Io(e) => write!(f, "I/O error: {}", e),
            Error::WireFormat => write!(f, "Wire format error"),
            Error::Custom(e) => write!(f, "Custom error: {}", e),
            Error::Anyhow(e) => write!(f, "Anyhow error: {}", e),
            Error::Quic(e) => write!(f, "Quic error: {}", e),
        }
    }
}

/// An asynchronous JetStream service that can handle RPC calls and messages.
#[trait_variant::make(Send+Sync)]
pub trait Service: Protocol {
    /// Handles an RPC call asynchronously.
    async fn rpc(
        &mut self,
        req: Self::Request,
    ) -> Result<Self::Response, Error>;

    /// Handles a message by reading from the reader, processing it, and writing the response.
    async fn handle_message<R, W>(
        &mut self,
        reader: &mut R,
        writer: &mut W,
    ) -> Result<(), Error>
    where
        R: AsyncReadExt + Unpin + Send + Sync,
        W: AsyncWriteExt + Unpin + Send + Sync,
    {
        Box::pin(async move {
            let req = Self::Request::decode_async(reader).await?;
            let resp = self.rpc(req).await?;
            resp.encode_async(writer).await?;
            Ok(())
        })
    }
}

/// A shared, thread-safe JetStream service that can be cloned.
#[derive(Clone)]
pub struct SharedJetStreamService<S: Service> {
    inner: Arc<tokio::sync::Mutex<S>>,
}

impl<S: Service> SharedJetStreamService<S> {
    /// Creates a new shared JetStream service.
    pub fn new(service: S) -> Self {
        Self {
            inner: Arc::new(tokio::sync::Mutex::new(service)),
        }
    }
}

impl<S: Service> Protocol for SharedJetStreamService<S> {
    type Request = S::Request;
    type Response = S::Response;
}

impl<S: Service> Service for SharedJetStreamService<S> {
    async fn rpc(
        &mut self,
        req: Self::Request,
    ) -> Result<Self::Response, Error> {
        let mut service = self.inner.lock().await;
        service.rpc(req).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use jetstream_wireformat::{
        wire_format_extensions::ConvertWireFormat, JetStreamWireFormat,
    };
    use okstd::prelude::*;
    use std::io::Cursor;

    struct TestService;

    #[derive(Debug, PartialEq, Eq, Clone, JetStreamWireFormat)]
    struct TestMessage {
        value: u32,
    }

    impl Message for TestMessage {}

    impl Protocol for TestService {
        type Request = TestMessage;
        type Response = TestMessage;
    }

    impl Service for TestService {
        #[doc = " Handles an RPC call asynchronously."]
        async fn rpc(
            &mut self,
            req: Self::Request,
        ) -> Result<Self::Response, Error> {
            Ok(TestMessage {
                value: req.value + 1,
            })
        }
    }

    #[okstd::test]
    async fn test_shared_service() {
        let mut service = SharedJetStreamService::new(TestService);
        let mut reader = Cursor::new(TestMessage { value: 42 }.to_bytes());
        let mut writer = Cursor::new(vec![]);

        service
            .handle_message(&mut reader, &mut writer)
            .await
            .unwrap();

        let resp =
            TestMessage::from_bytes(&bytes::Bytes::from(writer.into_inner()))
                .unwrap();
        assert_eq!(resp, TestMessage { value: 43 });
    }
}