Skip to content

Support for Drain mode#157

Merged
wallyqs merged 3 commits into
masterfrom
drain-mode
Aug 31, 2018
Merged

Support for Drain mode#157
wallyqs merged 3 commits into
masterfrom
drain-mode

Conversation

@wallyqs
Copy link
Copy Markdown
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.

2 participants