Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Review feedback - kdocs, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Grisha Kruglov committed Sep 22, 2020
1 parent c74f17e commit e06ef87
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,40 @@ internal sealed class Result<out T> {
object Failure : Result<Nothing>()
}

/**
* A helper function which allows retrying a [block] of suspend code for a few times in case it fails.
*
* @param logger [Logger] that will be used to log retry attempts/results
* @param retryCount How many retry attempts are allowed
* @param block A suspend function to execute
* @return A [Result.Success] wrapping result of execution of [block] on (eventual) success,
* or [Result.Failure] otherwise.
*/
internal suspend fun <T> withRetries(logger: Logger, retryCount: Int, block: suspend () -> T): Result<T> {
var attempt = 0
var res: T? = null
while (attempt < retryCount && (res == null || res == false)) {
attempt += 1
logger.info("attempt $attempt/$retryCount")
logger.info("withRetries: attempt $attempt/$retryCount")
res = block()
}
return if (res == null || res == false) {
logger.warn("all attempts failed")
logger.warn("withRetries: all attempts failed")
Result.Failure
} else {
Result.Success(res)
}
}

/**
* A helper function which allows retrying a [block] of suspend code for a few times in case it fails.
* Short-circuits execution if [block] returns [ServiceResult.AuthError] during any of its attempts.
*
* @param logger [Logger] that will be used to log retry attempts/results
* @param retryCount How many retry attempts are allowed
* @param block A suspend function to execute
* @return A [ServiceResult] representing result of [block] execution.
*/
internal suspend fun withServiceRetries(
logger: Logger,
retryCount: Int,
Expand All @@ -105,14 +123,14 @@ internal suspend fun withServiceRetries(
var attempt = 0
do {
attempt += 1
logger.info("attempt $attempt/$retryCount")
logger.info("withServiceRetries: attempt $attempt/$retryCount")
when (val res = block()) {
ServiceResult.Ok, ServiceResult.AuthError -> return res
ServiceResult.OtherError -> {}
}
} while (attempt < retryCount)

logger.warn("all attempts failed")
logger.warn("withServiceRetries: all attempts failed")
return ServiceResult.OtherError
}

Expand Down

0 comments on commit e06ef87

Please sign in to comment.