-
Notifications
You must be signed in to change notification settings - Fork 30
New feature for retrying request after failed #26
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
base: develop
Are you sure you want to change the base?
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 |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. | ||
| */ | ||
|
|
||
| package dev.icerock.moko.network.features | ||
|
|
||
| import io.ktor.client.HttpClient | ||
| import io.ktor.client.features.HttpClientFeature | ||
| import io.ktor.client.request.HttpRequestBuilder | ||
| import io.ktor.client.request.HttpSendPipeline | ||
| import io.ktor.client.statement.HttpStatement | ||
| import io.ktor.util.AttributeKey | ||
| import io.ktor.utils.io.errors.IOException | ||
| import kotlinx.coroutines.delay | ||
|
|
||
| /** | ||
| @param delayGetter the function for calculation a delay between failed request and next request | ||
| @param maxAmountRetrying the max amounts retrying requests | ||
| @param isShouldRetryRequest the function for condition a retrying request. By default it check `e is IOException ` | ||
| */ | ||
| class HttpRetrySendFeature( | ||
| private val delayGetter: (lastDelayInMillisecond: Long, timeRetrying: Int) -> Long, | ||
| private val maxAmountsRetrying: Int, | ||
| private val isShouldRetryRequest: (e: Throwable) -> Boolean | ||
| ) { | ||
|
|
||
| class Config { | ||
| var onGetDelay: (lastDelayInMillisecond: Long, timeRetrying: Int) -> Long = | ||
| { _, _ -> 2_000L } | ||
| var maxAmountRetrying: Int = 3 // first request + three retrying requests, | ||
| var onConditionRetrying: (e: Throwable) -> Boolean = { e -> e is IOException } | ||
| fun build() = HttpRetrySendFeature(onGetDelay, maxAmountRetrying, onConditionRetrying) | ||
| } | ||
|
|
||
| companion object Feature : HttpClientFeature<Config, HttpRetrySendFeature> { | ||
| private const val LAST_DELAY_HEADER = "HttpRetrySendFeature-Last-Delay" | ||
| private const val RETRY_COUNTER_HEADER = "HttpRetrySendFeature-Retry-Counter" | ||
|
|
||
| override val key: AttributeKey<HttpRetrySendFeature> = AttributeKey("HttpRetrySendFeature") | ||
|
|
||
| override fun install(feature: HttpRetrySendFeature, scope: HttpClient) { | ||
| scope.sendPipeline.intercept(HttpSendPipeline.Before) { | ||
| val counter = context.headers[RETRY_COUNTER_HEADER]?.toInt() ?: 0 | ||
| val lastDelay = context.headers[LAST_DELAY_HEADER]?.toLong() | ||
| ?: feature.delayGetter(0L, counter) | ||
| if (counter < feature.maxAmountsRetrying) { | ||
| try { | ||
| context.headers.remove(LAST_DELAY_HEADER) | ||
| context.headers.remove(RETRY_COUNTER_HEADER) | ||
| proceed() | ||
|
Comment on lines
+48
to
+50
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. strange logic. we send request without headers, but later we send request with headers one time and if it failed - we not catch again...do you debug this logic?
Author
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. In final this headers not send in a futher phase after
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. confused logic in code...support of this will be pain - we should rework it to more clear version. @Tetraquark will help you |
||
| } catch (e: Throwable) { | ||
| if (feature.isShouldRetryRequest(e)) { | ||
| val requestBuilder = HttpRequestBuilder().takeFrom(context) | ||
| val indexRetrying = counter + 1 | ||
| val nextDelay = feature.delayGetter(lastDelay, counter) | ||
| requestBuilder.headers[RETRY_COUNTER_HEADER] = indexRetrying.toString() | ||
| requestBuilder.headers[LAST_DELAY_HEADER] = nextDelay.toString() | ||
| delay(nextDelay) | ||
| finish() | ||
|
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. for what we finish pipeline? later called proceed
Author
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. When finish a pipeline without a calling of proceedWith the ktor throw error: |
||
|
|
||
| proceedWith(HttpStatement(requestBuilder, scope).execute().call) | ||
| } else { | ||
| throw e | ||
| } | ||
| } | ||
| } else { | ||
| context.headers.remove(LAST_DELAY_HEADER) | ||
| context.headers.remove(RETRY_COUNTER_HEADER) | ||
| proceed() | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| override fun prepare(block: Config.() -> Unit): HttpRetrySendFeature { | ||
| return Config().apply(block).build() | ||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,8 +33,7 @@ class TokenFeature private constructor( | |
| override fun install(feature: TokenFeature, scope: HttpClient) { | ||
| scope.requestPipeline.intercept(HttpRequestPipeline.State) { | ||
| feature.tokenProvider.getToken()?.apply { | ||
| context.headers.remove(feature.tokenHeaderName) | ||
| context.header(feature.tokenHeaderName, this) | ||
| context.headers[feature.tokenHeaderName] = this | ||
|
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. why this chaged?
Author
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. Because it replace two operation(maybe more, search->get->delete-->add) to one(rewrite | write) => mini performance)
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. it was added because we have case when multiple tokens was sent
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. |
||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.