nats-io / nats.rs

Rust client for NATS, the cloud native messaging system.
Apache License 2.0
1.04k stars 165 forks source link

Add Lazy Mode Support for Consumers #1322

Open ashupednekar opened 1 week ago

ashupednekar commented 1 week ago

Proposed change

Issue: Add Lazy Mode Support for Consumers

Feature Request

Add support for starting consumers in lazy mode to the NATS.rs client.

Description:

In this enhancement, we aim to introduce a feature that allows consumers to start listening to specific subjects in a lazy manner. The proposed implementation would allow consumers to listen on a pattern like some.pattern.*, but delay the creation of individual consumers until triggered by an internal event. This internal event could signal the base consumer to initiate a separate consumer for each relevant subject.

What is it

We have something called lazy mode for our consumers... what happens here is that the consumer process won't be subscribing to the subject pattern directly, instead it starts an internal consumer which listens for start event for every new unique subject and dynamically spawns a consumer thread subscribing to said subject.

This decision is made by the producer... before publishing the intended message, it checks with nats core if there is a consumer defined for said subject, and sends the start event accordingly.

Expected Outcome:

This feature would make the consumer system more dynamic and resource-efficient.

Use case

Contribution

A similar system has already been built on top of NATS.rs, and the functionality proved useful. Additionally, this feature will require a lazy producer counterpart to send the initial start event, which can only be achieved using JetStream, not the NATS core.

ashupednekar commented 1 week ago

I'd like to take up this feature if it aligns with the project's direction. I've built something similar before and believe this could be a valuable addition to NATS.rs. Let me know if there's interest, and I can start working on a proposal or implementation.

Jarema commented 13 hours ago

Hey.

Thanks for detailed explanation of the proposal.

However, I feel that this is very application-specific and use-case specific. Addtionally - Consumers are server side constructs, so it's not only about subscribing at given time, but also deciding when you want to create the on the server/cluster.

You should be able to achieve any of those goals with current client logic: