Skip to content

Commit

Permalink
feat: Provide retrieval_monotonic_time on compute metadata response o…
Browse files Browse the repository at this point in the history
…bjects (#62)
  • Loading branch information
dazuma committed Dec 12, 2023
1 parent d0e94f5 commit 5857e55
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/google/cloud/env/compute_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class ComputeMetadata
# {ComputeMetadata#lookup_response}.
#
# This object duck-types the `status`, `body`, and `headers` fields of
# `Faraday::Response`.
# `Faraday::Response`. It also includes the CLOCK_MONOTONIC time when
# the data was retrieved.
#
class Response
##
Expand All @@ -105,6 +106,7 @@ def initialize status, body, headers
@status = status
@body = body
@headers = headers
@retrieval_monotonic_time = Process.clock_gettime Process::CLOCK_MONOTONIC
end

##
Expand All @@ -125,6 +127,11 @@ def initialize status, body, headers
#
attr_reader :headers

# The CLOCK_MONOTONIC time at which this response was retrieved.
# @return [Numeric]
#
attr_reader :retrieval_monotonic_time

##
# Returns true if the metadata-flavor is correct for Google Cloud
# @return [boolean]
Expand Down Expand Up @@ -752,9 +759,9 @@ def internal_lookup path, query, open_timeout, request_timeout
end
response = Response.new http_response.status, http_response.body, http_response.headers
if path.nil?
post_update_existence(response.status == 200 && response.google_flavor?)
post_update_existence(response.status == 200 && response.google_flavor?, response.retrieval_monotonic_time)
elsif response.google_flavor?
post_update_existence true
post_update_existence true, response.retrieval_monotonic_time
end
lifetime = determine_data_lifetime path, response.body.strip
LazyValue.expiring_value lifetime, response
Expand All @@ -767,14 +774,14 @@ def internal_lookup path, query, open_timeout, request_timeout
# @private
# Update existence based on a received result
#
def post_update_existence success
def post_update_existence success, current_time = nil
return if @existence == :confirmed
@mutex.synchronize do
if success
@existence = :confirmed
elsif @existence != :confirmed &&
Process.clock_gettime(Process::CLOCK_MONOTONIC) > @startup_time + warmup_time
@existence = :no
elsif @existence != :confirmed
current_time ||= Process.clock_gettime Process::CLOCK_MONOTONIC
@existence = :no if current_time > @startup_time + warmup_time
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/google/cloud/env/compute_metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
response = compute_metadata.lookup_response "project/project-id"
assert_equal project_id, response.body
assert_equal 200, response.status
assert_in_delta Process.clock_gettime(Process::CLOCK_MONOTONIC), response.retrieval_monotonic_time, 0.1
end

it "retries a lookup" do
Expand Down Expand Up @@ -98,13 +99,17 @@
[200, flavor_header, project_id]
end
end
current_time = Process.clock_gettime Process::CLOCK_MONOTONIC
response1 = compute_metadata.lookup_response "project/project-id"
assert_equal project_id, response1.body
assert_equal 200, response1.status
assert_in_delta current_time, response1.retrieval_monotonic_time, 0.1
assert_equal 1, lookup_count
sleep 0.5
response2 = compute_metadata.lookup_response "project/project-id"
assert_equal project_id, response2.body
assert_equal 200, response2.status
assert_in_delta current_time, response1.retrieval_monotonic_time, 0.1
assert_equal 1, lookup_count
end

Expand Down

0 comments on commit 5857e55

Please sign in to comment.