protobuf-nats 0.9.0.pre2 → 0.9.0.pre3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7b7a2ede49bfda45aadc50f2cc978e9ac6f4780
4
- data.tar.gz: a6a6253219abc6417bcf87079da8d61694dfe941
3
+ metadata.gz: 22955c5ba9df8f9ad52ea33c197fd740cdfe8434
4
+ data.tar.gz: ff0dc5ef47d11178b7f8960030f025a389f2024c
5
5
  SHA512:
6
- metadata.gz: 3a6d7bc54084e79bf44ab56e1847823b37523eca4be6587509cf2a22682bdffd86ea25a574acac65a2e7e0dd92e04a1a173e0230a0b3ca02da1bad02db107400
7
- data.tar.gz: a25d3136a2c8767f13fc163f298f905d69f19970db022ccdee4ee8baffaea402a54448770a822fa5b58218936f6e3e84406a8fc316aa0fc6360e17932be9c3eb
6
+ metadata.gz: 9e75e969b3ce1561a4d402931147e5a8b4e52d09b47e36c3809694d3069b4f47c2d304771b5dd3167c44ab44e283af9000458aec6ddf6f018525f5b24dbeb70e
7
+ data.tar.gz: 340adf7236b84ec9f1d9a519a03d64ef0e31dcacbbecdd5bfa856db078608c70bd4571b4a02569095a3243933bccf76ab8025e11519d5dc4f61e795401f750af
data/README.md CHANGED
@@ -76,6 +76,12 @@ An example config looks like this:
76
76
  tls_client_key: "/path/to/client-key.pem"
77
77
  tls_ca_cert: "/path/to/ca.pem"
78
78
  connect_timeout: 2
79
+ server_subscription_key_only_subscribe_to_when_includes_any_of:
80
+ - "search"
81
+ - "create"
82
+ server_subscription_key_do_not_subscribe_to_when_includes_any_of:
83
+ - "old_search"
84
+ - "old_create"
79
85
  subscription_key_replacements:
80
86
  - "original_service": "replacement_service"
81
87
  ```
@@ -126,6 +126,9 @@ module Protobuf
126
126
  end
127
127
 
128
128
  def send_request
129
+ # This will ensure the client is started.
130
+ ::Protobuf::Nats.start_client_nats_connection
131
+
129
132
  if use_subscription_pooling?
130
133
  available = self.class.subscription_pool.instance_variable_get("@available")
131
134
  ::ActiveSupport::Notifications.instrument "client.subscription_pool_available_size.protobuf-nats", available.length
@@ -4,7 +4,10 @@ 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, :max_reconnect_attempts, :subscription_key_replacements
7
+ attr_accessor :uses_tls, :servers, :connect_timeout, :tls_client_cert, :tls_client_key, :tls_ca_cert, :max_reconnect_attempts
8
+ attr_accessor :server_subscription_key_do_not_subscribe_to_when_includes_any_of,
9
+ :server_subscription_key_only_subscribe_to_when_includes_any_of,
10
+ :subscription_key_replacements
8
11
 
9
12
  CONFIG_MUTEX = ::Mutex.new
10
13
 
@@ -16,6 +19,8 @@ module Protobuf
16
19
  :tls_client_key => nil,
17
20
  :tls_ca_cert => nil,
18
21
  :uses_tls => false,
22
+ :server_subscription_key_do_not_subscribe_to_when_includes_any_of => [],
23
+ :server_subscription_key_only_subscribe_to_when_includes_any_of => [],
19
24
  :subscription_key_replacements => [],
20
25
  }.freeze
21
26
 
@@ -62,6 +67,8 @@ module Protobuf
62
67
  tls_client_key: tls_client_key,
63
68
  tls_ca_cert: tls_ca_cert,
64
69
  connect_timeout: connect_timeout,
70
+ server_subscription_key_do_not_subscribe_to_when_includes_any_of: server_subscription_key_do_not_subscribe_to_when_includes_any_of,
71
+ server_subscription_key_only_subscribe_to_when_includes_any_of: server_subscription_key_only_subscribe_to_when_includes_any_of,
65
72
  subscription_key_replacements: subscription_key_replacements,
66
73
  }
67
74
  options[:tls] = {:context => new_tls_context} if uses_tls
@@ -13,7 +13,7 @@ end
13
13
  module Protobuf
14
14
  module Nats
15
15
  class JNats
16
- attr_reader :connection
16
+ attr_reader :connection, :options
17
17
 
18
18
  class Message
19
19
  attr_reader :data, :subject, :reply
@@ -30,11 +30,14 @@ module Protobuf
30
30
  @on_reconnect_cb = lambda {}
31
31
  @on_disconnect_cb = lambda {}
32
32
  @on_close_cb = lambda {}
33
+ @options = nil
33
34
  @subz_cbs = {}
34
35
  @subz_mutex = ::Mutex.new
35
36
  end
36
37
 
37
38
  def connect(options = {})
39
+ @options ||= options
40
+
38
41
  servers = options[:servers] || ["nats://localhost:4222"]
39
42
  servers = [servers].flatten.map { |uri_string| java.net.URI.new(uri_string) }
40
43
  connection_factory = ::Java::IoNatsClient::ConnectionFactory.new
@@ -68,9 +71,16 @@ module Protobuf
68
71
  @connection
69
72
  end
70
73
 
71
- # Do not depend on #close for a greaceful disconnect.
74
+ def connection
75
+ return @connection unless @connection.nil?
76
+ # Ensure no consumer or supervisor are running
77
+ close
78
+ connect(options || {})
79
+ end
80
+
81
+ # Do not depend on #close for a graceful disconnect.
72
82
  def close
73
- @connection.close
83
+ @connection.close rescue nil
74
84
  @connection = nil
75
85
  @supervisor.kill rescue nil
76
86
  @supervisor = nil
@@ -79,7 +89,7 @@ module Protobuf
79
89
  end
80
90
 
81
91
  def flush(timeout_sec = 0.5)
82
- @connection.flush(timeout_sec * 1000)
92
+ connection.flush(timeout_sec * 1000)
83
93
  end
84
94
 
85
95
  def next_message(sub, timeout_sec)
@@ -90,7 +100,7 @@ module Protobuf
90
100
 
91
101
  def publish(subject, data, mailbox = nil)
92
102
  # The "true" here is to force flush. May not need this.
93
- @connection.publish(subject, mailbox, data.to_java_bytes, true)
103
+ connection.publish(subject, mailbox, data.to_java_bytes, true)
94
104
  end
95
105
 
96
106
  def subscribe(subject, options = {}, &block)
@@ -100,8 +110,8 @@ module Protobuf
100
110
  # We pass our work queue for processing async work because java nats
101
111
  # uses a cahced thread pool: 1 thread per async subscription.
102
112
  # Sync subs need their own queue so work is not processed async.
103
- work_queue = block.nil? ? @connection.createMsgChannel : @work_queue
104
- sub = @connection.subscribe(subject, queue, nil, work_queue)
113
+ work_queue = block.nil? ? connection.createMsgChannel : @work_queue
114
+ sub = connection.subscribe(subject, queue, nil, work_queue)
105
115
 
106
116
  # Register the block callback. We only lock to save the callback.
107
117
  if block
@@ -80,6 +80,24 @@ module Protobuf
80
80
  was_enqueued
81
81
  end
82
82
 
83
+ def do_not_subscribe_to_includes?(subscription_key)
84
+ return false unless ::Protobuf::Nats.config.server_subscription_key_do_not_subscribe_to_when_includes_any_of.respond_to?(:any?)
85
+ return false if ::Protobuf::Nats.config.server_subscription_key_do_not_subscribe_to_when_includes_any_of.empty?
86
+
87
+ ::Protobuf::Nats.config.server_subscription_key_do_not_subscribe_to_when_includes_any_of.any? do |key|
88
+ subscription_key.include?(key)
89
+ end
90
+ end
91
+
92
+ def only_subscribe_to_includes?(subscription_key)
93
+ return true unless ::Protobuf::Nats.config.server_subscription_key_only_subscribe_to_when_includes_any_of.respond_to?(:any?)
94
+ return true if ::Protobuf::Nats.config.server_subscription_key_only_subscribe_to_when_includes_any_of.empty?
95
+
96
+ ::Protobuf::Nats.config.server_subscription_key_only_subscribe_to_when_includes_any_of.any? do |key|
97
+ subscription_key.include?(key)
98
+ end
99
+ end
100
+
83
101
  def pause_file_path
84
102
  ::ENV.fetch("PB_NATS_SERVER_PAUSE_FILE_PATH", nil)
85
103
  end
@@ -108,9 +126,12 @@ module Protobuf
108
126
  service_klasses.each do |service_klass|
109
127
  service_klass.rpcs.each do |service_method, _|
110
128
  # Skip services that are not implemented.
111
- next unless service_klass.method_defined? service_method
129
+ next unless service_klass.method_defined?(service_method)
130
+ subscription_key = ::Protobuf::Nats.subscription_key(service_klass, service_method)
131
+ next if do_not_subscribe_to_includes?(subscription_key)
132
+ next unless only_subscribe_to_includes?(subscription_key)
112
133
 
113
- yield ::Protobuf::Nats.subscription_key(service_klass, service_method)
134
+ yield subscription_key
114
135
  end
115
136
  end
116
137
  end
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module Nats
3
- VERSION = "0.9.0.pre2"
3
+ VERSION = "0.9.0.pre3"
4
4
  end
5
5
  end
data/lib/protobuf/nats.rb CHANGED
@@ -78,43 +78,40 @@ module Protobuf
78
78
  end
79
79
 
80
80
  def self.start_client_nats_connection
81
- @start_client_nats_connection ||= begin
82
- GET_CONNECTED_MUTEX.synchronize do
83
- break true if @client_nats_connection
84
- break true if @start_client_nats_connection
85
-
86
- # Disable publisher pending buffer on reconnect
87
- options = config.connection_options.merge(:disable_reconnect_buffer => true)
88
-
89
- begin
90
- @client_nats_connection = NatsClient.new
91
- @client_nats_connection.connect(options)
92
- rescue ::Protobuf::Nats::Errors::IOException
93
- @client_nats_connection = nil
94
- raise
95
- end
96
-
97
- # Ensure we have a valid connection to the NATS server.
98
- @client_nats_connection.flush(5)
99
-
100
- @client_nats_connection.on_disconnect do
101
- logger.warn("Client NATS connection was disconnected")
102
- end
103
-
104
- @client_nats_connection.on_reconnect do
105
- logger.warn("Client NATS connection was reconnected")
106
- end
107
-
108
- @client_nats_connection.on_close do
109
- logger.warn("Client NATS connection was closed")
110
- end
111
-
112
- @client_nats_connection.on_error do |error|
113
- notify_error_callbacks(error)
114
- end
115
-
116
- true
81
+ return true if @start_client_nats_connection && @client_nats_connection
82
+
83
+ GET_CONNECTED_MUTEX.synchronize do
84
+ break true if @client_nats_connection
85
+ break true if @start_client_nats_connection
86
+
87
+ # Disable publisher pending buffer on reconnect
88
+ options = config.connection_options.merge(:disable_reconnect_buffer => true)
89
+
90
+ client = NatsClient.new
91
+ client.connect(options)
92
+
93
+ # Ensure we have a valid connection to the NATS server.
94
+ client.flush(5)
95
+
96
+ client.on_disconnect do
97
+ logger.warn("Client NATS connection was disconnected")
98
+ end
99
+
100
+ client.on_reconnect do
101
+ logger.warn("Client NATS connection was reconnected")
117
102
  end
103
+
104
+ client.on_close do
105
+ logger.warn("Client NATS connection was closed")
106
+ end
107
+
108
+ client.on_error do |error|
109
+ notify_error_callbacks(error)
110
+ end
111
+
112
+ @client_nats_connection = client
113
+
114
+ true
118
115
  end
119
116
  end
120
117
 
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.9.0.pre2
4
+ version: 0.9.0.pre3
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-11-06 00:00:00.000000000 Z
11
+ date: 2017-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport