Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-unsubscribe options #67

Closed
4 tasks
mtmk opened this issue Jun 28, 2023 · 2 comments · Fixed by #70
Closed
4 tasks

Auto-unsubscribe options #67

mtmk opened this issue Jun 28, 2023 · 2 comments · Fixed by #70
Assignees

Comments

@mtmk
Copy link
Collaborator

mtmk commented Jun 28, 2023

Users should be able to automatically unsubscribe:

  • After a certain number of messages (max-msgs)
  • After a certain time period (timeout)
  • Time period after the previous message was received (idle timeout)
  • Also, unsubscribe explicitly (before Dispose)

First scenario (max-msgs) can be implemented using UNSUB max_msgs:

  • SUB then immediately UNSUB with max_msgs specified
  • Keep a message count for subscription
  • When count == max_msgs signal user code (e.g. end iterators) and remove subscription
  • When reconnecting UNSUB with max_msgs - count

Timeouts would complete iterators and unsubscribe.

If combination of the options is used then whichever happens first wins and cancel others.

  • add Unsubscribe() method on NatsSub
  • add MaxMsgs on SubOpts
  • add Timeout on SubOpts
  • add IdleTimeout on SubOpts
@mtmk mtmk self-assigned this Jun 28, 2023
@Jarema
Copy link
Member

Jarema commented Jun 28, 2023

In Rust and Go clients, there are those methods:

Rust: sub.unsubscribe_after(max: u64) -> Result<(), UnsubscribeError>
Go: func (s *Subscription) AutoUnsubscribe(max int) error

So both are called on active subscription.
Both are also counting number of messages since the start of the subscription, and when max is already reched when setting UNSUB, it will immediately close the subscription.

If not, it will check the max internally at every message, and close subscription when counter reaches max.

For re-subscribing, you can still maintain the subscription state on the client side, and just resubscribe with the server.

@mtmk
Copy link
Collaborator Author

mtmk commented Jun 28, 2023

Currently we don't have an explicit unsubscribe method, it's done during subscription dispose. More natural way might be to add maxMsgs field to subscription options in our case.

@mtmk mtmk changed the title UNSUB max_msgs support Auto-unsubscribe options Jun 28, 2023
@mtmk mtmk linked a pull request Jun 30, 2023 that will close this issue
@mtmk mtmk closed this as completed in #70 Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants