restis-client 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +7 -0
- data/README +1 -0
- data/Rakefile +14 -0
- data/VERSION +1 -0
- data/autotest/discover.rb +1 -0
- data/init.rb +8 -0
- data/lib/restis/client.rb +34 -0
- data/spec/restis/client_spec.rb +40 -0
- data/spec/spec_helper.rb +17 -0
- metadata +78 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
redis durable topic subscriber implementation
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "restis-client"
|
5
|
+
gemspec.summary = "client for messaging over redis"
|
6
|
+
gemspec.description = "durable topic subscriber implementation"
|
7
|
+
gemspec.email = "douglas@theros.info"
|
8
|
+
gemspec.homepage = "http://github.com/qmx/restis-client"
|
9
|
+
gemspec.authors = ["Douglas Campos", "Gustavo Santana"]
|
10
|
+
end
|
11
|
+
Jeweler::GemcutterTasks.new
|
12
|
+
rescue LoadError
|
13
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
14
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1 @@
|
|
1
|
+
Autotest.add_discovery { "rspec2" }
|
data/init.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Restis
|
2
|
+
class Client
|
3
|
+
attr_accessor :redis
|
4
|
+
def initialize
|
5
|
+
@redis = Redis.new(:timeout => 0)
|
6
|
+
end
|
7
|
+
|
8
|
+
def publish(channel, message)
|
9
|
+
@redis.rpush("#{channel}:backlog", message)
|
10
|
+
@redis.publish(channel, message)
|
11
|
+
end
|
12
|
+
|
13
|
+
def subscribe(channel, key, &block)
|
14
|
+
loop do
|
15
|
+
last_read_msg = @redis.get(key)
|
16
|
+
queue_size = @redis.llen("#{channel}:backlog")
|
17
|
+
messages = @redis.lrange("#{channel}:backlog", last_read_msg, queue_size)
|
18
|
+
break if messages.empty?
|
19
|
+
messages.each do |msg|
|
20
|
+
block.call(@redis, channel, msg)
|
21
|
+
@redis.incr(key)
|
22
|
+
end
|
23
|
+
@redis.set(key, queue_size)
|
24
|
+
end
|
25
|
+
redis = Redis.new(:timeout => 0)
|
26
|
+
redis.subscribe(channel) do |on|
|
27
|
+
on.message do |channel, msg|
|
28
|
+
block.call(redis, channel, msg)
|
29
|
+
Redis.new.incr(key)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Restis::Client do
|
4
|
+
context "when publishing" do
|
5
|
+
it "should keep published items on backlog" do
|
6
|
+
redis = Redis.new
|
7
|
+
thread = Thread.new do
|
8
|
+
redis.subscribe("channel1") do |on|
|
9
|
+
on.message do |channel, message|
|
10
|
+
@message = message
|
11
|
+
redis.unsubscribe
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
client = Restis::Client.new
|
16
|
+
client.publish("channel1", "message1")
|
17
|
+
thread.join
|
18
|
+
redis.llen("channel1:backlog").should == 1
|
19
|
+
@message.should == "message1"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
context "when consuming" do
|
23
|
+
it "should receive the unread backlog before the subscription" do
|
24
|
+
publisher = Restis::Client.new
|
25
|
+
10.times { publisher.publish("ch1", "1234") }
|
26
|
+
thread = Thread.new do
|
27
|
+
sleep 0.42 # preventing race condition
|
28
|
+
publisher = Restis::Client.new
|
29
|
+
50.times {publisher.publish("ch1", "1234")}
|
30
|
+
end
|
31
|
+
subscriber = Restis::Client.new
|
32
|
+
subscriber.subscribe("ch1", "mykey") do |connection, channel, msg|
|
33
|
+
count = Redis.new.incr("count")
|
34
|
+
connection.unsubscribe if connection.subscribed? and count == 50
|
35
|
+
end
|
36
|
+
thread.join
|
37
|
+
Redis.new.get("count").should == "60"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: restis-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 0.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Douglas Campos
|
14
|
+
- Gustavo Santana
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-08-24 00:00:00 -03:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: durable topic subscriber implementation
|
24
|
+
email: douglas@theros.info
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files:
|
30
|
+
- README
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- autotest/discover.rb
|
38
|
+
- init.rb
|
39
|
+
- lib/restis/client.rb
|
40
|
+
- spec/restis/client_spec.rb
|
41
|
+
- spec/spec_helper.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/qmx/restis-client
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.3.7
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: client for messaging over redis
|
76
|
+
test_files:
|
77
|
+
- spec/restis/client_spec.rb
|
78
|
+
- spec/spec_helper.rb
|