arrow_flight/streams.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! [`FallibleRequestStream`] and [`FallibleTonicResponseStream`] adapters
6
7use crate::error::FlightError;
8use futures::{
9 FutureExt, Stream, StreamExt,
10 channel::oneshot::{Receiver, Sender},
11};
12use std::pin::Pin;
13use std::task::{Poll, ready};
14
15/// Wrapper around a fallible stream (one that returns errors) that makes it infallible.
16///
17/// Any errors encountered in the stream are ignored are sent to the provided
18/// oneshot sender.
19///
20/// This can be used to accept a stream of `Result<_>` from a client API and send
21/// them to the remote server that wants only the successful results.
22pub struct FallibleRequestStream<T, E> {
23 /// sender to notify error
24 sender: Option<Sender<E>>,
25 /// fallible stream
26 fallible_stream: Pin<Box<dyn Stream<Item = std::result::Result<T, E>> + Send + 'static>>,
27}
28
29impl<T, E> FallibleRequestStream<T, E> {
30 /// Create a FallibleRequestStream
31 pub fn new(
32 sender: Sender<E>,
33 fallible_stream: Pin<Box<dyn Stream<Item = std::result::Result<T, E>> + Send + 'static>>,
34 ) -> Self {
35 Self {
36 sender: Some(sender),
37 fallible_stream,
38 }
39 }
40}
41
42impl<T, E> Stream for FallibleRequestStream<T, E> {
43 type Item = T;
44
45 fn poll_next(
46 self: std::pin::Pin<&mut Self>,
47 cx: &mut std::task::Context<'_>,
48 ) -> std::task::Poll<Option<Self::Item>> {
49 let pinned = self.get_mut();
50 let mut request_streams = pinned.fallible_stream.as_mut();
51 match ready!(request_streams.poll_next_unpin(cx)) {
52 Some(Ok(data)) => Poll::Ready(Some(data)),
53 Some(Err(e)) => {
54 // in theory this should only ever be called once
55 // as this stream should not be polled again after returning
56 // None, however we still check for None to be safe
57 if let Some(sender) = pinned.sender.take() {
58 // an error means the other end of the channel is not around
59 // to receive the error, so ignore it
60 let _ = sender.send(e);
61 }
62 Poll::Ready(None)
63 }
64 None => Poll::Ready(None),
65 }
66 }
67}
68
69/// Wrapper for a tonic response stream that maps errors to `FlightError` and
70/// returns errors from a oneshot channel into the stream.
71///
72/// The user of this stream can inject an error into the response stream using
73/// the one shot receiver. This is used to propagate errors in
74/// [`FlightClient::do_put`] and [`FlightClient::do_exchange`] from the client
75/// provided input stream to the response stream.
76///
77/// # Error Priority
78/// Error from the receiver are prioritised over the response stream.
79///
80/// [`FlightClient::do_put`]: crate::FlightClient::do_put
81/// [`FlightClient::do_exchange`]: crate::FlightClient::do_exchange
82pub(crate) struct FallibleTonicResponseStream<T> {
83 /// Receiver for FlightError
84 receiver: Receiver<FlightError>,
85 /// Tonic response stream
86 response_stream: Pin<Box<dyn Stream<Item = Result<T, tonic::Status>> + Send + 'static>>,
87}
88
89impl<T> FallibleTonicResponseStream<T> {
90 pub(crate) fn new(
91 receiver: Receiver<FlightError>,
92 response_stream: Pin<Box<dyn Stream<Item = Result<T, tonic::Status>> + Send + 'static>>,
93 ) -> Self {
94 Self {
95 receiver,
96 response_stream,
97 }
98 }
99}
100
101impl<T> Stream for FallibleTonicResponseStream<T> {
102 type Item = Result<T, FlightError>;
103
104 fn poll_next(
105 self: Pin<&mut Self>,
106 cx: &mut std::task::Context<'_>,
107 ) -> Poll<Option<Self::Item>> {
108 let pinned = self.get_mut();
109 let receiver = &mut pinned.receiver;
110 // Prioritise sending the error that's been notified over
111 // polling the response_stream
112 if let Poll::Ready(Ok(err)) = receiver.poll_unpin(cx) {
113 return Poll::Ready(Some(Err(err)));
114 }
115
116 match ready!(pinned.response_stream.poll_next_unpin(cx)) {
117 Some(Ok(res)) => Poll::Ready(Some(Ok(res))),
118 Some(Err(status)) => Poll::Ready(Some(Err(status.into()))),
119 None => Poll::Ready(None),
120 }
121 }
122}