ruby_nsq 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.md +7 -0
- data/LICENSE.txt +201 -0
- data/README.md +71 -0
- data/Rakefile +34 -0
- data/examples/async/reader.rb +58 -0
- data/examples/async/writer.rb +17 -0
- data/examples/simple/reader.rb +25 -0
- data/lib/nsq/backoff_timer.rb +42 -0
- data/lib/nsq/connection.rb +222 -0
- data/lib/nsq/loggable.rb +23 -0
- data/lib/nsq/message.rb +22 -0
- data/lib/nsq/queue_subscriber.rb +45 -0
- data/lib/nsq/reader.rb +99 -0
- data/lib/nsq/subscriber.rb +121 -0
- data/lib/nsq/timer.rb +39 -0
- data/lib/nsq.rb +35 -0
- data/lib/ruby_nsq.rb +1 -0
- data/test/backoff_timer_test.rb +27 -0
- data/test/sync_connection_test.rb +12 -0
- metadata +168 -0
data/History.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright 2012 Clarity Services, Inc.
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# ruby_nsq
|
2
|
+
|
3
|
+
https://github.com/ClarityServices/ruby_nsq
|
4
|
+
|
5
|
+
## Description:
|
6
|
+
|
7
|
+
Ruby client for the [NSQ](https://github.com/bitly/nsq) realtime message processing system.
|
8
|
+
|
9
|
+
## Install:
|
10
|
+
|
11
|
+
gem install ruby_nsq
|
12
|
+
|
13
|
+
## Usage:
|
14
|
+
|
15
|
+
See [examples](https://github.com/ClarityServices/ruby_nsq/tree/master/examples)
|
16
|
+
|
17
|
+
Simple example for synchronous message handling:
|
18
|
+
```
|
19
|
+
require 'nsq'
|
20
|
+
|
21
|
+
reader = NSQ.create_reader(:nsqd_tcp_addresses => '127.0.0.1:4150')
|
22
|
+
# Subscribe to topic=test channel=simple
|
23
|
+
reader.subscribe('test', 'simple') do |message|
|
24
|
+
# If this block raises an exception, then the message will be requeued.
|
25
|
+
puts "Read #{message.body}"
|
26
|
+
end
|
27
|
+
reader.run # Doesn't return until reader.stop is called
|
28
|
+
puts 'Reader stopped'
|
29
|
+
```
|
30
|
+
|
31
|
+
Advanced example demonstrating asynchronous handling of messages on multiple threads:
|
32
|
+
```
|
33
|
+
require 'nsq'
|
34
|
+
|
35
|
+
foo_worker_count = 50
|
36
|
+
bar_worker_count = 30
|
37
|
+
baz_worker_count = 20
|
38
|
+
|
39
|
+
reader = NSQ.create_reader(:nsqd_tcp_addresses => '127.0.0.1:4150')
|
40
|
+
|
41
|
+
foo_subscriber = reader.subscribe('test', 'foo', :max_in_flight => foo_worker_count)
|
42
|
+
bar_subscriber = reader.subscribe('test2', 'bar', :max_in_flight => bar_worker_count)
|
43
|
+
baz_subscriber = reader.subscribe('test2', 'baz', :max_in_flight => baz_worker_count)
|
44
|
+
|
45
|
+
foo_threads = foo_worker_count.times.map do |i|
|
46
|
+
Thread.new(i) do |i|
|
47
|
+
foo_subscriber.run do |message|
|
48
|
+
puts 'Foo[%02d] read: %s' % i, message.body
|
49
|
+
sleep rand(10) # Dummy processing of message
|
50
|
+
end
|
51
|
+
puts 'Foo[%02d] thread exiting' % i
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
bar_threads = ... Same kind of thing as above ...
|
56
|
+
baz_threads = ... Same kind of thing as above ...
|
57
|
+
|
58
|
+
reader.run # Doesn't return until reader.stop is called
|
59
|
+
puts 'Reader stopped'
|
60
|
+
foo_threads.each(&:join)
|
61
|
+
bar_threads.each(&:join)
|
62
|
+
baz_threads.each(&:join)
|
63
|
+
```
|
64
|
+
|
65
|
+
## TODO:
|
66
|
+
|
67
|
+
* Fix timestamp
|
68
|
+
|
69
|
+
* Implement lookupd
|
70
|
+
|
71
|
+
* Tests!
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'ruby_nsq'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.md')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
Bundler::GemHelper.install_tasks
|
24
|
+
|
25
|
+
require 'rake/testtask'
|
26
|
+
|
27
|
+
Rake::TestTask.new(:test) do |t|
|
28
|
+
t.libs << 'lib'
|
29
|
+
t.libs << 'test'
|
30
|
+
t.pattern = 'test/**/*_test.rb'
|
31
|
+
t.verbose = false
|
32
|
+
end
|
33
|
+
|
34
|
+
task :default => :test
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'nsq'
|
4
|
+
require 'thread'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
x_worker_count = 50
|
8
|
+
y_worker_count = 30
|
9
|
+
z_worker_count = 20
|
10
|
+
|
11
|
+
puts 'Press enter to start and enter to finish'
|
12
|
+
$stdin.gets
|
13
|
+
|
14
|
+
reader = NSQ.create_reader(
|
15
|
+
:nsqd_tcp_addresses => '127.0.0.1:4150',
|
16
|
+
:logger_level => Logger::DEBUG
|
17
|
+
)
|
18
|
+
|
19
|
+
x_subscriber = reader.subscribe('test_xy', 'x', :max_in_flight => x_worker_count)
|
20
|
+
y_subscriber = reader.subscribe('test_xy', 'y', :max_in_flight => y_worker_count)
|
21
|
+
z_subscriber = reader.subscribe('test_z', 'z', :max_in_flight => z_worker_count)
|
22
|
+
|
23
|
+
class MyThread < Thread
|
24
|
+
attr_accessor :message_count
|
25
|
+
def initialize(index, subscriber, char)
|
26
|
+
@index = index
|
27
|
+
super do |i, subscriber, char|
|
28
|
+
@message_count = 0
|
29
|
+
subscriber.run do |message|
|
30
|
+
eval message.body
|
31
|
+
print char
|
32
|
+
@message_count += 1
|
33
|
+
end
|
34
|
+
print char.upcase
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
threads = {}
|
40
|
+
[[x_subscriber, x_worker_count, 'x'], [y_subscriber, y_worker_count, 'y'], [z_subscriber, z_worker_count, 'z']].each do |subscriber, count, char|
|
41
|
+
threads[char] = count.times.map do |i|
|
42
|
+
MyThread.new(i, subscriber, char)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
main_thread = Thread.new do
|
47
|
+
reader.run
|
48
|
+
end
|
49
|
+
$stdin.gets
|
50
|
+
puts 'Exiting...'
|
51
|
+
reader.stop
|
52
|
+
main_thread.join
|
53
|
+
threads.each_value { |arr| arr.each(&:join) }
|
54
|
+
puts
|
55
|
+
puts "Summary of worker message counts"
|
56
|
+
threads.each do |char, arr|
|
57
|
+
puts "#{char} - #{arr.map(&:message_count).join(' ')}"
|
58
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
if ARGV.length != 3
|
4
|
+
$stderr.puts "ruby writer.rb <topic> <count> <eval-string>"
|
5
|
+
$stderr.puts " where <topic> is either test_xy or test_z"
|
6
|
+
$stderr.puts " and <eval-string> could be something like 'sleep rand(100)/10.0'"
|
7
|
+
$stderr.puts " Example: ./writer.rb test_xy 500 'sleep rand(100)/10.0'"
|
8
|
+
$stderr.puts " or: ./writer.rb test_z 5000 nil"
|
9
|
+
exit 1
|
10
|
+
end
|
11
|
+
topic = ARGV[0]
|
12
|
+
count = ARGV[1].to_i
|
13
|
+
eval_string = ARGV[2]
|
14
|
+
# TODO: Figure out TCP protocol
|
15
|
+
count.times do
|
16
|
+
system "curl -d #{eval_string.inspect} 'http://127.0.0.1:4151/put?topic=#{topic}'"
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'nsq'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
puts 'Press enter to start and enter to finish'
|
7
|
+
gets
|
8
|
+
reader = NSQ.create_reader(
|
9
|
+
:nsqd_tcp_addresses => '127.0.0.1:4150',
|
10
|
+
#:logger_level => Logger::DEBUG
|
11
|
+
)
|
12
|
+
thread = Thread.new do
|
13
|
+
begin
|
14
|
+
reader.subscribe('test', 'simple') do |message|
|
15
|
+
puts "Read #{message.body}"
|
16
|
+
end
|
17
|
+
reader.run
|
18
|
+
rescue Exception => e
|
19
|
+
$stderr.puts "Unexpected error: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
|
20
|
+
end
|
21
|
+
puts 'Reader exiting'
|
22
|
+
end
|
23
|
+
gets
|
24
|
+
reader.stop
|
25
|
+
thread.join
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# Stolen from pynsq library since somebodies thought about this a lot more than me
|
2
|
+
module NSQ
|
3
|
+
# This is a timer that is smart about backing off exponentially when there are problems
|
4
|
+
class BackoffTimer
|
5
|
+
|
6
|
+
attr_reader :min_interval, :max_interval, :short_interval, :long_interval
|
7
|
+
|
8
|
+
def initialize(min_interval, max_interval, ratio=0.25, short_length=10, long_length=250)
|
9
|
+
@min_interval = min_interval.to_f
|
10
|
+
@max_interval = max_interval.to_f
|
11
|
+
ratio = ratio.to_f
|
12
|
+
|
13
|
+
@max_short_timer = (@max_interval - @min_interval) * ratio
|
14
|
+
@max_long_timer = (@max_interval - @min_interval) * (1.0 - ratio)
|
15
|
+
@short_unit = @max_short_timer / short_length
|
16
|
+
@long_unit = @max_long_timer / long_length
|
17
|
+
|
18
|
+
@short_interval = 0.0
|
19
|
+
@long_interval = 0.0
|
20
|
+
end
|
21
|
+
|
22
|
+
# Update the timer to reflect a successful call
|
23
|
+
def success
|
24
|
+
@short_interval -= @short_unit
|
25
|
+
@long_interval -= @long_unit
|
26
|
+
@short_interval = [@short_interval, 0.0].max
|
27
|
+
@long_interval = [@long_interval, 0.0].max
|
28
|
+
end
|
29
|
+
|
30
|
+
# Update the timer to reflect a failed call
|
31
|
+
def failure
|
32
|
+
@short_interval += @short_unit
|
33
|
+
@long_interval += @long_unit
|
34
|
+
@short_interval = [@short_interval, @max_short_timer].min
|
35
|
+
@long_interval = [@long_interval, @max_long_timer].min
|
36
|
+
end
|
37
|
+
|
38
|
+
def interval
|
39
|
+
@min_interval + @short_interval + @long_interval
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,222 @@
|
|
1
|
+
require 'monitor'
|
2
|
+
require 'thread' #Mutex
|
3
|
+
|
4
|
+
module NSQ
|
5
|
+
class Connection
|
6
|
+
attr_reader :name
|
7
|
+
|
8
|
+
def initialize(reader, subscriber, host, port)
|
9
|
+
@reader = reader
|
10
|
+
@subscriber = subscriber
|
11
|
+
@selector = reader.selector
|
12
|
+
@host = host
|
13
|
+
@port = port
|
14
|
+
@name = "#{subscriber.name}:#{host}:#{port}"
|
15
|
+
@write_monitor = Monitor.new
|
16
|
+
@ready_mutex = Mutex.new
|
17
|
+
@sending_ready = false
|
18
|
+
|
19
|
+
# Connect states :init, :interval, :connecting, :connected, :closed
|
20
|
+
@connect_state = :init
|
21
|
+
|
22
|
+
@next_connection_time = nil
|
23
|
+
@next_ready_time = nil
|
24
|
+
@connection_backoff_timer = nil
|
25
|
+
@ready_backoff_timer = @subscriber.create_ready_backoff_timer
|
26
|
+
|
27
|
+
connect
|
28
|
+
end
|
29
|
+
|
30
|
+
def send_init(topic, channel, short_id, long_id)
|
31
|
+
write NSQ::MAGIC_V2
|
32
|
+
write "SUB #{topic} #{channel} #{short_id} #{long_id}\n"
|
33
|
+
self.send_ready
|
34
|
+
end
|
35
|
+
|
36
|
+
def send_ready
|
37
|
+
@ready_count = @subscriber.ready_count
|
38
|
+
write "RDY #{@ready_count}\n" unless @subscriber.stopped?
|
39
|
+
@sending_ready = false
|
40
|
+
end
|
41
|
+
|
42
|
+
def send_finish(id, success)
|
43
|
+
write "FIN #{id}\n"
|
44
|
+
@ready_mutex.synchronize do
|
45
|
+
@ready_count -= 1
|
46
|
+
if success
|
47
|
+
@ready_backoff_timer.success
|
48
|
+
else
|
49
|
+
@ready_backoff_timer.failure
|
50
|
+
end
|
51
|
+
check_ready
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def send_requeue(id, time_ms)
|
56
|
+
write "REQ #{id} #{time_ms}\n"
|
57
|
+
@ready_mutex.synchronize do
|
58
|
+
@ready_count -= 1
|
59
|
+
@ready_backoff_timer.failure
|
60
|
+
check_ready
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def reset
|
65
|
+
return unless verify_connect_state?(:connecting, :connected)
|
66
|
+
# Close with the hopes of re-establishing
|
67
|
+
close(false)
|
68
|
+
@write_monitor.synchronize do
|
69
|
+
return unless verify_connect_state?(:init)
|
70
|
+
@connection_backoff_timer ||= @subscriber.create_connection_backoff_timer
|
71
|
+
@connection_backoff_timer.failure
|
72
|
+
interval = @connection_backoff_timer.interval
|
73
|
+
if interval > 0
|
74
|
+
@connect_state = :interval
|
75
|
+
NSQ.logger.debug {"#{self}: Reattempting connection in #{interval} seconds"}
|
76
|
+
@reader.add_timeout(interval) do
|
77
|
+
connect
|
78
|
+
end
|
79
|
+
else
|
80
|
+
connect
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def close(permanent=true)
|
86
|
+
NSQ.logger.debug {"#{@name}: Closing..."}
|
87
|
+
@write_monitor.synchronize do
|
88
|
+
begin
|
89
|
+
@selector.deregister(@socket)
|
90
|
+
# Use straight socket to write otherwise we need to use Monitor instead of Mutex
|
91
|
+
@socket.write "CLS\n"
|
92
|
+
@socket.close
|
93
|
+
rescue Exception => e
|
94
|
+
ensure
|
95
|
+
@connect_state = permanent ? :closed : :init
|
96
|
+
@socket = nil
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def connect
|
102
|
+
return unless verify_connect_state?(:init, :interval)
|
103
|
+
NSQ.logger.debug {"#{self}: Beginning connect"}
|
104
|
+
@connect_state = :connecting
|
105
|
+
@buffer = ''
|
106
|
+
@connecting = false
|
107
|
+
@connected = false
|
108
|
+
@ready_count = 0
|
109
|
+
@socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
110
|
+
@sockaddr = Socket.pack_sockaddr_in(@port, @host)
|
111
|
+
@monitor = @selector.register(@socket, :w)
|
112
|
+
@monitor.value = proc { do_connect }
|
113
|
+
do_connect
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
def do_connect
|
119
|
+
@write_monitor.synchronize do
|
120
|
+
return unless verify_connect_state?(:connecting)
|
121
|
+
begin
|
122
|
+
@socket.connect_nonblock(@sockaddr)
|
123
|
+
# Apparently we always throw an exception here
|
124
|
+
NSQ.logger.debug {"#{self}: do_connect fell thru without throwing an exception"}
|
125
|
+
rescue Errno::EINPROGRESS
|
126
|
+
NSQ.logger.debug {"#{self}: do_connect - connect in progress"}
|
127
|
+
rescue Errno::EISCONN
|
128
|
+
NSQ.logger.debug {"#{self}: do_connect - connection complete"}
|
129
|
+
@selector.deregister(@socket)
|
130
|
+
monitor = @selector.register(@socket, :r)
|
131
|
+
monitor.value = proc { read_messages }
|
132
|
+
@connect_state = :connected
|
133
|
+
# The assumption for connections is that a good connection means the server is good, no ramping back up like ready counts
|
134
|
+
@connection_backoff_timer = nil
|
135
|
+
@subscriber.handle_connection(self)
|
136
|
+
rescue SystemCallError => e
|
137
|
+
@subscriber.handle_io_error(self, e)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def check_ready
|
143
|
+
if !@sending_ready && @ready_count <= @subscriber.ready_threshold
|
144
|
+
interval = @ready_backoff_timer.interval
|
145
|
+
if interval == 0.0
|
146
|
+
send_ready
|
147
|
+
else
|
148
|
+
NSQ.logger.debug {"#{self}: Delaying READY for #{interval} seconds"}
|
149
|
+
@sending_ready = true
|
150
|
+
@reader.add_timeout(interval) do
|
151
|
+
send_ready
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def read_messages
|
158
|
+
@buffer << @socket.read_nonblock(4096)
|
159
|
+
while @buffer.length >= 8
|
160
|
+
size, frame = @buffer.unpack('NN')
|
161
|
+
break if @buffer.length < 4+size
|
162
|
+
case frame
|
163
|
+
when NSQ::FRAME_TYPE_RESPONSE
|
164
|
+
if @buffer[8,11] == "_heartbeat_"
|
165
|
+
send_nop
|
166
|
+
@subscriber.handle_heartbeat(self)
|
167
|
+
@buffer = @buffer[(4+size)..-1]
|
168
|
+
else
|
169
|
+
NSQ.logger.error("I don't know what to do with the rest of this buffer: #{@buffer[8,size-4].inspect}") if @buffer.length > 8
|
170
|
+
@buffer = @buffer[(4+size)..-1]
|
171
|
+
end
|
172
|
+
when NSQ::FRAME_TYPE_ERROR
|
173
|
+
@subscriber.handle_frame_error(self, @buffer[8, size-4])
|
174
|
+
@buffer = @buffer[(4+size)..-1]
|
175
|
+
when NSQ::FRAME_TYPE_MESSAGE
|
176
|
+
raise "Bad message: #{@buffer.inspect}" if size < 30
|
177
|
+
ts_hi, ts_lo, attempts, id = @buffer.unpack('@8NNna16')
|
178
|
+
body = @buffer[34, size-30]
|
179
|
+
message = Message.new(self, id, ts_hi, ts_lo, attempts, body)
|
180
|
+
@buffer = @buffer[(4+size)..-1]
|
181
|
+
NSQ.logger.debug {"#{self}: Read message=#{message}"}
|
182
|
+
@subscriber.handle_message(self, message)
|
183
|
+
else
|
184
|
+
raise "Unrecognized message frame: #{frame} buffer=#{@buffer.inspect}"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
rescue Exception => e
|
188
|
+
@subscriber.handle_io_error(self, e)
|
189
|
+
end
|
190
|
+
|
191
|
+
def send_nop
|
192
|
+
write "NOP\n"
|
193
|
+
end
|
194
|
+
|
195
|
+
def write(msg)
|
196
|
+
NSQ.logger.debug {"#{@name}: Sending #{msg.inspect}"}
|
197
|
+
# We should only ever have one reader but we can have multiple writers
|
198
|
+
@write_monitor.synchronize do
|
199
|
+
@socket.write(msg) if verify_connect_state?(:connected)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
def to_s
|
204
|
+
@name
|
205
|
+
end
|
206
|
+
|
207
|
+
private
|
208
|
+
|
209
|
+
def verify_connect_state?(*states)
|
210
|
+
return true if states.include?(@connect_state)
|
211
|
+
NSQ.logger.error("Unexpected connect state of #{@connect_state}, expected to be in #{states.inspect}\n\t#{caller[0]}")
|
212
|
+
if @connect_state != :closed
|
213
|
+
# Likely in a bug state.
|
214
|
+
# I don't want to get in an endless loop of exceptions. Is this a good idea or bad? Maybe close to deregister first
|
215
|
+
# Attempt recovery
|
216
|
+
@connect_state = :init
|
217
|
+
connect
|
218
|
+
end
|
219
|
+
return false
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
data/lib/nsq/loggable.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module NSQ
|
2
|
+
module Loggable
|
3
|
+
def logger
|
4
|
+
@logger ||= (rails_logger || default_logger)
|
5
|
+
end
|
6
|
+
|
7
|
+
def rails_logger
|
8
|
+
(defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger) ||
|
9
|
+
(defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:debug) && RAILS_DEFAULT_LOGGER)
|
10
|
+
end
|
11
|
+
|
12
|
+
def default_logger
|
13
|
+
require 'logger'
|
14
|
+
l = Logger.new($stdout)
|
15
|
+
l.level = Logger::INFO
|
16
|
+
l
|
17
|
+
end
|
18
|
+
|
19
|
+
def logger=(logger)
|
20
|
+
@logger = logger
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/nsq/message.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module NSQ
|
2
|
+
class Message
|
3
|
+
attr_reader :connection, :id, :attempts, :body
|
4
|
+
|
5
|
+
def initialize(connection, id, timestamp_high, timestamp_low, attempts, body)
|
6
|
+
@connection = connection
|
7
|
+
@id = id
|
8
|
+
@timestamp_high = timestamp_high
|
9
|
+
@timestamp_low = timestamp_low
|
10
|
+
@attempts = attempts
|
11
|
+
@body = body
|
12
|
+
end
|
13
|
+
|
14
|
+
def timestamp
|
15
|
+
Time.at((@timestamp_high * 2**32 + @timestamp_low) / 1000000000.0)
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
"#{connection} id=#{id} timestamp=#{timestamp} attempts=#{attempts} body=#{body}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'thread' #Mutex
|
2
|
+
|
3
|
+
module NSQ
|
4
|
+
class QueueSubscriber < Subscriber
|
5
|
+
def initialize(reader, topic, channel, options)
|
6
|
+
super
|
7
|
+
@queue = Queue.new
|
8
|
+
@run_mutex = Mutex.new
|
9
|
+
@run_count = 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def ready_count
|
13
|
+
# Return the minimum of Subscriber#ready_count and the amount of space left in the queue
|
14
|
+
[super, self.max_in_flight - @queue.size].min
|
15
|
+
end
|
16
|
+
|
17
|
+
def handle_message(connection, message)
|
18
|
+
@queue << [connection, message]
|
19
|
+
end
|
20
|
+
|
21
|
+
def run(&block)
|
22
|
+
@run_mutex.synchronize { @run_count += 1}
|
23
|
+
until @stopped
|
24
|
+
pair = @queue.pop
|
25
|
+
if pair == :stop
|
26
|
+
@queue << :stop
|
27
|
+
return
|
28
|
+
end
|
29
|
+
connection, message = pair
|
30
|
+
process_message(connection, message, &block)
|
31
|
+
end
|
32
|
+
ensure
|
33
|
+
@run_mutex.synchronize { @run_count -= 1}
|
34
|
+
end
|
35
|
+
|
36
|
+
def stop
|
37
|
+
@stopped = true
|
38
|
+
# Give the threads something to pop
|
39
|
+
@queue << :stop
|
40
|
+
# TODO: Put a max time on this so we don't potentially hang
|
41
|
+
sleep 1 while @run_count > 0
|
42
|
+
super
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/nsq/reader.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'thread'
|
3
|
+
require 'monitor'
|
4
|
+
require 'nio'
|
5
|
+
#require 'thread_safe'
|
6
|
+
|
7
|
+
module NSQ
|
8
|
+
class Reader
|
9
|
+
attr_reader :name, :long_id, :short_id, :selector, :options
|
10
|
+
|
11
|
+
def initialize(options={})
|
12
|
+
@options = options
|
13
|
+
@nsqd_tcp_addresses = s_to_a(options[:nsqd_tcp_addresses])
|
14
|
+
@lookupd_http_addresses = s_to_a(options[:lookupd_http_addresses])
|
15
|
+
@lookupd_poll_interval = options[:lookupd_poll_interval] || 120
|
16
|
+
@long_id = options[:long_id] || Socket.gethostname
|
17
|
+
@short_id = options[:short_id] || @long_id.split('.')[0]
|
18
|
+
NSQ.logger = options[:logger] if options[:logger]
|
19
|
+
NSQ.logger.level = options[:logger_level] if options[:logger_level]
|
20
|
+
|
21
|
+
@selector = ::NIO::Selector.new
|
22
|
+
@timer = Timer.new(@selector)
|
23
|
+
@topic_count = Hash.new(0)
|
24
|
+
@subscribers = {}
|
25
|
+
@subscriber_mutex = Monitor.new
|
26
|
+
@name = "#{@long_id}:#{@short_id}"
|
27
|
+
|
28
|
+
raise 'Must pass either option :nsqd_tcp_addresses or :lookupd_http_addresses' if @nsqd_tcp_addresses.empty? && @lookupd_http_addresses.empty?
|
29
|
+
|
30
|
+
# TODO: If the messages are failing, the backoff timer will exponentially increase a timeout before sending a RDY
|
31
|
+
#self.backoff_timer = dict((k, BackoffTimer.BackoffTimer(0, 120)) for k in self.task_lookup.keys())
|
32
|
+
|
33
|
+
@conns = {}
|
34
|
+
@last_lookup = nil
|
35
|
+
|
36
|
+
@logger.info("starting reader for topic '%s'..." % self.topic) if @logger
|
37
|
+
end
|
38
|
+
|
39
|
+
def subscribe(topic, channel, subscribe_options={}, &block)
|
40
|
+
NSQ.assert_topic_and_channel_valid(topic, channel)
|
41
|
+
@topic = topic
|
42
|
+
@channel = channel
|
43
|
+
subscriber = nil
|
44
|
+
name = "#{topic}:#{channel}"
|
45
|
+
@subscriber_mutex.synchronize do
|
46
|
+
raise "Already subscribed to #{name}" if @subscribers[name]
|
47
|
+
subscriber_class = block_given? ? Subscriber : QueueSubscriber
|
48
|
+
subscriber = @subscribers[name] = subscriber_class.new(self, topic, channel, subscribe_options, &block)
|
49
|
+
end
|
50
|
+
|
51
|
+
@nsqd_tcp_addresses.each do |addr|
|
52
|
+
address, port = addr.split(':')
|
53
|
+
subscriber.add_connection(address, port.to_i)
|
54
|
+
end
|
55
|
+
subscriber
|
56
|
+
end
|
57
|
+
|
58
|
+
def unsubscribe(topic, channel)
|
59
|
+
name = "#{topic}:#{channel}"
|
60
|
+
@subscriber_mutex.synchronize do
|
61
|
+
subscriber = @subscribers[name]
|
62
|
+
return unless subscriber
|
63
|
+
subscriber.stop
|
64
|
+
@subscribers.delete(name)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def run
|
69
|
+
@stopped = false
|
70
|
+
until @stopped do
|
71
|
+
if (Time.now.to_i - @last_lookup.to_i) > @lookupd_poll_interval
|
72
|
+
# Do lookupd
|
73
|
+
end
|
74
|
+
@selector.select(@timer.next_interval) { |m| m.value.call }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def stop
|
79
|
+
NSQ.logger.info("#{self}: Reader stopping...")
|
80
|
+
@stopped = true
|
81
|
+
@selector.wakeup
|
82
|
+
@subscriber_mutex.synchronize do
|
83
|
+
@subscribers.each_value {|subscriber| subscriber.stop}
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def add_timeout(interval, &block)
|
88
|
+
@timer.add(interval, &block)
|
89
|
+
end
|
90
|
+
|
91
|
+
def to_s
|
92
|
+
@name
|
93
|
+
end
|
94
|
+
|
95
|
+
def s_to_a(val)
|
96
|
+
val.kind_of?(String) ? [val] : val
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module NSQ
|
2
|
+
class Subscriber
|
3
|
+
attr_reader :selector, :name
|
4
|
+
attr_accessor :max_in_flight
|
5
|
+
|
6
|
+
def initialize(reader, topic, channel, options, &block)
|
7
|
+
options = reader.options.merge(options)
|
8
|
+
@name = "#{reader.name}:#{topic}:#{channel}"
|
9
|
+
@reader = reader
|
10
|
+
@selector = reader.selector
|
11
|
+
@topic = topic
|
12
|
+
@channel = channel
|
13
|
+
@block = block
|
14
|
+
@max_tries = options[:max_tries]
|
15
|
+
@max_in_flight = (options[:max_in_flight] || 1).to_i
|
16
|
+
@requeue_delay = (options[:requeue_delay] || 90).to_i * 1000
|
17
|
+
@connection_hash = {}
|
18
|
+
|
19
|
+
ready_options = options[:ready_backoff_timer] || {}
|
20
|
+
connection_options = options[:connection_backoff_timer] || {}
|
21
|
+
|
22
|
+
@ready_min_interval = ready_options[:min_interval] || 0
|
23
|
+
@ready_max_interval = ready_options[:max_interval] || 120
|
24
|
+
@ready_ratio = ready_options[:ratio] || 0.25
|
25
|
+
@ready_short_length = ready_options[:short_length] || 10
|
26
|
+
@ready_long_length = ready_options[:long_length] || 250
|
27
|
+
|
28
|
+
@connection_min_interval = connection_options[:min_interval] || 0
|
29
|
+
@connection_max_interval = connection_options[:max_interval] || 30
|
30
|
+
@connection_ratio = connection_options[:ratio] || 0.25
|
31
|
+
@connection_short_length = connection_options[:short_length] || 10
|
32
|
+
@connection_long_length = connection_options[:long_length] || 250
|
33
|
+
|
34
|
+
raise "Invalid value for max_in_flight, must be between 0 and 2500: #{@max_in_flight}" unless @max_in_flight.between?(1,2499)
|
35
|
+
end
|
36
|
+
|
37
|
+
def create_ready_backoff_timer
|
38
|
+
BackoffTimer.new(@ready_min_interval, @ready_max_interval, @ready_ratio, @ready_short_length, @ready_long_length)
|
39
|
+
end
|
40
|
+
|
41
|
+
def create_connection_backoff_timer
|
42
|
+
BackoffTimer.new(@connection_min_interval, @connection_max_interval, @connection_ratio, @connection_short_length, @connection_long_length)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Threshold for a connection where it's time to send a new READY message
|
46
|
+
def ready_threshold
|
47
|
+
@max_in_flight / @connection_hash.size / 4
|
48
|
+
end
|
49
|
+
|
50
|
+
# The actual value for the READY message
|
51
|
+
def ready_count
|
52
|
+
# TODO: Should we take into account the last_ready_count minus the number of messages sent since then?
|
53
|
+
# Rounding up!
|
54
|
+
(@max_in_flight + @connection_hash.size - 1) / @connection_hash.size
|
55
|
+
end
|
56
|
+
|
57
|
+
def connection_count
|
58
|
+
@connection_hash.size
|
59
|
+
end
|
60
|
+
|
61
|
+
def add_connection(host, port)
|
62
|
+
@connection_hash[[host, port]] = Connection.new(@reader, self, host, port)
|
63
|
+
end
|
64
|
+
|
65
|
+
def remove_connection(host, port)
|
66
|
+
connection = @connection_hash.delete([host, port])
|
67
|
+
return unless connection
|
68
|
+
connection.close
|
69
|
+
end
|
70
|
+
|
71
|
+
def stop
|
72
|
+
@stopped = true
|
73
|
+
@connection_hash.each_value do |connection|
|
74
|
+
connection.close
|
75
|
+
end
|
76
|
+
@connection_hash.clear
|
77
|
+
end
|
78
|
+
|
79
|
+
def stopped?
|
80
|
+
@stopped
|
81
|
+
end
|
82
|
+
|
83
|
+
def handle_connection(connection)
|
84
|
+
connection.send_init(@topic, @channel, @reader.short_id, @reader.long_id)
|
85
|
+
end
|
86
|
+
|
87
|
+
def handle_heartbeat(connection)
|
88
|
+
end
|
89
|
+
|
90
|
+
def handle_message(connection, message)
|
91
|
+
process_message(connection, message, &@block)
|
92
|
+
end
|
93
|
+
|
94
|
+
def process_message(connection, message, &block)
|
95
|
+
yield message
|
96
|
+
connection.send_finish(message.id, true)
|
97
|
+
rescue Exception => e
|
98
|
+
NSQ.logger.error("#{connection.name}: Exception during handle_message: #{e.message}\n\t#{e.backtrace.join("\n\t")}")
|
99
|
+
if @max_tries && attempts >= @max_tries
|
100
|
+
NSQ.logger.warning("#{connection.name}: Giving up on message after #{@max_tries} tries: #{body.inspect}")
|
101
|
+
connection.send_finish(message.id, false)
|
102
|
+
else
|
103
|
+
connection.send_requeue(message.id, attempts * @requeue_delay)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def handle_frame_error(connection, error_message)
|
108
|
+
NSQ.logger.error("Received error from nsqd: #{error_message.inspect}")
|
109
|
+
connection.reset
|
110
|
+
end
|
111
|
+
|
112
|
+
def handle_io_error(connection, exception)
|
113
|
+
NSQ.logger.error("Socket error: #{exception.message}\n\t#{exception.backtrace[0,2].join("\n\t")}")
|
114
|
+
connection.reset
|
115
|
+
end
|
116
|
+
|
117
|
+
def to_s
|
118
|
+
@name
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/lib/nsq/timer.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'thread'
|
2
|
+
|
3
|
+
module NSQ
|
4
|
+
class Timer
|
5
|
+
def initialize(selector)
|
6
|
+
@selector = selector
|
7
|
+
@proc_array = []
|
8
|
+
@mutex = Mutex.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def add(interval, &block)
|
12
|
+
new_run_at = Time.now + interval
|
13
|
+
@mutex.synchronize do
|
14
|
+
old_next_pair = @proc_array.first
|
15
|
+
@proc_array << [new_run_at, block]
|
16
|
+
# Sort the proc_array so the next one to run is at the front
|
17
|
+
@proc_array.sort_by { |pair| pair.first }
|
18
|
+
new_next_pair = @proc_array.first
|
19
|
+
# If the next proc has changed, then wakeup the selector so we can set the new next time
|
20
|
+
@selector.wakeup unless new_next_pair == old_next_pair
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Execute any necessary procs and return the next interval or nil if no procs
|
25
|
+
def next_interval
|
26
|
+
now = Time.now
|
27
|
+
@mutex.synchronize do
|
28
|
+
loop do
|
29
|
+
run_at, proc = @proc_array.first
|
30
|
+
return nil unless run_at
|
31
|
+
interval = run_at - now
|
32
|
+
return interval if interval > 0
|
33
|
+
proc.call
|
34
|
+
@proc_array.shift
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/nsq.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'nsq/loggable'
|
2
|
+
require 'nsq/message'
|
3
|
+
require 'nsq/reader'
|
4
|
+
require 'nsq/subscriber'
|
5
|
+
require 'nsq/queue_subscriber'
|
6
|
+
require 'nsq/connection'
|
7
|
+
require 'nsq/backoff_timer'
|
8
|
+
require 'nsq/timer'
|
9
|
+
|
10
|
+
module NSQ
|
11
|
+
extend NSQ::Loggable
|
12
|
+
|
13
|
+
MAGIC_V2 = " V2"
|
14
|
+
|
15
|
+
FRAME_TYPE_RESPONSE = 0
|
16
|
+
FRAME_TYPE_ERROR = 1
|
17
|
+
FRAME_TYPE_MESSAGE = 2
|
18
|
+
|
19
|
+
def self.create_reader(options, &block)
|
20
|
+
Reader.new(options, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.assert_topic_and_channel_valid(topic, channel)
|
24
|
+
raise "Invalid topic #{topic}" unless valid_topic_name?(topic)
|
25
|
+
raise "Invalid channel #{channel}" unless valid_channel_name?(channel)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.valid_topic_name?(topic)
|
29
|
+
!!topic.match(/^[\.a-zA-Z0-9_-]+$/)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.valid_channel_name?(channel)
|
33
|
+
!!channel.match(/^[\.a-zA-Z0-9_-]+(#ephemeral)?$/)
|
34
|
+
end
|
35
|
+
end
|
data/lib/ruby_nsq.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'nsq'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'nsq/backoff_timer'
|
3
|
+
|
4
|
+
describe ::NSQ::BackoffTimer do
|
5
|
+
before do
|
6
|
+
@timer = ::NSQ::BackoffTimer.new(0.1, 120, 0.25, 10, 1000)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "" do
|
10
|
+
it "should return the proper interval on successes and failures" do
|
11
|
+
assert_equal '%0.4f' % @timer.interval, '0.1000', @timer.inspect
|
12
|
+
@timer.success
|
13
|
+
assert_equal '%0.4f' % @timer.interval, '0.1000', @timer.inspect
|
14
|
+
@timer.failure
|
15
|
+
assert_equal '%0.2f' % @timer.interval, '3.19', @timer.inspect
|
16
|
+
assert_equal '%0.4f' % @timer.min_interval, '0.1000', @timer.inspect
|
17
|
+
assert_equal '%0.4f' % @timer.short_interval, '2.9975', @timer.inspect
|
18
|
+
assert_equal '%0.6f' % @timer.long_interval, '0.089925', @timer.inspect
|
19
|
+
@timer.failure
|
20
|
+
assert_equal '%0.2f' % @timer.interval, '6.27', @timer.inspect
|
21
|
+
@timer.success
|
22
|
+
assert_equal '%0.2f' % @timer.interval, '3.19', @timer.inspect
|
23
|
+
25.times { @timer.failure }
|
24
|
+
assert_equal '%0.2f' % @timer.interval, '32.41', @timer.inspect
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
c = SyncConn()
|
3
|
+
c.connect("127.0.0.1", 4150)
|
4
|
+
c.send(nsq.subscribe('test', 'ch', 'a', 'b'))
|
5
|
+
10.times do
|
6
|
+
c.send(nsq.ready(1))
|
7
|
+
resp = c.read_response()
|
8
|
+
unpacked = nsq.unpack_response(resp)
|
9
|
+
msg = nsq.decode_message(unpacked[1])
|
10
|
+
print msg.id, msg.body
|
11
|
+
c.send(nsq.finish(msg.id))
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_nsq
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brad Pardee
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nio4r
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rdoc
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: minitest
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: turn
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: wirble
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: hirb
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: Ruby client for NSQ modeled after pynsq
|
111
|
+
email:
|
112
|
+
- bradpardee@gmail.com
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- lib/nsq/backoff_timer.rb
|
118
|
+
- lib/nsq/connection.rb
|
119
|
+
- lib/nsq/loggable.rb
|
120
|
+
- lib/nsq/message.rb
|
121
|
+
- lib/nsq/queue_subscriber.rb
|
122
|
+
- lib/nsq/reader.rb
|
123
|
+
- lib/nsq/subscriber.rb
|
124
|
+
- lib/nsq/timer.rb
|
125
|
+
- lib/nsq.rb
|
126
|
+
- lib/ruby_nsq.rb
|
127
|
+
- examples/async/reader.rb
|
128
|
+
- examples/async/writer.rb
|
129
|
+
- examples/simple/reader.rb
|
130
|
+
- LICENSE.txt
|
131
|
+
- Rakefile
|
132
|
+
- History.md
|
133
|
+
- README.md
|
134
|
+
- test/backoff_timer_test.rb
|
135
|
+
- test/sync_connection_test.rb
|
136
|
+
homepage: http://github.com/ClarityServices/ruby_nsq
|
137
|
+
licenses: []
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
none: false
|
144
|
+
requirements:
|
145
|
+
- - ! '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
segments:
|
149
|
+
- 0
|
150
|
+
hash: 1380563601085235267
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
153
|
+
requirements:
|
154
|
+
- - ! '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
segments:
|
158
|
+
- 0
|
159
|
+
hash: 1380563601085235267
|
160
|
+
requirements: []
|
161
|
+
rubyforge_project:
|
162
|
+
rubygems_version: 1.8.23
|
163
|
+
signing_key:
|
164
|
+
specification_version: 3
|
165
|
+
summary: Ruby client for NSQ
|
166
|
+
test_files:
|
167
|
+
- test/backoff_timer_test.rb
|
168
|
+
- test/sync_connection_test.rb
|