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

Support for Drain mode #157

Merged
merged 3 commits into from
Aug 31, 2018
Merged

Support for Drain mode #157

merged 3 commits into from
Aug 31, 2018

Conversation

wallyqs
Copy link
Member

@wallyqs wallyqs commented Aug 29, 2018

Adds new drain API which a client can use to close a connection gracefully, allowing subscribers to process pending messages that have already been delivered by the server, done by announcing first to the server that no longer interested in receiving messages in all the subscriptions.

Unlike the Go client, it is only possible to drain the full connection since current implementation relies on a single buffer for all the subscriptions.

NATS.start(drain_timeout: 1) do |nc|
  NATS.subscribe('foo', queue: "workers") do |msg, reply, sub|
    nc.publish(reply, "ACK:#{msg}")
  end

  NATS.subscribe('bar', queue: "workers") do |msg, reply, sub|
    nc.publish(reply, "ACK:#{msg}")
  end

  NATS.subscribe('quux', queue: "workers") do |msg, reply, sub|
    nc.publish(reply, "ACK:#{msg}")
  end

  EM.add_timer(2) do
    next if NATS.draining?

    # Drain gracefully closes the connection.
    NATS.drain do
      puts "Done draining. Connection is closed."
    end
  end
end

Signed-off-by: Waldemar Quevedo <wally@synadia.com>
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
Also disallow publishing once subs have been drained

Signed-off-by: Waldemar Quevedo <wally@synadia.com>
@wallyqs wallyqs merged commit 5d89552 into master Aug 31, 2018
@wallyqs wallyqs deleted the drain-mode branch August 31, 2018 02:16
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 this pull request may close these issues.

None yet

2 participants