laststage

Crates.iolaststage
lib.rslaststage
version1.0.0
sourcesrc
created_at2022-04-22 11:40:09.173676
updated_at2022-04-22 11:49:32.629607
descriptionfast, durable, high concurrent HashMap
homepagehttps://github.com/Rustixir/last_stage
repositoryhttps://github.com/Rustixir/last_stage
max_upload_size
id572120
size30,182
DanyalMh (Rustixir)

documentation

README

LastStage

LastStage is a specification for exchanging events between producers and consumers. with push-based model

This project currently provides :

  • Producer produce events and dispatch to subscribers by Dispatcher (no input - one/many output)

  • ProducerConsumer it get events from upstream and dispatch to subscribers by Dispatcher (one/many input - one/many output)

  • Consumer it get events from upstream and consume it (one/many input - no putput)

  • Dispatcher first get one-many subscriber then start to dispatch events by two mode (Broadcast / RoundRobin)

Examples

Examples directory:

Installation

laststage = "1.0.0"

Quick example


#[tokio::main]
async fn main() {

    let(shutdown_sender, shutdown_recv) = channel();


    // -----------------------------------
    //
    // Producer -> ProducerConsumer -> Consumer 
    //
    // ------------------------------------



    // Run Consumer
    let log_chan = ConsumerRunnable::new(Box::new(Log)).run(100);


    // Run ProducerConsumer
    let filter_chan = ProducerConsumerRunnable::new(Box::new(FilterByAge), 
                                                    vec![log_chan], 
                                                    Some(DispatcherType::RoundRobin)
                                                    ).unwrap().run(100);

    // Run Producer
    let _ = ProducerRunnable::new(Box::new(Prod), 
                                  vec![filter_chan], 
                                  None, 
                                  100, 
                                  shutdown_recv).unwrap().run();

    
    tokio::time::sleep(Duration::from_secs(10)).await;
}


#[derive(Clone)]
struct ProdEvent {
    pub funame: String,
    pub age: i32
}

struct Prod;

#[async_trait]
impl Producer<ProdEvent> for Prod {
    async fn init(&mut self) {

    }

    async fn handle_demand(&mut self, max_demand: usize) -> Vec<ProdEvent> {
        (0..max_demand as i32)
            .into_iter()
            .map(|i| {
                
                ProdEvent { 
                    funame: format!("DanyalMh-{}", i), 
                    age: (i + 30) % 35 
                }

            })
            .collect()
    }

    async fn terminate(&mut self) {

    }
} 


// -------------------------------------------


struct FilterByAge;

#[async_trait]
impl ProducerConsumer<ProdEvent, ProdEvent> for FilterByAge {
    async fn init(&mut self) {
        
    }

    async fn handle_events(&mut self, events: Vec<ProdEvent>) -> Vec<ProdEvent> {
        events
            .into_iter()
            .filter(|pe| pe.age > 25 && pe.age < 32)
            .collect()
    }

    async fn terminate(&mut self) {

    }


} 



struct Log;

#[async_trait]
impl Consumer<ProdEvent> for Log {
    async fn init(&mut self) {

    }

    async fn handle_events(&mut self, events: Vec<ProdEvent>) -> State<ProdEvent> {
        events
            .into_iter()
            .for_each(|pe| {
                println!("==> {} -> {}", pe.funame, pe.age)
            });
        
        State::Continue
    }  


    async fn terminate(&mut self) {
        
    }
}


Commit count: 15

cargo fmt