Class: Gitlab::Triage::RestAPINetwork

Inherits:
Object
  • Object
show all
Includes:
Retryable
Defined in:
lib/gitlab/triage/rest_api_network.rb

Constant Summary collapse

MINIMUM_RATE_LIMIT =
25

Constants included from Retryable

Gitlab::Triage::Retryable::BACK_OFF_SECONDS, Gitlab::Triage::Retryable::MAX_RETRIES, Gitlab::Triage::Retryable::RETRY_WAIT_SECONDS

Instance Attribute Summary collapse

Attributes included from Retryable

#tries

Instance Method Summary collapse

Methods included from Retryable

#execute_with_retry, #maximum_retries_reached?, #puts_execute_with_retry_message

Constructor Details

#initialize(adapter) ⇒ RestAPINetwork

Returns a new instance of RestAPINetwork.



19
20
21
22
23
# File 'lib/gitlab/triage/rest_api_network.rb', line 19

def initialize(adapter)
  @adapter = adapter
  @options = adapter.options
  @cache = {}
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



17
18
19
# File 'lib/gitlab/triage/rest_api_network.rb', line 17

def adapter
  @adapter
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/gitlab/triage/rest_api_network.rb', line 17

def options
  @options
end

Instance Method Details

#delete_api(url) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitlab/triage/rest_api_network.rb', line 89

def delete_api(url)
  response = execute_with_retry(
    exception_types: Net::ReadTimeout,
    backoff_exceptions: Errors::Network::TooManyRequests, debug: options.debug) do
    puts Gitlab::Triage::UI.debug "delete_api: #{url}" if options.debug

    @adapter.delete(token, url)
  end

  rate_limit_debug(response) if options.debug
  rate_limit_wait(response)
end

#post_api(url, body) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gitlab/triage/rest_api_network.rb', line 67

def post_api(url, body)
  response = execute_with_retry(
    exception_types: Net::ReadTimeout,
    backoff_exceptions: Errors::Network::TooManyRequests, debug: options.debug) do
    puts Gitlab::Triage::UI.debug "post_api: #{url}" if options.debug

    @adapter.post(token, url, body)
  end

  rate_limit_debug(response) if options.debug
  rate_limit_wait(response)

  results = response.delete(:results)

  case results
  when Hash
    results.with_indifferent_access
  else
    raise_unexpected_response(results)
  end
end

#query_api(url) ⇒ Object



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/gitlab/triage/rest_api_network.rb', line 29

def query_api(url)
  response = {}
  resources = []

  begin
    print '.'
    url = response.fetch(:next_page_url) { url }

    response = execute_with_retry(
      exception_types: [Net::ReadTimeout, Errors::Network::InternalServerError],
      backoff_exceptions: Errors::Network::TooManyRequests, debug: options.debug) do
      puts Gitlab::Triage::UI.debug "query_api: #{url}" if options.debug

      @adapter.get(token, url)
    end

    results = response.delete(:results)

    case results
    when Array
      resources.concat(results)
    when Hash
      if results['message']&.match?(/404 Group|Project Not Found/)
        raise_unexpected_response(results)
      else
        resources << results
      end
    else
      raise_unexpected_response(results)
    end

    rate_limit_debug(response) if options.debug
    rate_limit_wait(response)
  end while response.delete(:more_pages)

  resources.map!(&:with_indifferent_access)
end

#query_api_cached(url) ⇒ Object



25
26
27
# File 'lib/gitlab/triage/rest_api_network.rb', line 25

def query_api_cached(url)
  @cache[url] || @cache[url] = query_api(url)
end

#raise_unexpected_response(results) ⇒ Object (private)



120
121
122
# File 'lib/gitlab/triage/rest_api_network.rb', line 120

def raise_unexpected_response(results)
  raise Errors::Network::UnexpectedResponse, "Unexpected response: #{results.inspect}"
end

#rate_limit_debug(response) ⇒ Object (private)



108
109
110
111
# File 'lib/gitlab/triage/rest_api_network.rb', line 108

def rate_limit_debug(response)
  rate_limit_infos = "Rate limit remaining: #{response[:ratelimit_remaining]} (reset at #{response[:ratelimit_reset_at]})"
  puts Gitlab::Triage::UI.debug "rate_limit_infos: #{rate_limit_infos}"
end

#rate_limit_wait(response) ⇒ Object (private)



113
114
115
116
117
118
# File 'lib/gitlab/triage/rest_api_network.rb', line 113

def rate_limit_wait(response)
  return unless response.delete(:ratelimit_remaining) < MINIMUM_RATE_LIMIT

  puts Gitlab::Triage::UI.debug "Rate limit almost exceeded, sleeping for #{response[:ratelimit_reset_at] - Time.now} seconds" if options.debug
  sleep(1) until Time.now >= response[:ratelimit_reset_at]
end

#tokenObject (private)



104
105
106
# File 'lib/gitlab/triage/rest_api_network.rb', line 104

def token
  options.token
end