Class: MinioRunner::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/minio_runner/network.rb

Defined Under Namespace

Classes: NetworkError

Constant Summary collapse

LONG_RESPONSE_TIME_SECONDS =
3
MAC_OS_LOCAL_DOMAIN_ERROR_MESSAGE =
<<~END
  For macOS, there are some issues which cause large delays for .local domain names. See
  https://superuser.com/a/1297335/245469 and https://stackoverflow.com/a/17982964/875941. To
  resolve this, you need to add IPV6 lookup addresses to the hosts file, and it helps to put
  all the entries on one line.

  ::1 minio.local testbucket.minio.local
  fe80::1%lo0 minio.local testbucket.minio.local
  127.0.0.1 minio.local testbucket.minio.local
END
MAX_REDIRECTS =
5

Class Method Summary collapse

Class Method Details

.download(url, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/minio_runner/network.rb', line 67

def download(url, &block)
  file_name = File.basename(url)
  tempfile =
    Tempfile.open(["", file_name], binmode: true) do |file|
      file.print Network.get(url)
      file
    end

  raise "Could not download #{url}" unless File.exist?(tempfile.to_path)

  MinioRunner.logger.debug("Successfully downloaded #{tempfile.to_path}")

  yield tempfile if block_given?
ensure
  tempfile&.close!
end

.get(url, redirect_limit: MAX_REDIRECTS) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/minio_runner/network.rb', line 26

def get(url, redirect_limit: MAX_REDIRECTS)
  MinioRunner.logger.debug("Making network call to #{url}")
  uri = URI(url)
  response = nil
  request_start_time = Time.now

  begin
    Net::HTTP.start(
      uri.host,
      uri.port,
      use_ssl: uri.scheme == "https",
      open_timeout: MinioRunner::System.mac? ? 10 : 3,
    ) { |http| response = http.get(uri.request_uri) }
  rescue SocketError, Net::OpenTimeout => err
    MinioRunner.logger.debug(
      "Connection error when checking minio server health: #{err.message}",
    )
    log_time_error(request_start_time)
    raise MinioRunner::Network::NetworkError.new(
            "Connection error, cannot reach URL: #{url} (#{err.class})",
          )
  end

  MinioRunner.logger.debug("Get response: #{response.inspect}")

  case response
  when Net::HTTPSuccess
    log_time_error(request_start_time)
    response.body
  when Net::HTTPRedirection
    raise MinioRunner::Network::NetworkError.new("Too many redirects for #{url}") if redirect_limit <= 0
    location = response["location"]
    MinioRunner.logger.debug("Following redirect to #{location}")
    get(location, redirect_limit: redirect_limit - 1)
  else
    raise MinioRunner::Network::NetworkError.new(
            "#{response.class::EXCEPTION_TYPE}: #{response.code} \"#{response.message}\" with #{url}",
          )
  end
end

.log_time_error(request_start_time) ⇒ Object



84
85
86
87
88
89
# File 'lib/minio_runner/network.rb', line 84

def log_time_error(request_start_time)
  if (Time.now - request_start_time) > Network::LONG_RESPONSE_TIME_SECONDS &&
       MinioRunner.config.minio_domain.end_with?(".local") && MinioRunner::System.mac?
    MinioRunner.logger.warn(MAC_OS_LOCAL_DOMAIN_ERROR_MESSAGE)
  end
end