Skip to content

Commit f4479a9

Browse files
committed
Merge branch 'master' into update-specs-with-all-packages
2 parents bf5b200 + 0ef1b53 commit f4479a9

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

‎lib/partial/remote.rb‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ def initialize(repo, locale, name, vars, repos_cfg)
1515
@vars = vars
1616
end
1717

18+
MAX_ATTEMPTS = 5
19+
1820
def content
19-
text = URI.open(@url).read
21+
text = fetch
2022
# Rewrite relative image paths to absolute GitHub URLs so they render outside their source repo
2123
text.scan(/\!\[.*\]\((.*)\)/).flatten
2224
.reject { |ip| ip.start_with?('http') }
@@ -25,5 +27,26 @@ def content
2527
.each { |ip| text.gsub!(ip, "#{@base_url}/#{ip}") }
2628
text
2729
end
30+
31+
private
32+
33+
# raw.githubusercontent.com intermittently answers concurrent requests with
34+
# 5xx errors (e.g. "501 First fragment: Transfer-Encoding not supported"),
35+
# so retry transient failures with exponential backoff
36+
def fetch
37+
attempt = 0
38+
begin
39+
attempt += 1
40+
URI.open(@url).read
41+
rescue OpenURI::HTTPError, SocketError, Timeout::Error, SystemCallError, OpenSSL::SSL::SSLError => e
42+
status = e.respond_to?(:io) ? e.io.status.first.to_i : nil
43+
transient = status.nil? || status == 429 || status >= 500
44+
raise unless transient && attempt < MAX_ATTEMPTS
45+
46+
warn "retrying #{@url} (attempt #{attempt} of #{MAX_ATTEMPTS} failed): #{e.message}"
47+
sleep(2**attempt)
48+
retry
49+
end
50+
end
2851
end
2952
end

‎lib/partial_cache.rb‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
11
# frozen_string_literal: true
22

33
class PartialCache
4+
POOL_SIZE = 20
5+
46
def initialize(localized_builds)
5-
@cache = localized_builds
7+
partials = localized_builds
68
.flat_map(&:partials)
79
.uniq { |partial| partial.url }
810
.sort_by { |partial| partial.url }
9-
.map { |partial| Thread.new { [partial.url, partial.content] } }
10-
.map { |thread| thread.value }
11-
.tap { |results| results.each { |url, _| warn "fetched #{url}" } }
11+
12+
@cache = fetch_all(partials)
13+
.tap { |results| results.each { |url, _| warn "fetched #{url}" } }
1214
.to_h
1315
end
1416

1517
def [](url)
1618
@cache[url]
1719
end
20+
21+
private
22+
23+
# Fetch partials with at most POOL_SIZE concurrent requests
24+
def fetch_all(partials)
25+
queue = Queue.new
26+
partials.each { |partial| queue << partial }
27+
queue.close
28+
29+
results = Array.new(partials.size)
30+
index = partials.each_with_index.to_h { |partial, i| [partial.url, i] }
31+
32+
Array.new([POOL_SIZE, partials.size].min) {
33+
Thread.new {
34+
while (partial = queue.pop)
35+
results[index[partial.url]] = [partial.url, partial.content]
36+
end
37+
}
38+
}.each(&:join)
39+
40+
results
41+
end
1842
end

0 commit comments

Comments
 (0)