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 +4 -4
- data/README.md +3 -0
- data/lib/protobuf/nats/server.rb +48 -9
- data/lib/protobuf/nats/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a5657826a433d857f0b08fa99f314df065af9be
|
4
|
+
data.tar.gz: 0f4a0ec4a8c239e595251c2894f6e7d7bca8419a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/protobuf/nats/server.rb
CHANGED
@@ -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
|
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
|
-
|
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
|
-
|
131
|
-
|
132
|
-
|
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
|
-
|
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
|
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
|
+
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-
|
11
|
+
date: 2017-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: protobuf
|