Class: OsCtl::Repo::Downloader::Base
- Inherits:
-
Object
- Object
- OsCtl::Repo::Downloader::Base
- Defined in:
- lib/osctl/repo/downloader/base.rb
Constant Summary collapse
- DEFAULT_ATTEMPTS =
3- DEFAULT_WAIT =
5- NETWORK_EXCEPTIONS =
[ EOFError, IOError, SocketError, SystemCallError, Timeout::Error, OpenSSL::SSL::SSLError ].freeze
- DEFAULT_REQUEST_HEADERS =
{ 'Accept-Encoding' => 'identity' }.freeze
Instance Attribute Summary collapse
-
#repo ⇒ Object
readonly
protected
Returns the value of attribute repo.
Instance Method Summary collapse
- #connect ⇒ Object protected
- #index_uri ⇒ Object protected
-
#initialize(repo) ⇒ Base
constructor
A new instance of Base.
- #read_response_body(res) ⇒ Object protected
- #request_get(http, uri, headers = nil) ⇒ Object protected
- #response_content_length(res) ⇒ Object protected
- #retryable_http_code?(code) ⇒ Boolean protected
- #with_retries(attempts: DEFAULT_ATTEMPTS, wait: DEFAULT_WAIT, retry_if: nil) ⇒ Object protected
Constructor Details
#initialize(repo) ⇒ Base
Returns a new instance of Base.
8 9 10 |
# File 'lib/osctl/repo/downloader/base.rb', line 8 def initialize(repo) @repo = repo end |
Instance Attribute Details
#repo ⇒ Object (readonly, protected)
Returns the value of attribute repo.
14 15 16 |
# File 'lib/osctl/repo/downloader/base.rb', line 14 def repo @repo end |
Instance Method Details
#connect ⇒ Object (protected)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/osctl/repo/downloader/base.rb', line 29 def connect uri = URI(repo.url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == 'https') # Retry at the downloader boundary, which knows whether the caller has # already received data. Net::HTTP cannot safely replay a streamed GET. http.max_retries = 0 callback_error = nil http.start do |session| yield session rescue StandardError => e callback_error = e raise end rescue *NETWORK_EXCEPTIONS => e raise if callback_error.equal?(e) raise NetworkError, e end |
#index_uri ⇒ Object (protected)
50 51 52 |
# File 'lib/osctl/repo/downloader/base.rb', line 50 def index_uri URI(repo.index_url) end |
#read_response_body(res) ⇒ Object (protected)
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/osctl/repo/downloader/base.rb', line 93 def read_response_body(res) expected_length = response_content_length(res) actual_length = 0 callback_error = nil res.read_body do |fragment| next_length = actual_length + fragment.bytesize if expected_length && next_length > expected_length raise NetworkError, "downloaded more than #{expected_length} bytes" end begin yield fragment rescue StandardError => e callback_error = e raise end actual_length = next_length end return if expected_length.nil? || actual_length == expected_length raise NetworkError, "downloaded #{actual_length} bytes, expected #{expected_length}" rescue *NETWORK_EXCEPTIONS => e raise if callback_error.equal?(e) raise NetworkError, e end |
#request_get(http, uri, headers = nil) ⇒ Object (protected)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/osctl/repo/downloader/base.rb', line 75 def request_get(http, uri, headers = nil) callback_error = nil http.request_get( uri.request_uri, DEFAULT_REQUEST_HEADERS.merge(headers || {}) ) do |response| yield response rescue StandardError => e callback_error = e raise end rescue *NETWORK_EXCEPTIONS => e raise if callback_error.equal?(e) raise NetworkError, e end |
#response_content_length(res) ⇒ Object (protected)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/osctl/repo/downloader/base.rb', line 126 def response_content_length(res) fields = res.get_fields('content-length') return if fields.nil? lengths = fields.flat_map { |field| field.split(',') }.map(&:strip) unless lengths.length == 1 && lengths.first.match?(/\A[0-9]+\z/) raise NetworkError, 'invalid or duplicate Content-Length header' end transfer_encodings = Array(res.get_fields('transfer-encoding')) .flat_map { |field| field.split(',') } .map(&:strip) .reject(&:empty?) unless transfer_encodings.empty? raise NetworkError, 'response has both Content-Length and Transfer-Encoding' end Integer(lengths.first, 10) end |
#retryable_http_code?(code) ⇒ Boolean (protected)
146 147 148 |
# File 'lib/osctl/repo/downloader/base.rb', line 146 def retryable_http_code?(code) code == 429 || code >= 500 end |
#with_retries(attempts: DEFAULT_ATTEMPTS, wait: DEFAULT_WAIT, retry_if: nil) ⇒ Object (protected)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/osctl/repo/downloader/base.rb', line 54 def with_retries(attempts: DEFAULT_ATTEMPTS, wait: DEFAULT_WAIT, retry_if: nil) attempt = 0 begin attempt += 1 yield rescue BadHttpResponse => e raise if attempt >= attempts || !retryable_http_code?(e.code) raise if retry_if && !retry_if.call sleep(wait) retry rescue NetworkError => e raise if attempt >= attempts raise if retry_if && !retry_if.call sleep(wait) retry end end |