redis-slave 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/redis/slave.rb +80 -0
- data/lib/redis/slave/balancer.rb +37 -0
- data/lib/redis/slave/version.rb +5 -0
- data/redis-slave.gemspec +25 -0
- metadata +113 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/redis/slave.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require 'childprocess'
|
3
|
+
require 'tempfile'
|
4
|
+
require 'tmpdir'
|
5
|
+
require "redis/slave/version"
|
6
|
+
|
7
|
+
class Redis::Slave
|
8
|
+
|
9
|
+
attr_reader :process, :master_host, :master_port, :host, :port, :logfile, :dir
|
10
|
+
|
11
|
+
def initialize options={}
|
12
|
+
@master_host = options[:master_host] || '127.0.0.1'
|
13
|
+
@master_port = options[:master_port] || '6379'
|
14
|
+
@host = options[:host] || '127.0.0.1'
|
15
|
+
@port = options[:port] || find_available_port
|
16
|
+
@logfile = options[:logfile] || Tempfile.new('redis-slave-logfile').path
|
17
|
+
@dir = options[:dir] || Dir.tmpdir
|
18
|
+
end
|
19
|
+
|
20
|
+
def redis_master
|
21
|
+
::Redis.new(:host => @master_host, :port => @master_port)
|
22
|
+
end
|
23
|
+
|
24
|
+
def redis_slave
|
25
|
+
start!
|
26
|
+
::Redis.new(:host => @host, :port => @port)
|
27
|
+
end
|
28
|
+
|
29
|
+
def redis
|
30
|
+
Balancer.new(redis_master, redis_slave)
|
31
|
+
end
|
32
|
+
|
33
|
+
def started?
|
34
|
+
!!@started
|
35
|
+
end
|
36
|
+
|
37
|
+
def start!
|
38
|
+
return if started?
|
39
|
+
@process = ChildProcess.new('redis-server -')
|
40
|
+
process.duplex = true
|
41
|
+
process.start
|
42
|
+
at_exit{ process.send(:send_kill) }
|
43
|
+
process.io.stdin.puts config
|
44
|
+
process.io.stdin.close
|
45
|
+
@started = process.alive?
|
46
|
+
end
|
47
|
+
|
48
|
+
def config
|
49
|
+
<<-CONFIG
|
50
|
+
slaveof #{master_host} #{master_port}
|
51
|
+
slave-serve-stale-data yes
|
52
|
+
daemonize no
|
53
|
+
bind #{host}
|
54
|
+
port #{port}
|
55
|
+
logfile #{logfile}
|
56
|
+
databases 1
|
57
|
+
save 900 1
|
58
|
+
rdbcompression yes
|
59
|
+
dbfilename dump.rdb
|
60
|
+
dir #{dir}
|
61
|
+
appendonly no
|
62
|
+
appendfsync no
|
63
|
+
appendfsync everysec
|
64
|
+
no-appendfsync-on-rewrite yes
|
65
|
+
CONFIG
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
# copied from capybara-1.1.2
|
71
|
+
def find_available_port
|
72
|
+
server = TCPServer.new('127.0.0.1', 0)
|
73
|
+
server.addr[1]
|
74
|
+
ensure
|
75
|
+
server.close if server
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
require "redis/slave/balancer"
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Redis::Slave::Balancer
|
2
|
+
|
3
|
+
undef_method :type
|
4
|
+
|
5
|
+
MUTATING_METHODS = %w{
|
6
|
+
[]=
|
7
|
+
set hmset mset lset mapped_hmset getset msetnx setex hset setnx hsetnx
|
8
|
+
del hdel
|
9
|
+
srem zremrangebyscore zremrangebyrank zrem lrem
|
10
|
+
spop brpop rpop blpop lpop
|
11
|
+
lpush rpush
|
12
|
+
rpoplpush
|
13
|
+
incr zincrby incrby hincrby
|
14
|
+
decrby decr
|
15
|
+
ltrim
|
16
|
+
move
|
17
|
+
renamenx rename
|
18
|
+
save
|
19
|
+
subscribe unsubscribe
|
20
|
+
}
|
21
|
+
|
22
|
+
attr_accessor :master, :slave
|
23
|
+
|
24
|
+
def initialize master, slave
|
25
|
+
@master, @slave = master, slave
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing method, *args, &block
|
29
|
+
redis = MUTATING_METHODS.include?(method.to_s) ? @master : @slave
|
30
|
+
redis.send(method, *args, &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
def inspect
|
34
|
+
%[#<#{self.class} master=#{@master.inspect} slave=#{@slave.inspect}>]
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/redis-slave.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "redis/slave/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "redis-slave"
|
7
|
+
s.version = Redis::Slave::VERSION
|
8
|
+
s.authors = ["Jared Grippe"]
|
9
|
+
s.email = ["jared@deadlyicon.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{starts a redis slave process and balances communication between the master and slave}
|
12
|
+
s.description = %q{starts a redis slave process and balances communication between the master and slave}
|
13
|
+
|
14
|
+
s.rubyforge_project = "redis-slave"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rake"
|
22
|
+
|
23
|
+
s.add_runtime_dependency "redis"
|
24
|
+
s.add_runtime_dependency "childprocess"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-slave
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jared Grippe
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-01-13 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
22
|
+
none: false
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
hash: 3
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
prerelease: false
|
31
|
+
requirement: *id001
|
32
|
+
type: :development
|
33
|
+
name: rake
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
hash: 3
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
prerelease: false
|
45
|
+
requirement: *id002
|
46
|
+
type: :runtime
|
47
|
+
name: redis
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
prerelease: false
|
59
|
+
requirement: *id003
|
60
|
+
type: :runtime
|
61
|
+
name: childprocess
|
62
|
+
description: starts a redis slave process and balances communication between the master and slave
|
63
|
+
email:
|
64
|
+
- jared@deadlyicon.com
|
65
|
+
executables: []
|
66
|
+
|
67
|
+
extensions: []
|
68
|
+
|
69
|
+
extra_rdoc_files: []
|
70
|
+
|
71
|
+
files:
|
72
|
+
- .gitignore
|
73
|
+
- Gemfile
|
74
|
+
- Rakefile
|
75
|
+
- lib/redis/slave.rb
|
76
|
+
- lib/redis/slave/balancer.rb
|
77
|
+
- lib/redis/slave/version.rb
|
78
|
+
- redis-slave.gemspec
|
79
|
+
homepage: ""
|
80
|
+
licenses: []
|
81
|
+
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 3
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
requirements: []
|
106
|
+
|
107
|
+
rubyforge_project: redis-slave
|
108
|
+
rubygems_version: 1.8.15
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: starts a redis slave process and balances communication between the master and slave
|
112
|
+
test_files: []
|
113
|
+
|