Class: Cassandra::LoadBalancing::Policies::WhiteList

Inherits:
Cassandra::LoadBalancing::Policy show all
Extended by:
Forwardable
Defined in:
lib/cassandra/load_balancing/policies/white_list.rb

Instance Method Summary collapse

Methods inherited from Cassandra::LoadBalancing::Policy

#setup, #teardown

Constructor Details

#initialize(ips, wrapped_policy) ⇒ WhiteList

Returns a new instance of WhiteList.

Parameters:

Raises:

  • (ArgumentError)

    if arguments are of unexpected types



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cassandra/load_balancing/policies/white_list.rb', line 37

def initialize(ips, wrapped_policy)
  Util.assert_instance_of(::Enumerable, ips) do
    "ips must be an Enumerable, #{ips.inspect} given"
  end
  methods = [:host_up, :host_down, :host_found, :host_lost, :setup, :teardown,
             :distance, :plan]
  Util.assert_responds_to_all(methods, wrapped_policy) do
    "supplied policy must respond to #{methods.inspect}, but doesn't"
  end

  @ips    = ::Set.new
  @policy = wrapped_policy

  ips.each do |ip|
    case ip
    when ::IPAddr
      @ips << ip
    when ::String
      @ips << ::IPAddr.new(ip)
    else
      raise ::ArgumentError, 'each ip must be a String or IPAddr, ' \
          "#{ip.inspect} given"
    end
  end
end

Instance Method Details

#distance(host) ⇒ Object

Delegates to wrapped policy



32
# File 'lib/cassandra/load_balancing/policies/white_list.rb', line 32

def_delegators :@policy, :plan, :distance

#host_down(host) ⇒ Object

Delegates to wrapped policy if host's ip is whitelisted

Parameters:

See Also:



87
88
89
# File 'lib/cassandra/load_balancing/policies/white_list.rb', line 87

def host_down(host)
  @policy.host_down(host) if @ips.include?(host.ip)
end

#host_found(host) ⇒ Object

Delegates to wrapped policy if host's ip is whitelisted

Parameters:

See Also:



66
67
68
# File 'lib/cassandra/load_balancing/policies/white_list.rb', line 66

def host_found(host)
  @policy.host_found(host) if @ips.include?(host.ip)
end

#host_lost(host) ⇒ Object

Delegates to wrapped policy if host's ip is whitelisted

Parameters:

See Also:



73
74
75
# File 'lib/cassandra/load_balancing/policies/white_list.rb', line 73

def host_lost(host)
  @policy.host_lost(host) if @ips.include?(host.ip)
end

#host_up(host) ⇒ Object

Delegates to wrapped policy if host's ip is whitelisted

Parameters:

See Also:



80
81
82
# File 'lib/cassandra/load_balancing/policies/white_list.rb', line 80

def host_up(host)
  @policy.host_up(host) if @ips.include?(host.ip)
end

#plan(keyspace, statement, options) ⇒ Object

Delegates to wrapped policy



32
# File 'lib/cassandra/load_balancing/policies/white_list.rb', line 32

def_delegators :@policy, :plan, :distance