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
use std::{
    fmt,
    marker::PhantomData,
    pin::Pin,
    task::{Context, Poll},
};

use bytes::{Buf, BufMut, BytesMut};
use futures::{future, Stream};
use futures_util::ready;
use http::StatusCode;
use http_body::Body;
use hyper::body::Incoming;
use pilota::prost::Message;
use tracing::{debug, trace};

use super::{DefaultDecoder, BUFFER_SIZE, PREFIX_LEN};
use crate::{
    codec::{
        compression::{decompress, CompressionEncoding},
        Decoder,
    },
    metadata::MetadataMap,
    status::Code,
    Status,
};

/// Streaming Received Request and Received Response.
///
/// Provides an interface for receiving messages and trailers.
pub struct RecvStream<T> {
    body: Incoming,
    decoder: DefaultDecoder<T>,
    trailers: Option<MetadataMap>,
    buf: BytesMut,
    state: State,
    kind: Kind,
    compression_encoding: Option<CompressionEncoding>,
    decompress_buf: BytesMut,
}

impl<T> Unpin for RecvStream<T> {}

#[derive(Debug, Clone)]
enum State {
    Header,
    Body(Option<CompressionEncoding>, usize),
    Error,
}

#[derive(Debug, PartialEq, Eq)]
pub enum Kind {
    Request,
    Response(StatusCode),
}

impl<T> RecvStream<T> {
    pub fn new(
        body: Incoming,
        kind: Kind,
        compression_encoding: Option<CompressionEncoding>,
    ) -> Self {
        RecvStream {
            body,
            decoder: DefaultDecoder(PhantomData),
            trailers: None,
            buf: BytesMut::with_capacity(BUFFER_SIZE),
            state: State::Header,
            kind,
            compression_encoding,
            decompress_buf: BytesMut::new(),
        }
    }
}

impl<T: Message + Default> RecvStream<T> {
    /// Get the next message from the stream.
    async fn message(&mut self) -> Result<Option<T>, Status> {
        match future::poll_fn(|cx| Pin::new(&mut *self).poll_next(cx)).await {
            Some(Ok(m)) => Ok(Some(m)),
            Some(Err(e)) => Err(e),
            None => Ok(None),
        }
    }

    /// Get the trailers from the stream.
    pub async fn trailers(&mut self) -> Result<Option<MetadataMap>, Status> {
        if let Some(trailers) = self.trailers.take() {
            return Ok(Some(trailers));
        }

        // Ensure read body to the end in case of memory leak.
        // Related issue: https://github.com/hyperium/h2/issues/631.
        while self.message().await?.is_some() {}

        if let Some(trailers) = self.trailers.take() {
            return Ok(Some(trailers));
        }

        let maybe_trailer = future::poll_fn(|cx| Pin::new(&mut self.body).poll_frame(cx)).await;

        match maybe_trailer {
            Some(Ok(frame)) => match frame.into_trailers() {
                Ok(headers) => Ok(Some(MetadataMap::from_headers(headers))),
                Err(_frame) => {
                    // **unreachable** because the `frame` cannot be `Frame::Data` here
                    debug!("[VOLO] unexpected data from stream");
                    Err(Status::new(
                        Code::Internal,
                        "Unexpected data from stream.".to_string(),
                    ))
                }
            },
            Some(Err(err)) => Err(Status::from_error(Box::new(err))),
            None => Ok(None),
        }
    }

    fn decode_chunk(&mut self) -> Result<Option<T>, Status> {
        if let State::Header = self.state {
            // data is not enough to decode header, return and keep reading
            if self.buf.remaining() < PREFIX_LEN {
                return Ok(None);
            }
            trace!("[VOLO-GRPC] streaming received buf: {:?}", self.buf);

            let compression_encoding = match self.buf.get_u8() {
                0 => None,
                1 => {
                    if self.compression_encoding.is_some() {
                        self.compression_encoding
                    } else {
                        return Err(Status::new(
                            Code::Internal,
                            "protocol error: received message with compressed-flag but no \
                             grpc-encoding was specified"
                                .to_string(),
                        ));
                    }
                }
                flag => {
                    let message = format!(
                        "protocol error: received message with invalid compression flag: {flag} \
                         (valid flags are 0 and 1), while sending request"
                    );
                    // https://grpc.github.io/grpc/core/md_doc_compression.html
                    return Err(Status::new(Code::Internal, message));
                }
            };
            let len = self.buf.get_u32() as usize;
            self.buf.reserve(len);

            self.state = State::Body(compression_encoding, len);
        }

        if let State::Body(compression_encoding, len) = &self.state {
            // data is not enough to decode body, return and keep reading
            if self.buf.remaining() < *len || self.buf.len() < *len {
                return Ok(None);
            }
            trace!("[VOLO-GRPC] streaming reading body: {:?}", self.buf);
            let mut buf = self.buf.split_to(*len);
            let decode_result = if let Some(encoding) = compression_encoding {
                self.decompress_buf.clear();
                if let Err(err) = decompress(*encoding, &mut buf, &mut self.decompress_buf) {
                    let message = if let Kind::Response(status) = self.kind {
                        format!(
                            "Error decompressing: {err}, while receiving response with status: \
                             {status}"
                        )
                    } else {
                        format!("Error decompressing: {err}, while sending request")
                    };
                    return Err(Status::new(Code::Internal, message));
                }
                DefaultDecoder::<T>::decode(&mut self.decoder, &mut self.decompress_buf)
            } else {
                DefaultDecoder::<T>::decode(&mut self.decoder, &mut buf)
            };

            return match decode_result {
                Ok(Some(msg)) => {
                    self.state = State::Header;
                    Ok(Some(msg))
                }
                Ok(None) => Ok(None),
                Err(e) => Err(e),
            };
        }

        Ok(None)
    }
}

impl<T: Message + Default> Stream for RecvStream<T> {
    type Item = Result<T, Status>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let trailer_frame = loop {
            if let State::Error = &self.state {
                return Poll::Ready(None);
            }
            if let Some(item) = self.decode_chunk()? {
                return Poll::Ready(Some(Ok(item)));
            }

            match ready!(Pin::new(&mut self.body).poll_frame(cx)) {
                Some(Ok(frame)) => match frame.into_data() {
                    Ok(data) => self.buf.put(data),
                    Err(trailer) => {
                        break Some(trailer);
                    }
                },
                Some(Err(e)) => {
                    let err: crate::BoxError = e.into();
                    let status = Status::from_error(err);
                    if self.kind == Kind::Request && status.code() == Code::Cancelled {
                        return Poll::Ready(None);
                    }
                    debug!("[VOLO] decoder inner stream error: {:?}", status);
                    let _ = std::mem::replace(&mut self.state, State::Error);
                    return Poll::Ready(Some(Err(status)));
                }
                None => {
                    if self.buf.has_remaining() {
                        debug!("[VOLO] unexpected EOF decoding stream");
                        return Poll::Ready(Some(Err(Status::new(
                            Code::Internal,
                            "Unexpected EOF decoding stream.".to_string(),
                        ))));
                    } else {
                        break None;
                    }
                }
            }
        };

        if let Kind::Response(status) = self.kind {
            let trailer = match trailer_frame.map(|frame| frame.into_trailers()) {
                Some(Ok(trailer)) => Some(trailer),
                Some(Err(_frame)) => {
                    // **unreachable** because the `frame` cannot be `Frame::Data` here
                    debug!("[VOLO] unexpected data from stream");
                    return Poll::Ready(Some(Err(Status::new(
                        Code::Internal,
                        "Unexpected data from stream.".to_string(),
                    ))));
                }
                None => None,
            };

            if let Err(e) = Status::infer_grpc_status(trailer.as_ref(), status) {
                return if let Some(e) = e {
                    Some(Err(e)).into()
                } else {
                    Poll::Ready(None)
                };
            } else {
                self.trailers = trailer.map(MetadataMap::from_headers);
            }
        }

        Poll::Ready(None)
    }
}

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