-
Notifications
You must be signed in to change notification settings - Fork 675
feat(client): automatically retry on HTTP 409 Resource lock #2326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -779,10 +779,20 @@ def http_request( | |
| if 200 <= result.status_code < 300: | ||
| return result | ||
|
|
||
| if (429 == result.status_code and obey_rate_limit) or ( | ||
| result.status_code in gitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES | ||
| and retry_transient_errors | ||
| ): | ||
| def should_retry() -> bool: | ||
| if result.status_code == 429 and obey_rate_limit: | ||
| return True | ||
|
|
||
| if not retry_transient_errors: | ||
| return False | ||
| if result.status_code in gitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES: | ||
| return True | ||
| if result.status_code == 409 and "Resource lock" in result.reason: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't seem to work for us - could it be because |
||
| return True | ||
|
|
||
| return False | ||
|
|
||
| if should_retry(): | ||
| # Response headers documentation: | ||
| # https://docs.gitlab.com/ee/user/admin_area/settings/user_and_ip_rate_limits.html#response-headers | ||
| if max_retries == -1 or cur_retries < max_retries: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -372,6 +372,63 @@ def response_callback( | |
| assert "http://example.com/api/v4/user/status" in error_message | ||
|
|
||
|
|
||
| def test_http_request_on_409_resource_lock_retries(gl_retry): | ||
| url = "http://localhost/api/v4/user" | ||
| retried = False | ||
|
|
||
| def response_callback( | ||
| response: requests.models.Response, | ||
nejch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) -> requests.models.Response: | ||
| """We need a callback that adds a resource lock reason only on first call""" | ||
| nonlocal retried | ||
|
|
||
| if not retried: | ||
| response.reason = "Resource lock" | ||
|
|
||
| retried = True | ||
| return response | ||
|
Comment on lines
+379
to
+389
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're mostly doing this because |
||
|
|
||
| with responses.RequestsMock(response_callback=response_callback) as rsps: | ||
| rsps.add( | ||
| method=responses.GET, | ||
| url=url, | ||
| status=409, | ||
| match=helpers.MATCH_EMPTY_QUERY_PARAMS, | ||
| ) | ||
| rsps.add( | ||
nejch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| method=responses.GET, | ||
| url=url, | ||
| status=200, | ||
| match=helpers.MATCH_EMPTY_QUERY_PARAMS, | ||
| ) | ||
| response = gl_retry.http_request("get", "/user") | ||
|
|
||
| assert response.status_code == 200 | ||
|
|
||
|
|
||
| def test_http_request_on_409_resource_lock_without_retry_raises(gl): | ||
| url = "http://localhost/api/v4/user" | ||
|
|
||
| def response_callback( | ||
| response: requests.models.Response, | ||
| ) -> requests.models.Response: | ||
| """Without retry, this will fail on the first call""" | ||
| response.reason = "Resource lock" | ||
| return response | ||
|
|
||
| with responses.RequestsMock(response_callback=response_callback) as req_mock: | ||
| req_mock.add( | ||
| method=responses.GET, | ||
| url=url, | ||
| status=409, | ||
| match=helpers.MATCH_EMPTY_QUERY_PARAMS, | ||
| ) | ||
| with pytest.raises(GitlabHttpError) as excinfo: | ||
| gl.http_request("get", "/user") | ||
|
|
||
| assert excinfo.value.response_code == 409 | ||
|
|
||
|
|
||
| @responses.activate | ||
| def test_get_request(gl): | ||
| url = "http://localhost/api/v4/projects" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.