Class: Cequel::Metal::Policy::CassandraError::ClearAndRetryPolicy

Inherits:
ErrorPolicyBase
  • Object
show all
Defined in:
lib/cequel/metal/policy/cassandra_error.rb

Overview

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ClearAndRetryPolicy

Returns a new instance of ClearAndRetryPolicy.

Since:

  • 1.0.0



34
35
36
37
38
39
40
41
42
# File 'lib/cequel/metal/policy/cassandra_error.rb', line 34

def initialize(options = {})
  @max_retries = options.fetch(:max_retries, 3)
  @retry_delay = options.fetch(:retry_delay, 0.5)
  @clear_before_retry = !!options.fetch(:clear_before_retry, true)
  
  if @retry_delay <= 0.0
    raise ArgumentError, "The value for retry must be a positive number, not '#{@retry_delay}'"
  end
end

Instance Attribute Details

#clear_before_retryObject (readonly)

Returns Boolean if this policy clears connections before retry.

Returns:

  • Boolean if this policy clears connections before retry

Since:

  • 1.0.0



33
34
35
# File 'lib/cequel/metal/policy/cassandra_error.rb', line 33

def clear_before_retry
  @clear_before_retry
end

#max_retriesObject (readonly)

Returns Integer maximum number of retries to reconnect to Cassandra.

Returns:

  • Integer maximum number of retries to reconnect to Cassandra

Since:

  • 1.0.0



29
30
31
# File 'lib/cequel/metal/policy/cassandra_error.rb', line 29

def max_retries
  @max_retries
end

#retry_delayObject (readonly)

Returns Float delay between retries to reconnect to Cassandra.

Returns:

  • Float delay between retries to reconnect to Cassandra

Since:

  • 1.0.0



31
32
33
# File 'lib/cequel/metal/policy/cassandra_error.rb', line 31

def retry_delay
  @retry_delay
end

Instance Method Details

#execute_stmt(keyspace) ⇒ Object

Since:

  • 1.0.0



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cequel/metal/policy/cassandra_error.rb', line 44

def execute_stmt(keyspace)
  retries_remaining = max_retries
  begin
    yield
  rescue Cassandra::Errors::NoHostsAvailable,
        Cassandra::Errors::ExecutionError,
        Cassandra::Errors::TimeoutError => error
    raise error if retries_remaining == 0
    sleep(retry_delay)
    keyspace.clear_active_connections! if clear_before_retry
    retries_remaining -= 1
    retry                    
  end
end