Class: RightScale::PendingRequests

Inherits:
Hash
  • Object
show all
Defined in:
lib/right_agent/pending_requests.rb

Overview

Cache for requests that are waiting for a response Automatically deletes push requests when get too old Retains non-push requests until explicitly deleted

Constant Summary collapse

MAX_PUSH_AGE =

Maximum number of seconds to retain send pushes in cache

2 * 60
MIN_CLEANUP_INTERVAL =

Minimum number of seconds between push cleanups

15

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePendingRequests

Create cache



65
66
67
68
# File 'lib/right_agent/pending_requests.rb', line 65

def initialize
  @last_cleanup = Time.now
  super
end

Class Method Details

.oldest_age(pending_requests) ⇒ Object

Get age of oldest pending request

Parameters

pending_requests(Hash)

Pending requests to be examined

Return

age(Integer)

Age of oldest request



122
123
124
125
126
127
128
129
130
# File 'lib/right_agent/pending_requests.rb', line 122

def self.oldest_age(pending_requests)
  now = Time.now
  age = nil
  pending_requests.each_value do |r|
    seconds = (now - r.receive_time).to_i
    age = seconds if age.nil? || seconds > age
  end
  age
end

.youngest_age(pending_requests) ⇒ Object

Get age of youngest pending request

Parameters

pending_requests(Hash)

Pending requests to be examined

Return

age(Integer)

Age of youngest request



105
106
107
108
109
110
111
112
113
# File 'lib/right_agent/pending_requests.rb', line 105

def self.youngest_age(pending_requests)
  now = Time.now
  age = nil
  pending_requests.each_value do |r|
    seconds = (now - r.receive_time).to_i
    age = seconds if age.nil? || seconds < age
  end
  age
end

Instance Method Details

#[]=(token, pending_request) ⇒ Object

Store pending request

Parameters

token(String)

Generated message identifier

pending_request(PendingRequest)

Pending request

Return

(PendingRequest)

Stored request



78
79
80
81
82
83
84
85
# File 'lib/right_agent/pending_requests.rb', line 78

def []=(token, pending_request)
  now = Time.now
  if (now - @last_cleanup) > MIN_CLEANUP_INTERVAL
    self.reject! { |t, r| r.kind == :send_push && (now - r.receive_time) > MAX_PUSH_AGE }
    @last_cleanup = now
  end
  super
end

#kind(kind) ⇒ Object

Select cache entries of the given kind

Parameters

kind(Symbol)

Kind of request to be included: :send_push or :send_request

Return

(Hash)

Requests of specified kind



94
95
96
# File 'lib/right_agent/pending_requests.rb', line 94

def kind(kind)
  self.reject { |t, r| r.kind != kind}
end