Class: RedCluster::ReplicaSet
- Inherits:
-
Object
- Object
- RedCluster::ReplicaSet
- Defined in:
- lib/replica_set.rb
Instance Attribute Summary collapse
-
#master ⇒ Object
readonly
Returns the value of attribute master.
-
#slaves ⇒ Object
readonly
Returns the value of attribute slaves.
Instance Method Summary collapse
- #exec ⇒ Object
-
#initialize(cluster, options) ⇒ ReplicaSet
constructor
A new instance of ReplicaSet.
- #method_missing(command, *args) ⇒ Object
- #multi ⇒ Object
- #next_slave ⇒ Object
Constructor Details
#initialize(cluster, options) ⇒ ReplicaSet
Returns a new instance of ReplicaSet.
5 6 7 8 9 10 |
# File 'lib/replica_set.rb', line 5 def initialize(cluster, ) @my_cluster = cluster @master = Redis.new [:master] @slaves = [: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
#master ⇒ Object (readonly)
Returns the value of attribute master.
3 4 5 |
# File 'lib/replica_set.rb', line 3 def master @master end |
#slaves ⇒ Object (readonly)
Returns the value of attribute slaves.
3 4 5 |
# File 'lib/replica_set.rb', line 3 def slaves @slaves end |
Instance Method Details
#exec ⇒ Object
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 |
#multi ⇒ Object
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_slave ⇒ Object
49 50 51 52 53 |
# File 'lib/replica_set.rb', line 49 def next_slave ret = @slaves.shift @slaves.push ret ret end |