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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use async_trait::async_trait;
use futures::{
    channel::mpsc::{UnboundedReceiver, UnboundedSender},
    FutureExt, SinkExt, StreamExt,
};
use std::hint;

pub enum Instruction {
    Ok,
    Clear,
}

pub struct ResponseWithInstruction<T> {
    pub resp: T,
    pub instruction: Instruction,
}

impl<T> From<T> for ResponseWithInstruction<T> {
    fn from(val: T) -> Self {
        Self {
            resp: val,
            instruction: Instruction::Ok,
        }
    }
}

#[async_trait]
pub trait StatelessPipeline: Send + Sync {
    type Request;
    type Response;
    async fn process(&self, req: Self::Request) -> ResponseWithInstruction<Self::Response>;
}

pub struct PipelinePhase<T: StatelessPipeline> {
    rx: UnboundedReceiver<T::Request>,
    tx: UnboundedSender<T::Response>,
    processor: Box<T>,
}

impl<T: StatelessPipeline> PipelinePhase<T> {
    pub fn new(
        rx: UnboundedReceiver<T::Request>,
        tx: UnboundedSender<T::Response>,
        processor: Box<T>,
    ) -> Self {
        Self { rx, tx, processor }
    }

    pub fn exhaust_requests_non_blocking(&mut self) {
        while self.rx.next().now_or_never().is_some() {
            hint::spin_loop()
        }
    }

    pub async fn start(mut self) {
        // main loop
        while let Some(req) = self.rx.next().await {
            let ResponseWithInstruction { resp, instruction } = self.processor.process(req).await;
            match instruction {
                Instruction::Ok => {}
                Instruction::Clear => self.exhaust_requests_non_blocking(),
            }
            if self.tx.send(resp).await.is_err() {
                break;
            }
        }
    }
}