Trait AsyncConsumer

Source
pub trait AsyncConsumer {
    // Required method
    fn consume<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        channel: &'life1 Channel,
        deliver: Deliver,
        basic_properties: BasicProperties,
        content: Vec<u8>,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
}
Expand description

Trait defines the callback interfaces for consuming asynchronous content data from server.

Continously consume the content data until the consumer is cancelled or channel is closed.

Required Methods§

Source

fn consume<'life0, 'life1, 'async_trait>( &'life0 mut self, channel: &'life1 Channel, deliver: Deliver, basic_properties: BasicProperties, content: Vec<u8>, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Consume a delivery from Server.

Every delivery combines a Deliver frame, message propertities, and content body.

§Inputs

channel: consumer’s channel reference, typically used for acknowledge the delivery.

deliver: see basic.deliver or delivery metadata

basic_properties: see message properties.

content: the content body

§Non-blocking and blocking consumer

This method is invoked in a async task context, so its implementation should NOT be CPU bound, otherwise it will starving the async runtime.

For CPU bound task (blocking consumer), possible solution below

  1. User can spawn a blocking task using tokio::spawn_blocking for CPU bound job, and use tokio’s mpsc channel to cummunicate between sync and async code. If too many blocking tasks, user can create a thread pool shared by all blocking tasks, and this method is only to forward message to corresponding blocking task. Also check bridging async and blocking code.

  2. Create blocking consumer by implementing trait BlockingConsumer, and use Channel::basic_consume_blocking to start consuming message in a blocking context.

Implementors§