Class: OsCtl::Repo::Downloader::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/osctl/repo/downloader/base.rb

Direct Known Subclasses

Cached, Direct

Constant Summary collapse

DEFAULT_ATTEMPTS =
3
DEFAULT_WAIT =
5
NETWORK_EXCEPTIONS =
[
  EOFError,
  IOError,
  SocketError,
  SystemCallError,
  Timeout::Error,
  OpenSSL::SSL::SSLError
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#repoObject (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

#connectObject (protected)



25
26
27
28
29
30
31
32
# File 'lib/osctl/repo/downloader/base.rb', line 25

def connect(&)
  uri = URI(repo.url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')
  http.start(&)
rescue *NETWORK_EXCEPTIONS => e
  raise NetworkError, e
end

#index_uriObject (protected)



34
35
36
# File 'lib/osctl/repo/downloader/base.rb', line 34

def index_uri
  URI(repo.index_url)
end

#request_get(http, uri, headers = nil) ⇒ Object (protected)



57
58
59
60
61
62
63
64
65
# File 'lib/osctl/repo/downloader/base.rb', line 57

def request_get(http, uri, headers = nil, &)
  if headers
    http.request_get(uri.request_uri, headers, &)
  else
    http.request_get(uri.request_uri, &)
  end
rescue *NETWORK_EXCEPTIONS => e
  raise NetworkError, e
end

#retryable_http_code?(code) ⇒ Boolean (protected)

Returns:

  • (Boolean)


67
68
69
# File 'lib/osctl/repo/downloader/base.rb', line 67

def retryable_http_code?(code)
  code == 429 || code >= 500
end

#with_retries(attempts: DEFAULT_ATTEMPTS, wait: DEFAULT_WAIT) ⇒ Object (protected)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/osctl/repo/downloader/base.rb', line 38

def with_retries(attempts: DEFAULT_ATTEMPTS, wait: DEFAULT_WAIT)
  attempt = 0

  begin
    attempt += 1
    yield
  rescue BadHttpResponse => e
    raise if attempt >= attempts || !retryable_http_code?(e.code)

    sleep(wait)
    retry
  rescue NetworkError => e
    raise if attempt >= attempts

    sleep(wait)
    retry
  end
end