Class: Gitlab::Triage::RestAPINetwork
- Inherits:
-
Object
- Object
- Gitlab::Triage::RestAPINetwork
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
Instance Attribute Summary collapse
Attributes included from Retryable
#tries
Instance Method Summary
collapse
Methods included from Retryable
#execute_with_retry, #maximum_retries_reached?
Constructor Details
Returns a new instance of RestAPINetwork.
17
18
19
20
21
|
# File 'lib/gitlab/triage/rest_api_network.rb', line 17
def initialize(adapter)
@adapter = adapter
@options = adapter.options
@cache = {}
end
|
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
15
16
17
|
# File 'lib/gitlab/triage/rest_api_network.rb', line 15
def adapter
@adapter
end
|
#options ⇒ Object
Returns the value of attribute options.
15
16
17
|
# File 'lib/gitlab/triage/rest_api_network.rb', line 15
def options
@options
end
|
Instance Method Details
#delete_api(url) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/gitlab/triage/rest_api_network.rb', line 87
def delete_api(url)
response = execute_with_retry(
exception_types: Net::ReadTimeout,
backoff_exceptions: Errors::Network::TooManyRequests) 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)
rescue Net::ReadTimeout
{}
end
|
#post_api(url, body) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/gitlab/triage/rest_api_network.rb', line 63
def post_api(url, body)
response = execute_with_retry(
exception_types: Net::ReadTimeout,
backoff_exceptions: Errors::Network::TooManyRequests) 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
rescue Net::ReadTimeout
{}
end
|
#query_api(url) ⇒ Object
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
|
# File 'lib/gitlab/triage/rest_api_network.rb', line 27
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) 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
resources << results
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)
rescue Net::ReadTimeout
[]
end
|
#query_api_cached(url) ⇒ Object
23
24
25
|
# File 'lib/gitlab/triage/rest_api_network.rb', line 23
def query_api_cached(url)
@cache[url] || @cache[url] = query_api(url)
end
|
#raise_unexpected_response(results) ⇒ Object
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
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
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
|
#token ⇒ Object
104
105
106
|
# File 'lib/gitlab/triage/rest_api_network.rb', line 104
def token
options.token
end
|