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
//! Backpressure handling structures
//!
//! The usual way to apply backpressure to a stream is using one of the
//! [`ListenExt`](../trait.ListenExt.html) trait methods:
//! * [`backpressure`](../trait.ListenExt.html#method.backpressure)
//! * [`apply_backpressure`](../trait.ListenExt.html#method.apply_backpressure)
//! * [`backpressure_wrapper`](../trait.ListenExt.html#method.backpressure_wrapper)
//!
//! Also take a look at [`backpressure::new`](fn.new.html) for the low-level
//! interface.
//!
use std::fmt;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, TryLockError};

use async_std::stream::Stream;
use async_std::future::Future;
use async_std::task::{Poll, Context, Waker};

use crate::byte_stream::ByteStream;


struct Inner {
    active: AtomicUsize,
    limit: AtomicUsize,
    task: Mutex<Option<Waker>>,
}

/// A stream combinator that applies backpressure
///
/// See
/// [`ListenExt::backpressure`](../trait.ListenExt.html#method.backpressure)
/// for more info.
pub struct BackpressureToken<S>(Backpressure<S>);

/// A stream combinator that applies backpressure and yields ByteStream
///
/// See
/// [`ListenExt::backpressure_wrapper`](../trait.ListenExt.html#method.backpressure_wrapper)
/// for more info.
pub struct BackpressureWrapper<S>(Backpressure<S>);

/// A stream combinator that applies backpressure and yields a token
///
/// See
/// [`ListenExt::apply_backpressure`](../trait.ListenExt.html#method.apply_backpressure)
/// for more info.
pub struct Backpressure<S> {
    stream: S,
    backpressure: Receiver,
}

/// The throttler of a stream
///
/// See [`new`](fn.new.html) for more details
pub struct Receiver {
    inner: Arc<Inner>,
}

/// Future that resolves when there is less that limit tokens alive
pub struct HasCapacity<'a> {
    recv: &'a mut Receiver,
}

/// The handle that controls backpressure
///
/// It can be used to create tokens, changing limit and getting metrics.
///
/// See [`new`](fn.new.html) for more details
#[derive(Clone)]
pub struct Sender {
    inner: Arc<Inner>,
}

/// The token which holds onto a single resource item
pub struct Token {
    inner: Arc<Inner>,
}

impl<S: Unpin> Unpin for Backpressure<S> {}
impl<S: Unpin> Unpin for BackpressureToken<S> {}
impl<S: Unpin> Unpin for BackpressureWrapper<S> {}

impl Sender {
    /// Acquire a backpressure token
    ///
    /// The token holds one unit of resource
    ///
    /// *Note:* You can always acquire a token, even if capacity limit reached.
    pub fn token(&self) -> Token {
        self.inner.active.fetch_add(1, Ordering::SeqCst);
        Token {
            inner: self.inner.clone(),
        }
    }
    /// Change the limit for the number of connections
    ///
    /// If limit is increased it's applied immediately. If limit is lowered,
    /// we can't drop connections. So listening stream is paused until
    /// there are less then new limit tokens alive (i.e. first dropped
    /// tokens may not unblock the stream).
    pub fn set_limit(&self, new_limit: usize) {
        let old_limit = self.inner.limit.swap(new_limit, Ordering::SeqCst);
        if old_limit < new_limit {
            match self.inner.task.try_lock() {
                Ok(mut guard) => {
                    guard.take().map(|w| w.wake());
                }
                Err(TryLockError::WouldBlock) => {
                    // This means either another token is currently waking
                    // up a Receiver. Or Receiver is currently running.
                    // Receiver will recheck values after releasing the Mutex.
                }
                Err(TryLockError::Poisoned(_)) => {
                    unreachable!("backpressure lock should never be poisoned");
                }
            }
        }
    }

    /// Returns the number of currently active tokens
    ///
    /// Can return a value larger than limit if tokens are created manually.
    ///
    /// This can be used for metrics or debugging. You should not rely on
    /// this value being in sync. There is also no way to wake-up when this
    /// value is lower than limit, also see
    /// [`has_capacity`](struct.Receiver.html#method.has_capacity).
    pub fn get_active_tokens(&self) -> usize {
        self.inner.active.load(Ordering::Relaxed)
    }
}

impl Receiver {
    /// Handy to create token in Backpressure wrapper
    fn token(&self) -> Token {
        self.inner.active.fetch_add(1, Ordering::SeqCst);
        Token {
            inner: self.inner.clone(),
        }
    }

    /// Return future which resolves when the current number active of tokens
    /// is less than a limit
    ///
    /// If you create tokens in different task than the task that waits
    /// on `HasCapacity` there is a race condition.
    pub fn has_capacity(&mut self) -> HasCapacity {
        HasCapacity { recv: self }
    }

    fn poll(&mut self, cx: &mut Context) -> Poll<()> {
        let limit = self.inner.limit.load(Ordering::Acquire);
        loop {
            let active = self.inner.active.load(Ordering::Acquire);
            if active < limit {
                return Poll::Ready(());
            }
            match self.inner.task.try_lock() {
                Ok(mut guard) => {
                    *guard = Some(cx.waker().clone());
                    break;
                }
                Err(TryLockError::WouldBlock) => {
                    // This means either another token is currently waking
                    // up this receiver, retry
                    //
                    // Note: this looks like a busyloop, but we don't have
                    // anything long/slow behind the mutex. And it's only
                    // executed when limit is reached.
                    continue;
                }
                Err(TryLockError::Poisoned(_)) => {
                    unreachable!("backpressure lock should never be poisoned");
                }
            }
        }
        // Reread the limit after lock is unlocked because
        // token Drop relies on that
        let active = self.inner.active.load(Ordering::Acquire);
        if active < limit {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }
}

impl Drop for Token {
    fn drop(&mut self) {
        // TODO(tailhook) we could use Acquire for old_ref,
        // but not sure how safe is it to compare it with a limit
        let old_ref = self.inner.active.fetch_sub(1, Ordering::SeqCst);
        let limit = self.inner.limit.load(Ordering::SeqCst);
        if old_ref == limit {
            match self.inner.task.try_lock() {
                Ok(mut guard) => {
                    guard.take().map(|w| w.wake());
                }
                Err(TryLockError::WouldBlock) => {
                    // This means either another token is currently waking
                    // up a Receiver. Or Receiver is currently running.
                    // Receiver will recheck values after releasing the Mutex.
                }
                Err(TryLockError::Poisoned(_)) => {
                    unreachable!("backpressure lock should never be poisoned");
                }
            }
        }
    }
}

impl<S> BackpressureToken<S> {
    pub(crate) fn new(stream: S, backpressure: Receiver)
        -> BackpressureToken<S>
    {
        BackpressureToken(Backpressure::new(stream, backpressure))
    }

    /// Acquires a reference to the underlying stream that this combinator is
    /// pulling from.
    pub fn get_ref(&self) -> &S {
        self.0.get_ref()
    }

    /// Acquires a mutable reference to the underlying stream that this
    /// combinator is pulling from.
    pub fn get_mut(&mut self) -> &mut S {
        self.0.get_mut()
    }

    /// Acquires a pinned mutable reference to the underlying stream that this
    /// combinator is pulling from.
    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut S> {
        unsafe {
            self.map_unchecked_mut(|x| &mut x.0.stream)
        }
    }

    /// Consumes this combinator, returning the underlying stream.
    pub fn into_inner(self) -> S {
        self.0.into_inner()
    }
}

impl<S> BackpressureWrapper<S> {
    pub(crate) fn new(stream: S, backpressure: Receiver)
        -> BackpressureWrapper<S>
    {
        BackpressureWrapper(Backpressure::new(stream, backpressure))
    }

    /// Acquires a reference to the underlying stream that this combinator is
    /// pulling from.
    pub fn get_ref(&self) -> &S {
        self.0.get_ref()
    }

    /// Acquires a mutable reference to the underlying stream that this
    /// combinator is pulling from.
    pub fn get_mut(&mut self) -> &mut S {
        self.0.get_mut()
    }

    /// Acquires a pinned mutable reference to the underlying stream that this
    /// combinator is pulling from.
    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut S> {
        unsafe {
            self.map_unchecked_mut(|x| &mut x.0.stream)
        }
    }

    /// Consumes this combinator, returning the underlying stream.
    pub fn into_inner(self) -> S {
        self.0.into_inner()
    }
}

impl<S> Backpressure<S> {
    pub(crate) fn new(stream: S, backpressure: Receiver) -> Backpressure<S> {
        Backpressure { stream, backpressure }
    }

    /// Acquires a reference to the underlying stream that this combinator is
    /// pulling from.
    pub fn get_ref(&self) -> &S {
        &self.stream
    }

    /// Acquires a mutable reference to the underlying stream that this
    /// combinator is pulling from.
    pub fn get_mut(&mut self) -> &mut S {
        &mut self.stream
    }

    /// Acquires a pinned mutable reference to the underlying stream that this
    /// combinator is pulling from.
    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut S> {
        unsafe {
            self.map_unchecked_mut(|x| &mut x.stream)
        }
    }

    /// Consumes this combinator, returning the underlying stream.
    pub fn into_inner(self) -> S {
        self.stream
    }
}

/// Create a new pair of backpressure structures
///
/// These structures are called [`Sender`](struct.Sender.html)
/// and [`Receiver`](struct.Receiver.html) similar to channels.
/// The `Receiver` should be used to throttle, either by applying
/// it to a stream or using it directly. The `Sender` is a way to create
/// throtting tokens (the stream is paused when there are tokens >= limit),
/// and to change the limit.
///
/// See [`ListenExt`](../trait.ListenExt.html) for example usage
///
/// # Direct Use Example
///
/// ```no_run
/// # use std::time::Duration;
/// # use async_std::net::{TcpListener, TcpStream};
/// # use async_std::prelude::*;
/// # use async_std::task;
/// # fn main() -> std::io::Result<()> { task::block_on(async {
/// #
/// use async_listen::ListenExt;
/// use async_listen::backpressure;
///
/// let listener = TcpListener::bind("127.0.0.1:0").await?;
/// let (tx, mut rx) = backpressure::new(10);
/// let mut incoming = listener.incoming()
///     .handle_errors(Duration::from_millis(100));
///
/// loop {
///     rx.has_capacity().await;
///     let conn = match incoming.next().await {
///         Some(conn) => conn,
///         None => break,
///     };
///     let token = tx.token();  // should be created before spawn
///     task::spawn(async {
///         connection_loop(conn).await;
///         drop(token);  // should be dropped after
///     });
/// }
/// # async fn connection_loop(_stream: TcpStream) {
/// # }
/// #
/// # Ok(()) }) }
/// ```
///
pub fn new(initial_limit: usize) -> (Sender, Receiver) {
    let inner = Arc::new(Inner {
        limit: AtomicUsize::new(initial_limit),
        active: AtomicUsize::new(0),
        task: Mutex::new(None),
    });
    return (
        Sender {
            inner: inner.clone(),
        },
        Receiver {
            inner: inner.clone(),
        },
    )
}

impl fmt::Debug for Token {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        debug("Token", &self.inner, f)
    }
}

impl fmt::Debug for Sender {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        debug("Sender", &self.inner, f)
    }
}

impl fmt::Debug for Receiver {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        debug("Receiver", &self.inner, f)
    }
}

impl<'a> fmt::Debug for HasCapacity<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        debug("HasCapacity", &self.recv.inner, f)
    }
}

fn debug(name: &str, inner: &Arc<Inner>, f: &mut fmt::Formatter)
    -> fmt::Result
{
    let active = inner.active.load(Ordering::Relaxed);
    let limit = inner.limit.load(Ordering::Relaxed);
    write!(f, "<{} {}/{}>", name, active, limit)
}

impl<S: fmt::Debug> fmt::Debug for Backpressure<S> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Backpressure")
            .field("stream", &self.stream)
            .field("backpressure", &self.backpressure)
            .finish()
    }
}

impl<S: fmt::Debug> fmt::Debug for BackpressureToken<S> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("BackpressureToken")
            .field("stream", &self.0.stream)
            .field("backpressure", &self.0.backpressure)
            .finish()
    }
}

impl<S: fmt::Debug> fmt::Debug for BackpressureWrapper<S> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("BackpressureWrapper")
            .field("stream", &self.0.stream)
            .field("backpressure", &self.0.backpressure)
            .finish()
    }
}

impl<I, S> Stream for Backpressure<S>
    where S: Stream<Item=I> + Unpin
{
    type Item = I;
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context)
        -> Poll<Option<Self::Item>>
    {
        match self.backpressure.poll(cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(()) => self.as_mut().get_pin_mut().poll_next(cx),
        }
    }
}

impl<'a> Future for HasCapacity<'a> {
    type Output = ();
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> {
        self.recv.poll(cx)
    }
}

impl<I, S> Stream for BackpressureToken<S>
    where S: Stream<Item=I> + Unpin
{
    type Item = (Token, I);
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context)
        -> Poll<Option<Self::Item>>
    {
        unsafe { self.as_mut().map_unchecked_mut(|x| &mut x.0) }
        .poll_next(cx)
        .map(|opt| opt.map(|conn| (self.0.backpressure.token(), conn)))
    }
}

impl<I, S> Stream for BackpressureWrapper<S>
    where S: Stream<Item=I> + Unpin,
          ByteStream: From<(Token, I)>,
{
    type Item = ByteStream;
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context)
        -> Poll<Option<Self::Item>>
    {
        unsafe { self.as_mut().map_unchecked_mut(|x| &mut x.0) }
        .poll_next(cx)
        .map(|opt| opt.map(|conn| {
            ByteStream::from((self.0.backpressure.token(), conn))
        }))
    }
}