在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):michaelbull/kotlin-retry开源软件地址(OpenSource Url):https://github.com/michaelbull/kotlin-retry开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):kotlin-retry
retry(limitAttempts(10) + constantDelay(delayMillis = 50L)) {
/* your code */
} Installationrepositories {
mavenCentral()
}
dependencies {
implementation("com.michael-bull.kotlin-retry:kotlin-retry:1.0.9")
} IntroductionIO operations often experience temporary failures that warrant re-execution, e.g. a database transaction that may fail due to a deadlock.12
The In the example below, either of the calls to import com.github.michaelbull.retry.policy.limitAttempts
import com.github.michaelbull.retry.retry
import kotlinx.coroutines.runBlocking
suspend fun printExchangeBetween(a: Long, b: Long) {
val customer1 = customers.nameFromId(a)
val customer2 = customers.nameFromId(b)
println("$customer1 exchanged with $customer2")
}
fun main() = runBlocking {
retry(limitAttempts(5)) {
printExchangeBetween(1L, 2L)
}
} We can also provide a import com.github.michaelbull.retry.ContinueRetrying
import com.github.michaelbull.retry.StopRetrying
import com.github.michaelbull.retry.policy.RetryPolicy
import com.github.michaelbull.retry.policy.constantDelay
import com.github.michaelbull.retry.policy.limitAttempts
import com.github.michaelbull.retry.policy.plus
import com.github.michaelbull.retry.retry
import kotlinx.coroutines.runBlocking
import java.sql.SQLDataException
val retryTimeouts: RetryPolicy<Throwable> = {
if (reason is SQLDataException) ContinueRetrying else StopRetrying
}
suspend fun printExchangeBetween(a: Long, b: Long) {
val customer1 = customers.nameFromId(a)
val customer2 = customers.nameFromId(b)
println("$customer1 exchanged with $customer2")
}
fun main() = runBlocking {
retry(retryTimeouts + limitAttempts(5) + constantDelay(20)) {
printExchangeBetween(1L, 2L)
}
} BackoffThe examples above retry executions immediately after they fail, however we may wish to spread out retries with an ever-increasing delay. This is known as a "backoff" and comes in many forms. This library includes all the forms of backoff strategy detailed the article by Marc Brooker on the AWS Architecture Blog entitled "Exponential Backoff And Jitter". Binary Exponential
retry(limitAttempts(5) + binaryExponentialBackoff(base = 10L, max = 5000L)) {
/* code */
} Full Jitter
retry(limitAttempts(5) + fullJitterBackoff(base = 10L, max = 5000L)) {
/* code */
} Equal Jitter
retry(limitAttempts(5) + equalJitterBackoff(base = 10L, max = 5000L)) {
/* code */
} Decorrelated Jitter
retry(limitAttempts(5) + decorrelatedJitterBackoff(base = 10L, max = 5000L)) {
/* code */
} InspirationContributingBug reports and pull requests are welcome on GitHub. LicenseThis project is available under the terms of the ISC license. See the
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论