Skip to content

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

  1. Setup bitbucket server by following the instructions here
  2. Create a project and repo on BitBucket server.
  3. Enable the feature flag: Feature.enable(:bitbucket_server_importer_exponential_backoff)
  4. On your gdk instance, create a new project > click on Import project > Bitbucket Server > follow instructions to connect to the server.
  5. Import the project.
  6. In a rails console, grab the import credentials: credentials = Project.last.import_data.credentials
  7. Test that the client still works by making an API request e.g. BitbucketServer::Client.new(credentials).connection.get("/repos")

Simulate an error:

  1. Change line 37 in lib/bitbucket_server/connection.rb to raise OAuth2::Error.new("Rate limit for this resource has been exceeded")
  2. 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 in tail -f log/importer.log showing Retrying in x seconds.

[Optional] Cause an error:

  1. Go to rate limit settings: http://localhost:7990/admin/rate-limit, enable rate limiting and set Token bucket size to 2 and Token bucket refill rate to 1.
  2. 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.

Related to #251222 (closed)

Edited by Madelein van Niekerk

Merge request reports