redic-pool 0.1.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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MGMwN2MxMjRiMzExYWE3YzIwNmIzOGQ5MGM0ZTVkMjEwYTkzYWQ4NA==
5
+ data.tar.gz: !binary |-
6
+ MjUwMDgzYmY0YjA1MzI2Y2M3MzU2OTE4NmVmNmVlODExMDIyMmNiNg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZjA5NjdjYjg0YjY0Y2U4YmQ5MTNkMTYzNWM4YTc3ZTc0Nzc3M2IzZTI0Y2Qx
10
+ MzFlOGE1MjkzMzc4NjlkODlkZGY5ZGFjMmRiOTBjZWU2NDc4YjczNzAzMDgz
11
+ YWRjNzljMjhlOGI2NjkxMTFjOTc0NDU3YTQ1NDlhMTlkY2EyN2M=
12
+ data.tar.gz: !binary |-
13
+ ZTkyYWY5NzhlZjU3ODgzODliY2IzNDVjZTRjODJkMTkyMTdjN2IxYmE2ZTIz
14
+ M2U1NzcwNzkwZDFmOGIwZmRjMzgyYjY5OTcwZmJmM2ViOGVmMjkxZGUwMGEy
15
+ MzJhODg0MjRmNTE0NTE5MDAyOWMzZWI0Y2M2NWIyMWY1MDVjOTY=
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /.gs
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ 0.1.0 - 2013-12-18
2
+ ==================
3
+
4
+ * First release.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ Redic::Pool
2
+ ===========
3
+
4
+ A Redis connection pool using [Redis](https://github.com/amakawa/redic).
5
+
6
+ Usage
7
+ -----
8
+
9
+ require "redic/pool"
10
+
11
+ $redis = Redic::Pool.new("redis://localhost:6379", size: 10)
12
+
13
+ Array.new(100) do
14
+ Thread.new do
15
+ $redis.call("GET", "foo")
16
+ end
17
+ end.each(&:join)
18
+
19
+ $redis.call("INFO", "clients")[/connected_clients:(\d+)/, 1]
20
+ # => "10"
data/UNLICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
data/lib/redic/pool.rb ADDED
@@ -0,0 +1,22 @@
1
+ require "connection_pool"
2
+ require "redic"
3
+
4
+ class Redic::Pool
5
+ VERSION = "0.1.0"
6
+
7
+ attr :pool
8
+
9
+ def initialize(url, options = {})
10
+ @pool = ConnectionPool.new(size: options.fetch(:size, 10)) { Redic.new(url) }
11
+ end
12
+
13
+ def call(*args)
14
+ @pool.with do |client|
15
+ client.call(*args)
16
+ end
17
+ end
18
+
19
+ def with(&block)
20
+ @pool.with(&block)
21
+ end
22
+ end
data/rakefile ADDED
@@ -0,0 +1,5 @@
1
+ task :test do
2
+ exec "cutest test/**/*_test.rb"
3
+ end
4
+
5
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ require "./lib/redic/pool"
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "redic-pool"
5
+
6
+ s.version = Redic::Pool::VERSION
7
+
8
+ s.homepage = "https://github.com/djanowski/redic-pool"
9
+
10
+ s.summary = "A Redis connection pool using Redic."
11
+
12
+ s.authors = ["Damian Janowski"]
13
+
14
+ s.email = ["jano@dimaion.com"]
15
+
16
+ s.license = "Unlicense"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- test/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
21
+
22
+ s.add_dependency("connection_pool")
23
+ s.add_dependency("redic")
24
+
25
+ s.add_development_dependency("cutest")
26
+ end
data/test/pool_test.rb ADDED
@@ -0,0 +1,137 @@
1
+ require "cutest"
2
+
3
+ $VERBOSE = 1
4
+
5
+ module Parsers
6
+ def self.info(reply)
7
+ Hash[reply.lines.grep(/^[^#]/).map { |line| line.chomp.split(":", 2) }]
8
+ end
9
+ end
10
+
11
+ require_relative "../lib/redic/pool"
12
+
13
+ at_exit do
14
+ if $redis
15
+ Process.kill(:TERM, $redis)
16
+ end
17
+
18
+ Process.waitpid
19
+ end
20
+
21
+ $redis = spawn("redis-server --dir /tmp --dbfilename '' --port 9999 --logfile /dev/null")
22
+
23
+ sleep(0.5)
24
+
25
+ def teardown(r)
26
+ r.pool.shutdown { |c| c.call("QUIT") }
27
+ end
28
+
29
+ setup do
30
+ Redic::Pool.new("redis://localhost:9999", size: 10)
31
+ end
32
+
33
+ test "Pool" do |r|
34
+ threads = Array.new(100) do
35
+ Thread.new do
36
+ 10.times do
37
+ r.call("GET", "foo")
38
+ end
39
+ end
40
+ end
41
+
42
+ threads.each(&:join)
43
+
44
+ clients = Parsers.info(r.call("INFO", "clients")).fetch("connected_clients")
45
+
46
+ assert_equal(clients, "10")
47
+
48
+ teardown(r)
49
+ end
50
+
51
+ test "MULTI return value with WATCH" do |r|
52
+ r.call("DEL", "foo")
53
+
54
+ r.with do |c|
55
+ c.call("WATCH", "foo", "bar")
56
+
57
+ c.queue("MULTI")
58
+ c.queue("SET", "foo", "bar")
59
+ c.queue("EXEC")
60
+
61
+ assert_equal(c.commit.last, ["OK"])
62
+ end
63
+
64
+ assert_equal(r.call("GET", "foo"), "bar")
65
+
66
+ teardown(r)
67
+ end
68
+
69
+ test "Pipelining" do |r|
70
+ r.call("DEL", "foo")
71
+
72
+ catch(:out) do
73
+ r.with do |c|
74
+ c.queue("SET", "foo", "bar")
75
+ throw(:out)
76
+ end
77
+ end
78
+
79
+ assert_equal nil, r.call("GET", "foo")
80
+
81
+ teardown(r)
82
+ end
83
+
84
+ test "Pipelining with nesting" do |r|
85
+ r.call("DEL", "foo")
86
+
87
+ r.with do |c1|
88
+ c1.queue("DEL", "foo")
89
+
90
+ r.with do |c2|
91
+ c2.queue("SET", "foo", "bar")
92
+ end
93
+
94
+ c1.commit
95
+ end
96
+
97
+ assert_equal "bar", r.call("GET", "foo")
98
+
99
+ teardown(r)
100
+ end
101
+
102
+ test "Pipelining contention" do |r|
103
+ threads = Array.new(100) do
104
+ Thread.new do
105
+ 10.times do
106
+ r.with do |c|
107
+ c.call("SET", "foo", "bar")
108
+
109
+ r.with do |c|
110
+ c.call("DEL", "foo")
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ threads += Array.new(100) do
118
+ Thread.new do
119
+ 10.times do
120
+ r.with do |c|
121
+ c.queue("SET", "foo", "bar")
122
+ c.queue("DEL", "foo")
123
+ c.commit
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ threads.each(&:join)
130
+
131
+ clients = Parsers.info(r.call("INFO", "clients")).fetch("connected_clients")
132
+
133
+ assert_equal(clients, "10")
134
+ assert_equal(r.call("GET", "foo"), nil)
135
+
136
+ teardown(r)
137
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redic-pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Damian Janowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: connection_pool
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redic
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cutest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - jano@dimaion.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - CHANGELOG.md
64
+ - README.md
65
+ - UNLICENSE
66
+ - lib/redic/pool.rb
67
+ - rakefile
68
+ - redic-pool.gemspec
69
+ - test/pool_test.rb
70
+ homepage: https://github.com/djanowski/redic-pool
71
+ licenses:
72
+ - Unlicense
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.0.0
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: A Redis connection pool using Redic.
94
+ test_files:
95
+ - test/pool_test.rb