Class: LoadBalancedRestClient::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}) ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
10
11
# File 'lib/server.rb', line 5

def initialize(url, opts={})
  @url                 = url
  @client              = opts[:client_klass]       || ::RestClient::Resource.new(url, opts)
  @max_downtime        = opts[:max_downtime]       || 60 * 60 #= 1 hour
  @downtime_algorithm  = opts[:downtime_algorithm] || ::LoadBalancedRestClient::Algorithms::ExponentialDowntime.new
  @downtime_counter    = 0
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/server.rb', line 3

def client
  @client
end

#downtime_counterObject

Returns the value of attribute downtime_counter.



3
4
5
# File 'lib/server.rb', line 3

def downtime_counter
  @downtime_counter
end

#max_downtimeObject

Returns the value of attribute max_downtime.



3
4
5
# File 'lib/server.rb', line 3

def max_downtime
  @max_downtime
end

Instance Method Details

#mark_down!Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/server.rb', line 20

def mark_down!
  if should_try?
    downtime = next_downtime
    mark_down_for! downtime

    downtime
  else
    false
  end
end

#mark_down_for!(time_amount) ⇒ Object



13
14
15
16
17
18
# File 'lib/server.rb', line 13

def mark_down_for!(time_amount)
  @downtime_counter += 1
  @marked_down_until = Time.now + with_max_downtime { time_amount }

  true
end

#marked_down?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/server.rb', line 41

def marked_down?
  !@marked_down_until.nil? && @marked_down_until > Time.now
end

#next_downtimeObject



45
46
47
48
49
# File 'lib/server.rb', line 45

def next_downtime
  with_max_downtime do
    @downtime_algorithm.call(@downtime_counter + 1)
  end
end

#should_try?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/server.rb', line 37

def should_try?
  !marked_down?
end

#stop_marking_down!Object



31
32
33
34
35
# File 'lib/server.rb', line 31

def stop_marking_down!
  @marked_down_until = nil
  @downtime_counter  = 0
  true
end

#to_sObject



51
52
53
# File 'lib/server.rb', line 51

def to_s
  @url
end