protobuf-nats 0.4.1 → 0.5.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: 4e8cb63868c843fc5f1117ed16798efe91ab8b9f
4
- data.tar.gz: d8deaefee3c498815d6a5d70339c2e9a05768d22
3
+ metadata.gz: 1a5657826a433d857f0b08fa99f314df065af9be
4
+ data.tar.gz: 0f4a0ec4a8c239e595251c2894f6e7d7bca8419a
5
5
  SHA512:
6
- metadata.gz: 8559ded4b5fbbfc35a785b3c22f007db3a36e99725f40e1a9d1ec9a895a4169d2113568651303184deec2aec340e22be442b59872d127d03f1c7d8afcc858343
7
- data.tar.gz: e97e4ac6a56e0bf258112790d9d8bcf27b89b2b34ce373b26e06696fc772be9dd93fbd725372146ed644d919b2628ad8d577784f8e7591585038c9876e6a1e97
6
+ metadata.gz: 541b8c84ffe66f575447b69c84198ed3eedebd996879fa9dc75b3ac210ed899dc526626d7a31f0a526ec68a8407f6a19d69248e6fb7dd4845ef2fc5d41e706a9
7
+ data.tar.gz: 34e7f770187a6d14d3d88aa4637688c8a5c4fd48e6fcf9dbf7f1a635a3fe0c9e739ecabcb6ccee6c8f0db34a188f58632cff4cf56b06a5fa6459bcbe84393214
data/README.md CHANGED
@@ -27,6 +27,9 @@ You can also use the following environment variables to tune parameters:
27
27
 
28
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
29
 
30
+ `PB_NATS_SERVER_PAUSE_FILE_PATH` - If this file exists, the server will pause by unsubscribing all services. When the
31
+ file is removed it will resubscribe and restart slow start (default: `nil`).
32
+
30
33
  `PB_NATS_SERVER_SLOW_START_DELAY` - Seconds to wait before adding another round of subscriptions (default 10).
31
34
 
32
35
  `PB_NATS_SERVER_SUBSCRIPTIONS_PER_RPC_ENDPOINT` - Number of subscriptions to create for each rpc endpoint. This number is
@@ -13,6 +13,7 @@ module Protobuf
13
13
 
14
14
  def initialize(options)
15
15
  @options = options
16
+ @processing_requests = true
16
17
  @running = true
17
18
  @stopped = false
18
19
 
@@ -59,6 +60,10 @@ module Protobuf
59
60
  was_enqueued
60
61
  end
61
62
 
63
+ def pause_file_path
64
+ ::ENV.fetch("PB_NATS_SERVER_PAUSE_FILE_PATH", nil)
65
+ end
66
+
62
67
  def print_subscription_keys
63
68
  logger.info "Creating subscriptions:"
64
69
 
@@ -67,7 +72,7 @@ module Protobuf
67
72
  end
68
73
  end
69
74
 
70
- def subscribe_to_services
75
+ def subscribe_to_services_once
71
76
  with_each_subscription_key do |subscription_key_and_queue|
72
77
  subscriptions << nats.subscribe(subscription_key_and_queue, :queue => subscription_key_and_queue) do |request_data, reply_id, _subject|
73
78
  unless enqueue_request(request_data, reply_id)
@@ -100,15 +105,36 @@ module Protobuf
100
105
  # We have (X - 1) here because we always subscribe at least once.
101
106
  (subscriptions_per_rpc_endpoint - 1).times do
102
107
  next unless @running
108
+ next if paused?
103
109
  completed += 1
104
110
  sleep slow_start_delay
105
- subscribe_to_services
111
+ subscribe_to_services_once
106
112
  logger.info "Slow start adding another round of subscriptions (#{completed}/#{subscriptions_per_rpc_endpoint})..."
107
113
  end
108
114
 
109
115
  logger.info "Slow start finished."
110
116
  end
111
117
 
118
+ def detect_and_handle_a_pause
119
+ case
120
+ # If we are taking requests and detect a pause file, then unsubscribe.
121
+ when @processing_requests && paused?
122
+ @processing_requests = false
123
+ logger.warn("Pausing server!")
124
+ unsubscribe
125
+
126
+ # If we were paused and the pause file is no longer present, then subscribe again.
127
+ when !@processing_requests && !paused?
128
+ logger.warn("Resuming server: resubscribing to all services and restarting slow start!")
129
+ @processing_requests = true
130
+ subscribe
131
+ end
132
+ end
133
+
134
+ def paused?
135
+ !pause_file_path.nil? && ::File.exist?(pause_file_path)
136
+ end
137
+
112
138
  def run
113
139
  nats.on_reconnect do
114
140
  logger.warn "Server NATS connection was reconnected"
@@ -127,19 +153,19 @@ module Protobuf
127
153
  end
128
154
 
129
155
  print_subscription_keys
130
- subscribe_to_services
131
- yield if block_given?
132
- finish_slow_start
156
+ if paused?
157
+ yield if block_given?
158
+ else
159
+ subscribe { yield if block_given? }
160
+ end
133
161
 
134
162
  loop do
135
163
  break unless @running
164
+ detect_and_handle_a_pause
136
165
  sleep 1
137
166
  end
138
167
 
139
- logger.info "Unsubscribing from rpc routes..."
140
- subscriptions.each do |subscription_id|
141
- nats.unsubscribe(subscription_id)
142
- end
168
+ unsubscribe
143
169
 
144
170
  logger.info "Waiting up to 60 seconds for the thread pool to finish shutting down..."
145
171
  thread_pool.shutdown
@@ -155,6 +181,19 @@ module Protobuf
155
181
  def stop
156
182
  @running = false
157
183
  end
184
+
185
+ def subscribe
186
+ subscribe_to_services_once
187
+ yield if block_given?
188
+ finish_slow_start
189
+ end
190
+
191
+ def unsubscribe
192
+ logger.info "Unsubscribing from rpc routes..."
193
+ subscriptions.each do |subscription_id|
194
+ nats.unsubscribe(subscription_id)
195
+ end
196
+ end
158
197
  end
159
198
  end
160
199
  end
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module Nats
3
- VERSION = "0.4.1"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  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.4.1
4
+ version: 0.5.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-05-23 00:00:00.000000000 Z
11
+ date: 2017-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: protobuf