Skip to content

Commit 3620bb9

Browse files
Glyn Normingtonnebhale
authored andcommitted
After a DNS lookup failure, assume there is no internet connection.
This can occur in two places. If it occurs when sending a HEAD request against a version of an item in the application cache, mark the internet as unavailable and use the cached item. If it occurs when downloading an item, mark the internet as unavailable and look for the item in the buildpack cache. [#66409480]
1 parent b3f7290 commit 3620bb9

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/java_buildpack/util/cache/download_cache.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def download(mutable_file_cache, uri)
147147
fail(InferredNetworkFailure, "Unexpected HTTP response: #{response_code}")
148148
end
149149
end
150+
rescue => ex
151+
handle_failure(ex, 1, 1) {}
152+
false
150153
end
151154

152155
def file_cache(uri)
@@ -169,6 +172,7 @@ def http_options(rich_uri)
169172
end
170173

171174
def issue_http_request(request, uri, &block)
175+
@logger.debug { "HTTP.start(#{start_parameters(uri)})" }
172176
Net::HTTP.start(*start_parameters(uri)) do |http|
173177
retry_http_request(http, request, &block)
174178
end
@@ -256,6 +260,9 @@ def up_to_date_check(immutable_file_cache, uri)
256260
end
257261
end
258262
use_cache
263+
rescue => ex
264+
handle_failure(ex, 1, 1) {}
265+
false
259266
end
260267

261268
def use_ssl?(rich_uri)

lib/java_buildpack/util/cache/internet_availability.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def internet_available
6262
#
6363
# @param [String] reason a diagnostic which indicates why the internet should be deemed unavailable
6464
def internet_unavailable(reason)
65-
logger.error { reason } if internet_availability_stored?
65+
logger.error { "Internet unavailable: #{reason}. Buildpack cache will be used." } if internet_availability_stored?
6666
store_internet_availability false
6767
end
6868

spec/java_buildpack/util/cache/download_cache_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,31 @@
293293
expect_file_deleted 'lock'
294294
end
295295

296+
it 'should use the cached copy if the HEAD request cannot be made because, for example, the DNS is unavailable' do
297+
allow(Net::HTTP).to receive(:start).and_raise('DNS error')
298+
299+
touch app_dir, 'cached', 'foo-cached'
300+
touch app_dir, 'etag', 'foo-etag'
301+
touch app_dir, 'last_modified', 'foo-last-modified'
302+
303+
download_cache.get('http://foo-uri/') do |file|
304+
expect(file.read).to eq('foo-cached')
305+
end
306+
end
307+
296308
context do
297309
include_context 'buildpack_cache_helper'
298310

311+
it 'should use the buildpack cache if the download cannot be done because, for example, the DNS is unavailable' do
312+
allow(Net::HTTP).to receive(:start).and_raise('DNS error')
313+
314+
touch java_buildpack_cache_dir, 'cached', 'foo-stashed'
315+
316+
download_cache.get('http://foo-uri/') do |file|
317+
expect(file.read).to eq('foo-stashed')
318+
end
319+
end
320+
299321
it 'should use the buildpack cache if the download cannot be completed' do
300322
stub_request(:get, 'http://foo-uri/').to_raise(SocketError)
301323

0 commit comments

Comments
 (0)