Add exponential backoff for bitbucket server client
What does this MR do and why?
Adds exponential backoff for the Bitbucket Server client so that the request can be retried in case it fails. The request is retried with increasing delays and when the maximum retries are reached, an error is raised. Bitbucket Server uses the same exponential backoff code as Bitbucket Cloud because it uses the same mechanism.
The feature is behind a feature flag:
How to set up and validate locally
- Setup bitbucket server by following the instructions here
- Create a project and repo on BitBucket server.
- Enable the feature flag:
Feature.enable(:bitbucket_server_importer_exponential_backoff)
- On your
gdk
instance, create a new project > click onImport project
>Bitbucket Server
> follow instructions to connect to the server. - Import the project.
- In a rails console, grab the import credentials:
credentials = Project.last.import_data.credentials
- Test that the client still works by making an API request e.g.
BitbucketServer::Client.new(credentials).connection.get("/repos")
Simulate an error:
- Change line 37 in
lib/bitbucket_server/connection.rb
toraise OAuth2::Error.new("Rate limit for this resource has been exceeded")
- Reload and make the API request again. The request should hang a bit and then you should see a
Bitbucket::ExponentialBackoff::RateLimitError
error raised due to the maximum retries being exceeded. You will also see two logs intail -f log/importer.log
showingRetrying in x seconds
.
[Optional] Cause an error:
- Go to rate limit settings: http://localhost:7990/admin/rate-limit, enable rate limiting and set
Token bucket size
to2
andToken bucket refill rate
to1
. - Make multiple requests:
5.times { BitbucketServer::Client.new(credentials).connection.get("/repos") }
and note that on the fourth request, there is a delay of 5 seconds whereafter the request is retried and passes by viewing the logs:tail -f log/importer.log
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Related to #251222 (closed)
Edited by Madelein van Niekerk