Skip to content

Commit

Permalink
Merge pull request #3 from reugn/develop
Browse files Browse the repository at this point in the history
v0.3.0
  • Loading branch information
reugn authored Jun 22, 2021
2 parents 471a03f + 507700d commit 94acdbb
Show file tree
Hide file tree
Showing 22 changed files with 201 additions and 166 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/build.yml
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
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

23 changes: 9 additions & 14 deletions README.md
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).
105 changes: 53 additions & 52 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.FileInputStream
import java.util.*

plugins {
kotlin("jvm") version "1.3.50"
kotlin("plugin.serialization") version "1.3.50"
java
`java-library`
kotlin("jvm") version "1.5.10"
kotlin("plugin.serialization") version "1.5.10"
`maven-publish`
id("com.jfrog.bintray") version "1.8.4"
signing
}

group = "com.github.reugn"
version = "0.2.0"
group = "io.github.reugn"
version = "0.3.0"

repositories {
mavenCentral()
jcenter()
}

java {
withJavadocJar()
withSourcesJar()
}

dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc")
testImplementation("org.junit.jupiter:junit-jupiter:5.6.0")
}

Expand All @@ -32,61 +35,59 @@ tasks.test {
useJUnitPlatform()
}

val sourcesJar by tasks.creating(Jar::class) {
archiveClassifier.set("sources")
from(sourceSets.getByName("main").allSource)
}

val url = "https://github.com/reugn/kotlin-backoff"
publishing {
publications {
create<MavenPublication>("kotlin-backoff") {
create<MavenPublication>("mavenCentral") {
groupId = project.group.toString()
artifactId = project.name
version = project.version.toString()
from(components["java"])
artifact(sourcesJar)

pom.withXml {
asNode().apply {
appendNode("description", url)
appendNode("name", rootProject.name)
appendNode("url", url)
appendNode("licenses").appendNode("license").apply {
appendNode("name", url)
appendNode("url", url)
appendNode("distribution", "repo")
}
appendNode("developers").appendNode("developer").apply {
appendNode("id", "reugn")
appendNode("name", "Reugn")
pom {
name.set(project.name)
description.set("A simple Exponential Backoff library for Kotlin.")
url.set("https://github.com/reugn/kotlin-backoff")
licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
appendNode("scm").apply {
appendNode("url", url)
}
developers {
developer {
id.set("reugn")
name.set("reugn")
email.set("reugpro@gmail.com")
url.set("https://github.com/reugn")
}
}
scm {
connection.set("scm:git:git://github.com/reugn/kotlin-backoff.git")
developerConnection.set("scm:git:ssh://github.com/reugn/kotlin-backoff.git")
url.set("https://github.com/reugn/kotlin-backoff")
}
}
}
}
repositories {
maven {
name = "mavenCentral"
credentials(PasswordCredentials::class)
val nexus = "https://s01.oss.sonatype.org/"
val releasesRepoUrl = uri(nexus + "service/local/staging/deploy/maven2")
val snapshotsRepoUrl = uri(nexus + "content/repositories/snapshots")
url = if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl
}
}
}

val prop = Properties()
prop.load(FileInputStream("local.properties"))
bintray {
user = prop.getProperty("user").toString()
key = prop.getProperty("password").toString()
publish = true

setPublications("kotlin-backoff")
signing {
sign(publishing.publications["mavenCentral"])
useGpgCmd()
sign(configurations.archives.get())
}

pkg.apply {
repo = "maven"
name = project.name
userOrg = "reug"
githubRepo = githubRepo
vcsUrl = url
description = "Simple Kotlin Exponential backoff library"
setLicenses("Apache-2.0")
desc = description
tasks.javadoc {
if (JavaVersion.current().isJava9Compatible) {
(options as StandardJavadocDocletOptions).addBooleanOption("html5", true)
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Tue Feb 11 15:11:05 IST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 0 additions & 8 deletions src/main/kotlin/com/github/reugn/backoff/utils/Success.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.reugn.backoff
package io.github.reugn.kotlin.backoff

import com.github.reugn.backoff.utils.Result
import io.github.reugn.kotlin.backoff.utils.Result

interface Backoff<T> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.github.reugn.backoff
package io.github.reugn.kotlin.backoff

import com.github.reugn.backoff.strategy.Strategy
import com.github.reugn.backoff.utils.*
import io.github.reugn.kotlin.backoff.strategy.Strategy
import io.github.reugn.kotlin.backoff.utils.*
import kotlinx.coroutines.delay
import java.time.Duration

/**
* Strategy based Backoff implementation
* A Strategy based [Backoff] implementation.
*
* @param T the type of retry result
* @property delayTime base delay interval
* @property success retry method success condition
* @property max max number of retries
* @property strategy next delay time calculation Strategy
* @property validate validate and halt the retry loop on invalid exception
* @param T the type of the retry result.
* @property delayTime the initial delay interval.
* @property success the retry method [Success] determiner.
* @property max the maximum number of retries.
* @property strategy the next delay time calculation [Strategy].
* @property validate validate and exit the retry loop on an invalid exception.
*/
class StrategyBackoff<T>(
private val delayTime: Duration,
Expand Down Expand Up @@ -53,7 +53,7 @@ class StrategyBackoff<T>(
retryN(f, n, next)
else {
e?.let {
Err<T, RetryException>(RetryException(it), max)
Err(RetryException(it), max)
} ?: Err(RetryException(), max)
}
}
Expand Down
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
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.github.reugn.backoff.strategy
package io.github.reugn.kotlin.backoff.strategy

import kotlin.random.Random

class ExponentialFullJitterStrategy(private val base: Int) : Strategy {

override fun next(delay: Long): Long {
val next = delay * base
override fun next(previousDelayPeriod: Long): Long {
val next = previousDelayPeriod * base
val jitter = Random.nextInt(0, (next / 2).toInt())
return next + jitter
}
Expand Down
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
}
}
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
}
Loading

0 comments on commit 94acdbb

Please sign in to comment.