Class: Algolia::Transport::RetryStrategy

Inherits:
Object
  • Object
show all
Includes:
RetryOutcomeType
Defined in:
lib/algolia/transport/retry_strategy.rb

Constant Summary

Constants included from RetryOutcomeType

RetryOutcomeType::FAILURE, RetryOutcomeType::RETRY, RetryOutcomeType::SUCCESS

Instance Method Summary collapse

Constructor Details

#initialize(hosts) ⇒ RetryStrategy

Returns a new instance of RetryStrategy.

Parameters:



8
9
10
11
# File 'lib/algolia/transport/retry_strategy.rb', line 8

def initialize(hosts)
  @hosts = hosts
  @lock = Mutex.new
end

Instance Method Details

#decide(tryable_host, http_response_code: nil, is_timed_out: false, network_failure: false) ⇒ Binary

Decides on the outcome of the request

Parameters:

  • tryable_host (StatefulHost)

    host to test against

  • http_response_code (Integer) (defaults to: nil)

    http response code

  • is_timed_out (Boolean) (defaults to: false)

    whether or not the request timed out

Returns:

  • (Binary)

    retry outcome code



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/algolia/transport/retry_strategy.rb', line 43

def decide(tryable_host, http_response_code: nil, is_timed_out: false, network_failure: false)
  @lock.synchronize do
    if !is_timed_out && success?(http_response_code)
      tryable_host.up = true
      tryable_host.last_use = Time.now.utc
      SUCCESS
    elsif !is_timed_out && retryable?(http_response_code, network_failure)
      tryable_host.up = false
      tryable_host.last_use = Time.now.utc
      RETRY
    elsif is_timed_out
      tryable_host.up = true
      tryable_host.last_use = Time.now.utc
      tryable_host.retry_count += 1
      RETRY
    else
      FAILURE
    end
  end
end

#get_tryable_hosts(call_type) ⇒ Array

Retrieves the tryable hosts

Parameters:

  • call_type (binary)

    type of the host

Returns:

  • (Array)

    list of StatefulHost



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/algolia/transport/retry_strategy.rb', line 19

def get_tryable_hosts(call_type)
  @lock.synchronize do
    reset_expired_hosts

    if @hosts.any? { |host| host.up && flag?(host.accept, call_type) }
      @hosts.select { |host| host.up && flag?(host.accept, call_type) }
    else
      @hosts.each do |host|
        reset(host) if flag?(host.accept, call_type)
      end

      @hosts
    end
  end
end