-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
sender.rs
374 lines (331 loc) · 13.9 KB
/
sender.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
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
use std::{
fmt, mem,
pin::Pin,
task::{Context, Poll},
};
use futures::{ready, Sink};
use pin_project::pin_project;
use tokio::sync::mpsc::Sender;
use super::poll_sender::PollSender;
use crate::{buffer_usage_data::BufferUsageHandle, Bufferable, WhenFull};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum SendState {
// This sender should drop the next item it receives.
DropNext,
// The base sender is ready to be sent an item.
BaseReady,
// The overflow sender is ready to be sent an item.
OverflowReady,
// Default state.
Idle,
}
impl SendState {
fn is_ready(self) -> bool {
matches!(self, SendState::BaseReady | SendState::OverflowReady)
}
}
// Some type-level tomfoolery to have a trait that represents a `Sink` that can be cloned.
/// A [`Sink`] that can be cloned.
///
/// Required due to limitations around using non-auto traits in trait signatures. If your [`Sink`]
/// implementation is also `Clone`, then you are covered by the blanket trait implementation.
pub trait CloneableSink<Item, E>: Sink<Item, Error = E> + Send + dyn_clone::DynClone {}
impl<T, Item, E> CloneableSink<Item, E> for T where T: Sink<Item, Error = E> + Send + Clone {}
dyn_clone::clone_trait_object!(<T, E> CloneableSink<T, E>);
/// Adapter for papering over various sender backends by providing a [`Sink`] interface.
#[pin_project(project = ProjectedSenderAdapter)]
pub enum SenderAdapter<T> {
/// A sender that uses a Tokio MPSC channel.
Channel(PollSender<T>),
/// A sender that provides its own [`Sink`] implementation.
Opaque(Pin<Box<dyn CloneableSink<T, ()>>>),
}
impl<T> SenderAdapter<T>
where
T: Bufferable,
{
pub fn channel(tx: Sender<T>) -> Self {
SenderAdapter::Channel(PollSender::new(tx))
}
pub fn opaque<S>(inner: S) -> Self
where
S: CloneableSink<T, ()> + 'static,
{
SenderAdapter::Opaque(Box::pin(inner))
}
pub fn capacity(&self) -> Option<usize> {
match self {
Self::Channel(tx) => tx.get_ref().map(Sender::capacity),
Self::Opaque(_) => None,
}
}
}
impl<T> Clone for SenderAdapter<T> {
fn clone(&self) -> Self {
match self {
Self::Channel(tx) => Self::Channel(tx.clone()),
Self::Opaque(sink) => Self::Opaque(sink.clone()),
}
}
}
impl<T> fmt::Debug for SenderAdapter<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Channel(_) => f.debug_tuple("inner").field(&"Channel").finish(),
Self::Opaque(_) => f.debug_tuple("inner").field(&"Opaque").finish(),
}
}
}
impl<T> Sink<T> for SenderAdapter<T>
where
T: Bufferable,
{
type Error = ();
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.project() {
ProjectedSenderAdapter::Channel(tx) => tx.poll_reserve(cx).map_err(|_| ()),
ProjectedSenderAdapter::Opaque(inner) => inner.as_mut().poll_ready(cx),
}
}
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
match self.project() {
ProjectedSenderAdapter::Channel(tx) => tx.start_send(item).map_err(|_| ()),
ProjectedSenderAdapter::Opaque(inner) => inner.as_mut().start_send(item),
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.project() {
// There's nothing to actually flush when using `PollSender<T>`.
ProjectedSenderAdapter::Channel(_) => Poll::Ready(Ok(())),
ProjectedSenderAdapter::Opaque(inner) => inner.as_mut().poll_flush(cx),
}
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.project() {
ProjectedSenderAdapter::Channel(tx) => {
if !tx.is_closed() {
tx.close();
}
Poll::Ready(Ok(()))
}
ProjectedSenderAdapter::Opaque(inner) => inner.as_mut().poll_close(cx),
}
}
}
/// A buffer sender.
///
/// The sender handles sending events into the buffer, as well as the behavior around handling
/// events when the internal channel is full.
///
/// When creating a buffer sender/receiver pair, callers can specify the "when full" behavior of the
/// sender. This controls how events are handled when the internal channel is full. Three modes
/// are possible:
/// - block
/// - drop newest
/// - overflow
///
/// In "block" mode, callers are simply forced to wait until the channel has enough capacity to
/// accept the event. In "drop newest" mode, any event being sent when the channel is full will be
/// dropped and proceed no further. In "overflow" mode, events will be sent to another buffer
/// sender. Callers can specify the overflow sender to use when constructing their buffers initially.
#[pin_project]
#[derive(Debug)]
pub struct BufferSender<T> {
#[pin]
base: SenderAdapter<T>,
base_flush: bool,
#[pin]
overflow: Option<Box<BufferSender<T>>>,
overflow_flush: bool,
state: SendState,
when_full: WhenFull,
instrumentation: Option<BufferUsageHandle>,
}
impl<T> BufferSender<T> {
/// Creates a new [`BufferSender`] wrapping the given channel sender.
pub fn new(base: SenderAdapter<T>, when_full: WhenFull) -> Self {
Self {
base,
base_flush: false,
overflow: None,
overflow_flush: false,
state: SendState::Idle,
when_full,
instrumentation: None,
}
}
/// Creates a new [`BufferSender`] wrapping the given channel sender and overflow sender.
pub fn with_overflow(base: SenderAdapter<T>, overflow: BufferSender<T>) -> Self {
Self {
base,
base_flush: false,
overflow: Some(Box::new(overflow)),
overflow_flush: false,
state: SendState::Idle,
when_full: WhenFull::Overflow,
instrumentation: None,
}
}
/// Converts this sender into an overflowing sender using the given `BufferSender<T>`.
///
/// Note: this resets the internal state of this sender, and so this should not be called except
/// when initially constructing `BufferSender<T>`.
#[cfg(test)]
pub fn switch_to_overflow(&mut self, overflow: BufferSender<T>) {
self.overflow = Some(Box::new(overflow));
self.when_full = WhenFull::Overflow;
self.state = SendState::Idle;
self.base_flush = false;
self.overflow_flush = false;
}
/// Configures this sender to instrument the items passing through it.
pub fn with_instrumentation(&mut self, handle: BufferUsageHandle) {
self.instrumentation = Some(handle);
}
}
impl<T: Bufferable> BufferSender<T> {
#[cfg(test)]
pub(crate) fn get_base_ref(&self) -> &SenderAdapter<T> {
&self.base
}
#[cfg(test)]
pub(crate) fn get_overflow_ref(&self) -> Option<&BufferSender<T>> {
self.overflow.as_ref().map(AsRef::as_ref)
}
}
impl<T> Clone for BufferSender<T> {
fn clone(&self) -> Self {
Self {
base: self.base.clone(),
base_flush: false,
overflow: self.overflow.clone(),
overflow_flush: false,
state: SendState::Idle,
when_full: self.when_full,
instrumentation: self.instrumentation.clone(),
}
}
}
impl<T: Bufferable> Sink<T> for BufferSender<T> {
type Error = ();
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let this = self.project();
// For whatever reason, the caller is calling `poll_ready` again after a successful previous
// call. Since we already know we're ready, and `start_send` has not yet been called, we
// can simply short circuit here and return that we're (still) ready.
if this.state.is_ready() {
return Poll::Ready(Ok(()));
}
let (result, next_state) = match this.base.poll_ready(cx) {
Poll::Ready(result) => match result {
// We reserved a sending slot in the base channel.
Ok(()) => (Poll::Ready(Ok(())), SendState::BaseReady),
// Base sender's underlying channel is closed.
Err(e) => (Poll::Ready(Err(e)), SendState::Idle),
},
// Our base sender was not able to immediately reserve a sending slot.
Poll::Pending => match this.when_full {
// We need to block. Nothing else to do, as the base sender will notify us when
// there's capacity to do the send.
WhenFull::Block => (Poll::Pending, SendState::Idle),
// We need to drop the next item. We have to wait until the caller hands it over to
// us in order to drop it, though, so we pretend we're ready and mark ourselves to
// drop the next item when `start_send` is called.
//
// One "gotcha" here is that the base sender is still trying to reserve a sending
// slot for us, so technically it could complete between now and when we get to
// `start_send` and actually drop the item.
//
// Based on the current behavior of `PollSender<T>`, the best thing we can do here
// is to simply to drop the item and not abort the send, since that will leave
// `PollSender<T>` armed for the next time we call `poll_reserve`. Since buffers
// are SPSC, there's no risk in trying up a sender slot.
//
// TODO: In the future, `PollSender<T>::start_send` may be tweaked to attempt a
// call to `Sender<T>::try_send` as a last ditch effort when `PollSender<T>` has not
// yet reserved the sending slot. We could take advantage of this ourselves.
WhenFull::DropNewest => (Poll::Ready(Ok(())), SendState::DropNext),
// We're supposed to overflow. Quickly check to make sure we even have an overflow
// sender configured, and then figure out if the overflow sender can actually accept
// a send at the moment.
WhenFull::Overflow => match this.overflow.as_pin_mut() {
None => panic!("overflow mode set, but no overflow sender present"),
Some(overflow) => match overflow.poll_ready(cx) {
// Our overflow sender is ready for sending, so we mark ourselves so we know
// which sender to write to when `start_send` is called next.
Poll::Ready(result) => match result {
Ok(()) => (Poll::Ready(Ok(())), SendState::OverflowReady),
Err(e) => (Poll::Ready(Err(e)), SendState::Idle),
},
// Our overflow sender is not ready, either, so there's nothing else to do
// here except wait for a wakeup from either the base sender or overflow sender.
Poll::Pending => (Poll::Pending, SendState::Idle),
},
},
},
};
*this.state = next_state;
result
}
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
let this = self.project();
let item_size = this.instrumentation.as_ref().map(|_| item.size_of());
match mem::replace(this.state, SendState::Idle) {
// Sender isn't ready at all.
SendState::Idle => panic!(
"`start_send` should not be called unless `poll_ready` returned successfully"
),
// We've been instructed to drop the next item.
SendState::DropNext => {
if let Some(instrumentation) = this.instrumentation.as_ref() {
instrumentation.try_increment_dropped_event_count(1);
}
Ok(())
}
// Base is ready, so send the item there.
SendState::BaseReady => {
let result = this.base.start_send(item);
if result.is_ok() {
*this.base_flush = true;
if let Some(item_size) = item_size {
// Only update our instrumentation if _we_ got the item, not the overflow.
let handle = this
.instrumentation
.as_ref()
.expect("item_size can't be present without instrumentation");
handle.increment_received_event_count_and_byte_size(1, item_size as u64);
}
}
result
}
// Overflow is ready, so send the item there.
SendState::OverflowReady => {
let result = this.overflow.as_pin_mut().unwrap().start_send(item);
if result.is_ok() {
*this.overflow_flush = true;
}
result
}
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let this = self.project();
if *this.base_flush {
ready!(this.base.poll_flush(cx))?;
*this.base_flush = false;
}
if *this.overflow_flush {
ready!(this.overflow.as_pin_mut().unwrap().poll_flush(cx))?;
*this.overflow_flush = false;
}
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let this = self.project();
if let Some(overflow) = this.overflow.as_pin_mut() {
ready!(overflow.poll_close(cx))?;
}
ready!(this.base.poll_flush(cx))?;
Poll::Ready(Ok(()))
}
}