Skip to content
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

Add lambda in executeRequest params to define a custom retry policy #4950

Closed

Conversation

Florian14
Copy link
Contributor

Previously, failed requests were retried according to a canRetry boolean passed in executeRequest method params. There is an exception for the 429 error with the M_LIMIT_EXCEEDED error code which were automatically retried without taking account of the canRetry flag.

The goal of this PR is to have more flexibility on the retry policy to be able to have a custom policy for a specific request. For example, a 429 error with a very long retry delay should no be retried but notify the user about the delay.

To keep the previous behaviour, the current retry policy has been kept and passed as the default value of the new canRetryOnFailure method parameter.

@github-actions
Copy link

github-actions bot commented Jan 13, 2022

Unit Test Results

  66 files  ±0    66 suites  ±0   50s ⏱️ -2s
135 tests ±0  135 ✔️ ±0  0 💤 ±0  0 ±0 
418 runs  ±0  418 ✔️ ±0  0 💤 ±0  0 ±0 

Results for commit 21fbf19. ± Comparison against base commit 668fa0d.

♻️ This comment has been updated with latest results.

@Florian14 Florian14 force-pushed the feature/fre/request_with_custom_retry_policy branch from a9361cc to 7c63264 Compare January 13, 2022 21:55
@github-actions
Copy link

github-actions bot commented Jan 13, 2022

Matrix SDK

Integration Tests Results:

  • [org.matrix.android.sdk.session]
    passed="21" failures="1" errors="0" skipped="2"
  • [org.matrix.android.sdk.account]
    passed="1" failures="1" errors="0" skipped="0"
  • [org.matrix.android.sdk.internal]
    passed="158" failures="1" errors="0" skipped="38"
  • [org.matrix.android.sdk.ordering]
    passed="16" failures="0" errors="0" skipped="0"
  • [org.matrix.android.sdk.PermalinkParserTest]
    passed="1" failures="1" errors="0" skipped="0"

Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first sight it looks good to me. I guess the next step is to use the new mechanism to change the login request?

currentDelay = currentDelay.times(2L).coerceAtMost(maxDelayBeforeRetry)
// Try again (loop)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe restore this comment, it is still valid.

} else if (canRetry && currentRetryCount < maxRetriesCount && exception.shouldBeRetried()) {
delay(currentDelay)
if (canRetryOnFailure(exception) && currentRetryCount < maxRetriesCount) {
delay(exception.getRetryDelay(currentDelay))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not strictly equivalent with the previous version, when we had a 429, the default delay was 1s. But I think it is still fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed, if we want to keep previous behaviour for 429, we should also return a fixed delay in the lambda (or null to use the default delay behaviour which is multiplied by 2 after each failure).

With my current implementation, receiving 4 successive 429 errors will lead to a total delay of 15 seconds (1 + 2 + 4 +8) instead of 4 seconds with the fixed delay. We should probably have to think about it. Was the previous behaviour originally expected for this special case ?

Note that the delay is passed as a default value in this special case, the delay passed in the 429 error body should be used, if any (it is not a mandatory field).

WDYT @bmarty?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, the point is that if you want to have an automatic retry in case of 429, except if the delay is too long (which could be configurable by the app), it will be more difficult with your change.
So maybe keep the two cases (429 and canRetry) clearly separated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. I close this PR and will create a new one.

@@ -15,22 +15,24 @@
*/

package org.matrix.android.sdk.internal.network

import org.matrix.android.sdk.internal.network.defaultRequestRetryPolicy as internalDefaultRequestRetryPolicy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think it is necessary to create an alias in this case.

@Florian14
Copy link
Contributor Author

At first sight it looks good to me. I guess the next step is to use the new mechanism to change the login request?

Not sure, this is the case of Tchap fork, but should we apply it for all login flows? I haven't tried to trigger the error using matrix.org yet.

@Florian14 Florian14 closed this Jan 17, 2022
@Florian14 Florian14 deleted the feature/fre/request_with_custom_retry_policy branch October 3, 2022 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants