protobuf-nats 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c764e903778eb435b52043d155b769771bdda9ef
4
- data.tar.gz: 26791ed1c774ce5be5adf22cfe14fd52fd01dce6
3
+ metadata.gz: 28a6d2646c7aa30566ecf3042f164df413453b6f
4
+ data.tar.gz: e0f1fc04823c3da16ec32eb8c17f49ae5d8a73c5
5
5
  SHA512:
6
- metadata.gz: 760f358b74a5533d7b3fc9ed9f71fc235cee53a7b058c48e5ff880d33a944cfa5e96fba9169c3aae53b71ce25506bed88f96ca1e11b78ac3c68da39ba87a58a4
7
- data.tar.gz: 472b03996ed36835f54db33c5ee7d71119682a5d0fb436370c99a47ab60869ce14f225df4bfccb3c3144b863f174929a59f5e2ebbd60302e0084941eabf64f66
6
+ metadata.gz: 9fefcb0855bd3467316bcfff911585f09b57a93d93bd6eeca5fff07f15a64da5bf80939553a5650b33f2f2edf5654406c6790b251aa789ccef1a23f3b8d63571
7
+ data.tar.gz: c4bb2859a7082516118805e5232b89218c668a813b03b7d936b972fc91cd948eb67dee10d77759904f934a91325030b5f7979eb9e1453df83d2b4913d3a01cc4
data/README.md CHANGED
@@ -21,6 +21,22 @@ Or install it yourself as:
21
21
 
22
22
  ## Configuring
23
23
 
24
+ ### Environment Variables
25
+
26
+ You can also use the following environment variables to tune parameters:
27
+
28
+ `PB_NATS_SERVER_MAX_QUEUE_SIZE` - The size of the queue in front of your thread pool (default: thread count passed to CLI)
29
+
30
+ `PB_NATS_CLIENT_ACK_TIMEOUT` - Seconds to wait for an ACK from the rpc server (default: 5 seconds)
31
+
32
+ `PB_NATS_CLIENT_RESPONSE_TIMEOUT` - Seconds to wait for a non-ACK response from the rpc server (default: 60 seconds)
33
+
34
+ `PB_NATS_CLIENT_RECONNECT_DELAY` - If we detect a reconnect delay, we will wait this many seconds (default: the ACK timeout)
35
+
36
+ `PROTOBUF_NATS_CONFIG_PATH` - Custom path to the config yaml (default: "config/protobuf_nats.yml")
37
+
38
+ ### YAML Config
39
+
24
40
  The client and server are configured via environment variables defined in the `pure-ruby-nats` gem. However, there are a
25
41
  few params which cannot be set: `servers`, `uses_tls` and `connect_timeout`, so those my be defined in a yml file.
26
42
 
@@ -36,9 +52,11 @@ An example config looks like this:
36
52
  - "nats://127.0.0.1:4222"
37
53
  - "nats://127.0.0.1:4223"
38
54
  - "nats://127.0.0.1:4224"
55
+ max_reconnect_attempts: 500
39
56
  uses_tls: true
40
57
  tls_client_cert: "/path/to/client-cert.pem"
41
58
  tls_client_key: "/path/to/client-key.pem"
59
+ tls_ca_cert: "/path/to/ca.pem"
42
60
  connect_timeout: 2
43
61
  ```
44
62
 
Binary file
@@ -40,15 +40,48 @@ module Protobuf
40
40
  end
41
41
  end
42
42
 
43
+ def ack_timeout
44
+ @ack_timeout ||= if ::ENV.key?("PB_NATS_CLIENT_ACK_TIMEOUT")
45
+ ::ENV["PB_NATS_CLIENT_ACK_TIMEOUT"].to_i
46
+ else
47
+ 5
48
+ end
49
+ end
50
+
51
+ def reconnect_delay
52
+ @reconnect_delay ||= if ::ENV.key?("PB_NATS_CLIENT_RECONNECT_DELAY")
53
+ ::ENV["PB_NATS_CLIENT_RECONNECT_DELAY"].to_i
54
+ else
55
+ ack_timeout
56
+ end
57
+ end
58
+
59
+ def response_timeout
60
+ @response_timeout ||= if ::ENV.key?("PB_NATS_CLIENT_RESPONSE_TIMEOUT")
61
+ ::ENV["PB_NATS_CLIENT_RESPONSE_TIMEOUT"].to_i
62
+ else
63
+ 60
64
+ end
65
+ end
66
+
43
67
  def send_request
44
68
  failure_handler do
45
69
  begin
46
70
  retries ||= 3
47
71
 
48
72
  setup_connection
49
- request_options = {:timeout => 60, :ack_timeout => 5}
73
+ request_options = {:timeout => response_timeout, :ack_timeout => ack_timeout}
50
74
  @response_data = nats_request_with_two_responses(cached_subscription_key, @request_data, request_options)
51
75
  parse_response
76
+ rescue ::Protobuf::Nats::Errors::IOException => error
77
+ ::Protobuf::Nats.log_error(error)
78
+
79
+ delay = reconnect_delay
80
+ logger.warn "An IOException was raised. We are going to sleep for #{delay} seconds."
81
+ sleep delay
82
+
83
+ retry if (retries -= 1) > 0
84
+ raise
52
85
  rescue ::NATS::IO::Timeout
53
86
  # Nats response timeout.
54
87
  retry if (retries -= 1) > 0
@@ -112,8 +145,6 @@ module Protobuf
112
145
  response
113
146
  ensure
114
147
  # Ensure we don't leave a subscriptiosn sitting around.
115
- # This also cleans up memory. It's a no-op if the subscription
116
- # is already cleaned up.
117
148
  nats.unsubscribe(sub)
118
149
  end
119
150
 
@@ -4,12 +4,13 @@ require "yaml"
4
4
  module Protobuf
5
5
  module Nats
6
6
  class Config
7
- attr_accessor :uses_tls, :servers, :connect_timeout, :tls_client_cert, :tls_client_key, :tls_ca_cert
7
+ attr_accessor :uses_tls, :servers, :connect_timeout, :tls_client_cert, :tls_client_key, :tls_ca_cert, :max_reconnect_attempts
8
8
 
9
9
  CONFIG_MUTEX = ::Mutex.new
10
10
 
11
11
  DEFAULTS = {
12
12
  :connect_timeout => nil,
13
+ :max_reconnect_attempts => 60_000,
13
14
  :servers => nil,
14
15
  :tls_client_cert => nil,
15
16
  :tls_client_key => nil,
@@ -54,6 +55,7 @@ module Protobuf
54
55
  @connection_options ||= begin
55
56
  options = {
56
57
  servers: servers,
58
+ max_reconnect_attempts: max_reconnect_attempts,
57
59
  uses_tls: uses_tls,
58
60
  tls_client_cert: tls_client_cert,
59
61
  tls_client_key: tls_client_key,
@@ -0,0 +1,14 @@
1
+ module Protobuf
2
+ module Nats
3
+ module Errors
4
+ class MriIOException < ::StandardError
5
+ end
6
+
7
+ IOException = if defined? JRUBY_VERSION
8
+ java.io.IOException
9
+ else
10
+ MriIOException
11
+ end
12
+ end
13
+ end
14
+ end
@@ -39,8 +39,12 @@ module Protobuf
39
39
  servers = [servers].flatten.map { |uri_string| java.net.URI.new(uri_string) }
40
40
  connection_factory = ::Java::IoNatsClient::ConnectionFactory.new
41
41
  connection_factory.setServers(servers)
42
- # Basically never stop trying to connect
43
- connection_factory.setMaxReconnect(60_000)
42
+ connection_factory.setMaxReconnect(options[:max_reconnect_attempts])
43
+
44
+ # Shrink the pending buffer to always raise an error and let the caller retry.
45
+ if options[:disable_reconnect_buffer]
46
+ connection_factory.setReconnectBufSize(1)
47
+ end
44
48
 
45
49
  # Setup callbacks
46
50
  connection_factory.setDisconnectedCallback { |event| @on_disconnect_cb.call }
@@ -16,14 +16,22 @@ module Protobuf
16
16
  @running = true
17
17
  @stopped = false
18
18
 
19
- @nats = options[:client] || ::Protobuf::Nats::NatsClient.new
19
+ @nats = @options[:client] || ::Protobuf::Nats::NatsClient.new
20
20
  @nats.connect(::Protobuf::Nats.config.connection_options)
21
21
 
22
- @thread_pool = ::Protobuf::Nats::ThreadPool.new(options[:threads], :max_queue => options[:threads])
22
+ @thread_pool = ::Protobuf::Nats::ThreadPool.new(@options[:threads], :max_queue => max_queue_size)
23
23
 
24
24
  @subscriptions = []
25
25
  end
26
26
 
27
+ def max_queue_size
28
+ if ::ENV.key?("PB_NATS_SERVER_MAX_QUEUE_SIZE")
29
+ ::ENV["PB_NATS_SERVER_MAX_QUEUE_SIZE"].to_i
30
+ else
31
+ @options[:threads]
32
+ end
33
+ end
34
+
27
35
  def service_klasses
28
36
  ::Protobuf::Rpc::Service.implemented_services.map(&:safe_constantize)
29
37
  end
@@ -68,11 +76,11 @@ module Protobuf
68
76
 
69
77
  def run
70
78
  nats.on_reconnect do
71
- logger.warn "Reconnected to NATS server!"
79
+ logger.warn "Server NATS connection was reconnected"
72
80
  end
73
81
 
74
82
  nats.on_disconnect do
75
- logger.warn "Disconnected from NATS server!"
83
+ logger.warn "Server NATS connection was disconnected"
76
84
  end
77
85
 
78
86
  nats.on_error do |error|
@@ -80,7 +88,7 @@ module Protobuf
80
88
  end
81
89
 
82
90
  nats.on_close do
83
- logger.warn "NATS connection was closed!"
91
+ logger.warn "Server NATS connection was closed"
84
92
  end
85
93
 
86
94
  subscribe_to_services
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module Nats
3
- VERSION = "0.2.3"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
data/lib/protobuf/nats.rb CHANGED
@@ -6,6 +6,7 @@ require "protobuf/rpc/service_directory"
6
6
 
7
7
  require "nats/io/client"
8
8
 
9
+ require "protobuf/nats/errors"
9
10
  require "protobuf/nats/client"
10
11
  require "protobuf/nats/server"
11
12
  require "protobuf/nats/runner"
@@ -53,12 +54,32 @@ module Protobuf
53
54
  break true if @client_nats_connection
54
55
  break true if @start_client_nats_connection
55
56
 
56
- @client_nats_connection = NatsClient.new
57
- @client_nats_connection.connect(config.connection_options)
57
+ # Disable publisher pending buffer on reconnect
58
+ options = config.connection_options.merge(:disable_reconnect_buffer => true)
59
+
60
+ begin
61
+ @client_nats_connection = NatsClient.new
62
+ @client_nats_connection.connect(options)
63
+ rescue ::Protobuf::Nats::Errors::IOException
64
+ @client_nats_connection = nil
65
+ raise
66
+ end
58
67
 
59
68
  # Ensure we have a valid connection to the NATS server.
60
69
  @client_nats_connection.flush(5)
61
70
 
71
+ @client_nats_connection.on_disconnect do
72
+ logger.warn("Client NATS connection was disconnected")
73
+ end
74
+
75
+ @client_nats_connection.on_reconnect do
76
+ logger.warn("Client NATS connection was reconnected")
77
+ end
78
+
79
+ @client_nats_connection.on_close do
80
+ logger.warn("Client NATS connection was closed")
81
+ end
82
+
62
83
  @client_nats_connection.on_error do |error|
63
84
  log_error(error)
64
85
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf-nats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Dewitt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-14 00:00:00.000000000 Z
11
+ date: 2017-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: protobuf
@@ -137,6 +137,7 @@ files:
137
137
  - lib/protobuf/nats.rb
138
138
  - lib/protobuf/nats/client.rb
139
139
  - lib/protobuf/nats/config.rb
140
+ - lib/protobuf/nats/errors.rb
140
141
  - lib/protobuf/nats/jnats.rb
141
142
  - lib/protobuf/nats/runner.rb
142
143
  - lib/protobuf/nats/server.rb