gftea / amqprs

Async & Lock-free RabbitMQ Rust Client, Easy-to-use API
MIT License
214 stars 27 forks source link

Best practices for opening channel, and prevent it from being dropped. #99

Closed DavidSantacruzR closed 1 year ago

DavidSantacruzR commented 1 year ago

I'm building a wrapper to your library, and recently came across a problem to prevent a channel connection from being dropped. So, the question is, should each queue have their own channel or is it better to have one channel, and bind multiple queues to it?

Which one is considered bad practice?

For example:

Approach: one

struct MyConsumer {
        connection: Connection
        channels: Vec<Channel>   
}

impl MyConsumer {

    fn create_channel(&mut self, queue: String) -> Channel {
           /* logic to create, and bind a channel goes here, pushes to vector */

    }

}

Approach: two

struct MyConsumer {
        connection: Connection
        channel: Channel  
}

impl MyConsumer {

    fn create_channel(&mut self, queue: String) {
           /* creates queue and binds it to one channel*/

    }

}

My problem with my current approach is that when binding a queue to a channel, I'm unable to prevent the channel from being dropped.

Any advice is much appreciated.

gftea commented 1 year ago

see guideline provided by rabbit community https://github.com/gftea/amqprs/discussions/78#discussioncomment-5524344

michaelklishin commented 1 year ago

Besides the guides, I don't see any issue with treating channels as resources (as if it was a file or socket) and making your consumer implementation own it. Then you will have to make sure the consumer instance lives long enough.

However, you can also initialize the connection(s), channel(s) and connection recovery code in the main function or similar code path in your app, and make consumers borrow the resources they need.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] commented 1 year ago

This issue was closed because it has been stalled for 5 days with no activity.