-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from reugn/develop
v0.3.0
- Loading branch information
Showing
22 changed files
with
201 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: Build | ||
on: | ||
push: | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up JDK 1.8 | ||
uses: actions/setup-java@v1 | ||
with: | ||
java-version: 1.8 | ||
|
||
- name: Cache Gradle packages | ||
uses: actions/cache@v2 | ||
with: | ||
path: | | ||
~/.gradle/caches | ||
~/.gradle/wrapper | ||
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | ||
restore-keys: | | ||
${{ runner.os }}-gradle- | ||
- name: Build with Gradle | ||
run: ./gradlew clean test | ||
|
||
- name: Cleanup Gradle Cache | ||
# Remove some files from the Gradle cache, so they aren't cached by GitHub Actions. | ||
# Restoring these files from a GitHub Actions cache might cause problems for future builds. | ||
run: | | ||
rm -f ~/.gradle/caches/modules-2/modules-2.lock | ||
rm -f ~/.gradle/caches/modules-2/gc.properties |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,31 @@ | ||
# kotlin-backoff | ||
[![Download](https://api.bintray.com/packages/reug/maven/kotlin-backoff/images/download.svg)](https://bintray.com/reug/maven/kotlin-backoff/_latestVersion) | ||
[![Build Status](https://travis-ci.com/reugn/kotlin-backoff.svg?branch=master)](https://travis-ci.com/reugn/kotlin-backoff) | ||
[![Build](https://github.com/reugn/kotlin-backoff/actions/workflows/build.yml/badge.svg)](https://github.com/reugn/kotlin-backoff/actions/workflows/build.yml) | ||
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.reugn/kotlin-backoff/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.reugn/kotlin-backoff/) | ||
|
||
Simple Kotlin Exponential backoff library | ||
A simple Kotlin Exponential Backoff library designed for `kotlinx.coroutines`. | ||
|
||
## Installation | ||
## Getting started | ||
Gradle: | ||
```kotlin | ||
repositories { | ||
maven { | ||
setUrl("https://dl.bintray.com/reug/maven") | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation 'com.github.reugn:kotlin-backoff:<version>' | ||
implementation("io.github.reugn:kotlin-backoff:<version>") | ||
} | ||
``` | ||
|
||
## Examples | ||
```kotlin | ||
private val action = suspend { URL("http://worldtimeapi.org/").readText() } | ||
private val action = suspend { URL("http://worldclockapi.com/api/json/utc/now").readText() } | ||
|
||
@Test | ||
fun urlTest() { | ||
val backoff = StrategyBackoff<String>(Duration.ofMillis(100), { s -> s.isNotEmpty() }, 3, | ||
val backoff = StrategyBackoff<String>(Duration.ofMillis(500), { s -> s.isNotEmpty() }, 3, | ||
Strategy.expFullJitter(2), ::nonFatal) | ||
val result = runBlocking { backoff.retry(action) } | ||
assert(result.isOk()) | ||
assertEquals(result.retries, 1) | ||
} | ||
``` | ||
More examples can be found in the test section. | ||
|
||
## License | ||
Licensed under the [Apache 2.0 License](./LICENSE). | ||
Licensed under the [Apache 2.0 License](./LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
src/main/kotlin/com/github/reugn/backoff/strategy/ConstantStrategy.kt
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
src/main/kotlin/com/github/reugn/backoff/strategy/ExponentialPartialJitterStrategy.kt
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
src/main/kotlin/com/github/reugn/backoff/strategy/ExponentialStrategy.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...otlin/com/github/reugn/backoff/Backoff.kt → ...io/github/reugn/kotlin/backoff/Backoff.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/io/github/reugn/kotlin/backoff/strategy/ConstantStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.github.reugn.kotlin.backoff.strategy | ||
|
||
class ConstantStrategy : Strategy { | ||
|
||
override fun next(previousDelayPeriod: Long): Long = previousDelayPeriod | ||
} |
6 changes: 3 additions & 3 deletions
6
...strategy/ExponentialFullJitterStrategy.kt → ...strategy/ExponentialFullJitterStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/io/github/reugn/kotlin/backoff/strategy/ExponentialPartialJitterStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.github.reugn.kotlin.backoff.strategy | ||
|
||
import kotlin.random.Random | ||
|
||
class ExponentialPartialJitterStrategy(private val base: Int, private val ratio: Double) : Strategy { | ||
|
||
override fun next(previousDelayPeriod: Long): Long { | ||
val next = previousDelayPeriod * base | ||
val jitter = Random.nextInt(0, (previousDelayPeriod / 2).toInt()) | ||
return (next * ratio).toLong() + jitter | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/io/github/reugn/kotlin/backoff/strategy/ExponentialStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.github.reugn.kotlin.backoff.strategy | ||
|
||
class ExponentialStrategy(private val base: Int) : Strategy { | ||
|
||
override fun next(previousDelayPeriod: Long): Long = previousDelayPeriod * base | ||
} |
Oops, something went wrong.