Skip to content

Commit

Permalink
Merge pull request #160 from nats-io/release/v0.10.0
Browse files Browse the repository at this point in the history
Prepare for Release/v0.10.0
  • Loading branch information
wallyqs authored Aug 31, 2018
2 parents 32672dc + 50dfb31 commit 77d8a8e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 9 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
A [Ruby](http://ruby-lang.org) client for the [NATS messaging system](https://nats.io).

[![License Apache 2.0](https://img.shields.io/badge/License-Apache2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
[![Build Status](https://travis-ci.org/nats-io/ruby-nats.svg)](http://travis-ci.org/nats-io/ruby-nats) [![Gem Version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=rb&type=5&v=0.9.2)](https://rubygems.org/gems/nats/versions/0.9.2) [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/gems/nats)
[![Build Status](https://travis-ci.org/nats-io/ruby-nats.svg)](http://travis-ci.org/nats-io/ruby-nats) [![Gem Version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=rb&type=5&v=0.10.0)](https://rubygems.org/gems/nats/versions/0.10.0) [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/gems/nats)

## Supported Platforms

This gem and the client are known to work on the following Ruby platforms:

- MRI 2.2, 2.3.0, 2.4.0, 2.5.0
- JRuby 9.1.2.0, 9.1.15.0
- MRI 2.3.0, 2.4.0, 2.5.0
- JRuby 9.1.2.0, 9.1.15.0, 9.2.0.0

If you're looking for a non-EventMachine alternative, check out the [nats-pure](https://github.com/nats-io/pure-ruby-nats) gem.

Expand Down Expand Up @@ -108,11 +108,10 @@ end

### Auto discovery

Starting from release `0.8.0` of the gem, the client also auto
discovers new nodes announced by the server as they attach to the
cluster. Reconnection logic parameters such as time to back-off on
failure and max attempts apply the same to both discovered nodes and
those defined explicitly on connect:
The client also auto discovers new nodes announced by the server as
they attach to the cluster. Reconnection logic parameters such as
time to back-off on failure and max attempts apply the same to both
discovered nodes and those defined explicitly on connect:

```ruby
opts = {
Expand Down Expand Up @@ -149,7 +148,10 @@ NATS.unsubscribe(sid, MAX_WANTED)
# Multiple connections
NATS.subscribe('test') do |msg|
puts "received msg"
NATS.stop

# Gracefully disconnect from NATS after handling
# messages that have already been delivered by server.
NATS.drain
end

# Form second connection to send message on
Expand Down
81 changes: 81 additions & 0 deletions examples/drain_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'nats/client'

nc1 = nil
nc2 = nil
responses = []
inbox = NATS.create_inbox
["TERM", "INT"].each { |sig| trap(sig) {
EM.stop
}
}

subscribers = []
EM.run do
5.times do |n|
subscribers << NATS.connect(drain_timeout: 30, name: "client-#{n}") do |nc|
nc.on_error { |err| puts "#{Time.now} - Error: #{err}" }
nc.on_close { |err| puts "#{Time.now} - Connection drained and closed!" }
puts "#{Time.now} - Started Connection #{n}..."

nc.flush do
nc.subscribe('foo', queue: "workers") do |msg, reply, sub|
nc.publish(reply, "ACK1:#{msg}")
end

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

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

pub_client = NATS.connect do |nc|
EM.add_periodic_timer(0.001) do
Fiber.new do
response = nc.request("foo", "A")
puts "Dropped request!!!" if response.nil?
end.resume
end

EM.add_periodic_timer(0.001) do
Fiber.new do
response = nc.request("bar", "B")
puts "Dropped request!!!" if response.nil?
# puts "Response on 'bar' : #{response}"
end.resume
end

EM.add_periodic_timer(0.001) do
Fiber.new do
response = nc.request("quux", "C")
puts "Dropped request!!!" if response.nil?
end.resume
end
end

EM.add_timer(1) do
# Drain is like stop but gracefully closes the connection.
subs = subscribers[0..3]

subs.each_with_index do |nc, i|
if nc.draining?
puts "Already draining... #{responses.count}"
next
end

# Just using close will cause some requests to fail
# nc.close

# Drain is more graceful and allow clients to process requests
# that have already been delivered by the server to the subscriber.
puts "#{Time.now} - Start draining #{nc.options[:name]}... (pending_data: #{nc.pending_data_size})"
nc.drain do
puts "#{Time.now} - Done draining #{nc.options[:name]}!"
end
end
end
end

0 comments on commit 77d8a8e

Please sign in to comment.