Skip to content

Commit

Permalink
Merge pull request #58 from fanhattan/verify_role-optional
Browse files Browse the repository at this point in the history
Make node role verification optional via :verify_role option.
  • Loading branch information
ryanlecompte committed Oct 15, 2013
2 parents 15778c7 + 565f598 commit 6d9754d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -151,6 +151,7 @@ The full set of options that can be passed to RedisFailover::Client are:
:max_retries - max retries for a failure (default 3)
:safe_mode - indicates if safe mode is used or not (default true)
:master_only - indicates if only redis master is used (default false)
:verify_role - verify the actual role of a redis node before every command (default true)

The RedisFailover::Client also supports a custom callback that will be invoked whenever the list of redis clients changes. Example usage:

Expand Down
6 changes: 4 additions & 2 deletions lib/redis_failover/client.rb
Expand Up @@ -56,6 +56,7 @@ def call(command, &block)
# @option options [Integer] :max_retries max retries for a failure
# @option options [Boolean] :safe_mode indicates if safe mode is used or not
# @option options [Boolean] :master_only indicates if only redis master is used
# @option options [Boolean] :verify_role verify the actual role of a redis node before every command
# @note Use either :zkservers or :zk
# @return [RedisFailover::Client]
def initialize(options = {})
Expand Down Expand Up @@ -258,7 +259,7 @@ def dispatch(method, *args, &block)
# @raise [NoMasterError] if no master is available
def master
if master = @lock.synchronize { @master }
verify_role!(master, :master)
verify_role!(master, :master) if @verify_role
return master
end
raise NoMasterError
Expand All @@ -272,7 +273,7 @@ def master
def slave
# pick a slave, if none available fallback to master
if slave = @lock.synchronize { @slaves.shuffle.first }
verify_role!(slave, :slave)
verify_role!(slave, :slave) if @verify_role
return slave
end
master
Expand Down Expand Up @@ -501,6 +502,7 @@ def parse_options(options)
@max_retries = @retry ? options.fetch(:max_retries, 3) : 0
@safe_mode = options.fetch(:safe_mode, true)
@master_only = options.fetch(:master_only, false)
@verify_role = options.fetch(:verify_role, true)
end

# @return [String] the znode path for the master redis nodes config
Expand Down
8 changes: 5 additions & 3 deletions spec/client_spec.rb
Expand Up @@ -120,9 +120,11 @@ def fetch_nodes
client.reconnected.should be_true
end

it 'properly detects when a node has changed roles' do
client.current_master.change_role_to('slave')
expect { client.send(:master) }.to raise_error(InvalidNodeRoleError)
context 'with :verify_role true' do
it 'properly detects when a node has changed roles' do
client.current_master.change_role_to('slave')
expect { client.send(:master) }.to raise_error(InvalidNodeRoleError)
end
end

it 'raises error for unsupported operations' do
Expand Down

0 comments on commit 6d9754d

Please sign in to comment.