Class: RedCluster::ReplicaSet

Inherits:
Object
  • Object
show all
Defined in:
lib/replica_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster, options) ⇒ ReplicaSet



5
6
7
8
9
10
# File 'lib/replica_set.rb', line 5

def initialize(cluster, options)
  @my_cluster = cluster
  @master = Redis.new options[:master]
  @slaves = options[:slaves].map { |slave_config| Redis.new slave_config }
  setup_slaves
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(command, *args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/replica_set.rb', line 23

def method_missing(command, *args)
  if blocking_command?(command)
    raise "Blocking Commands Not Permitted"
  elsif pub_sub_command?(command)
    raise "Pub Sub Commands Not Permitted"
  elsif slaveof_command?(command)
    raise "Slave Commands Not Permitted"
  elsif command == :shutdown
    @master.shutdown
    @slaves.each(&:shutdown)
  elsif @in_multi
    @cmd_order_in_multi << @my_cluster.send(:multi_count)
    @master.send command, *args
  elsif read_command?(command)
    next_slave.send command, *args
  else
    @master.send command, *args
  end
rescue Errno::ECONNREFUSED
  new_master = @slaves.shift
  raise(NoMaster, "No master in replica set") unless new_master
  @master = new_master
  setup_slaves
  retry
end

Instance Attribute Details

#masterObject (readonly)

Returns the value of attribute master.



3
4
5
# File 'lib/replica_set.rb', line 3

def master
  @master
end

#slavesObject (readonly)

Returns the value of attribute slaves.



3
4
5
# File 'lib/replica_set.rb', line 3

def slaves
  @slaves
end

Instance Method Details

#execObject



18
19
20
21
# File 'lib/replica_set.rb', line 18

def exec
  @in_multi = nil
  @master.exec.map { |result| [@cmd_order_in_multi.shift, result] }
end

#multiObject



12
13
14
15
16
# File 'lib/replica_set.rb', line 12

def multi
  @in_multi = true
  @cmd_order_in_multi = []
  @master.multi
end

#next_slaveObject



49
50
51
52
53
# File 'lib/replica_set.rb', line 49

def next_slave
  ret = @slaves.shift
  @slaves.push ret
  ret
end