Skip to content

Instantly share code, notes, and snippets.

⌨️
I may be slow to respond.

Leopold Baik AgustaRC

⌨️
I may be slow to respond.
Block or report user

Report or block AgustaRC

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View retryWhenSample3.kt
internal typealias RETRY_PREDICATE = (Throwable) -> Boolean
internal const val MAX_RETRIES = 3L
internal const val DEFAULT_INTERVAL = 1L
internal val TIMEOUT: RETRY_PREDICATE = { it is SocketTimeoutException }
internal val NETWORK: RETRY_PREDICATE = { it is IOException }
internal val SERVICE_UNAVAILABLE: RETRY_PREDICATE = { it is HttpException && it.code() == 503 }
internal fun <T> applyRetryPolicy(
View retryWhenSample9.kt
single.retryWhen { attempts ->
Flowables.zip(
attempts.map { error -> if (predicates.count { it(error) } > 0) error else throw error },
Flowable.interval(interval, unit)
).map { (error, retryCount) -> if (retryCount >= maxRetries) throw error }
}
single.onErrorResumeNext(resumeNext)
View retryWhenSample8.kt
internal class ImagesRepositoryImpl(private val source: ImageDataSource) : ImagesRepository {
override fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>> {
return source.getImages(params)
.map { Result.success(it) }
.onErrorResumeNext { Single.just(Result.failure(it)) }
.compose(applyRetryPolicy(TIMEOUT) { Single.just(Result.failure(it)) })
}
}
View retryWhenSample7.kt
this += repository.getImages(params)
.compose(applySingleAsync())
.subscribe { result ->
result.fold(
onSuccess = { },
onFailure = { }
)
}
View retryWhenSample6.kt
internal class ImagesRepositoryImpl(private val source: ImageDataSource) : ImagesRepository {
override fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>> {
return source.getImages(params)
.map { Result.success(it) }
.compose(applyRetryPolicy(TIMEOUT, maxRetries = 5, interval = 2, unit = TimeUnit.SECONDS) { Single.just(Result.failure(it)) })
}
}
View retryWhenSample5.kt
internal class ImagesRepositoryImpl(private val source: ImageDataSource) : ImagesRepository {
override fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>> {
return source.getImages(params)
.map { Result.success(it) }
.compose(applyRetryPolicy(TIMEOUT, NETWORK, SERVICE_UNAVAILABLE) { Single.just(Result.failure(it)) })
}
}
View retryWhenSample2.kt
internal typealias RETRY_PREDICATE = (Throwable) -> Boolean
internal const val MAX_RETRIES = 3L
internal const val DEFAULT_INTERVAL = 1L
internal val TIMEOUT: RETRY_PREDICATE = { it is SocketTimeoutException }
internal val NETWORK: RETRY_PREDICATE = { it is IOException }
internal val SERVICE_UNAVAILABLE: RETRY_PREDICATE = { it is HttpException && it.code() == 503 }
View retryWhenSample4.kt
interface ImagesRepository {
fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>>
}
internal class ImagesRepositoryImpl(private val source: ImageDataSource) : ImagesRepository {
override fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>> {
return source.getImages(params)
.map { Result.success(it) }
.compose(applyRetryPolicy(TIMEOUT) { Single.just(Result.failure(it)) })
}
View retryWhenSample.kt
retryWhen { attempts ->
Flowables.zip(
attempts.map { error -> if (error is IOException) error else throw error },
Flowable.interval(1, TimeUnit.SECONDS)
).map { (error, retryCount) -> if (retryCount >= 3) throw error }
}
View retryWhen.java
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Single<T> retryWhen(Function<? super Flowable<Throwable>, ? extends Publisher<?>> handler) {
return toSingle(toFlowable().retryWhen(handler));
}
You can’t perform that action at this time.