Skip to content

Commit

Permalink
Merge pull request #57 from kuzzleio/1.2.2-proposal
Browse files Browse the repository at this point in the history
Release 1.2.2
  • Loading branch information
Shiranuit authored May 25, 2021
2 parents 7a8a9d9 + 81ceaab commit 7f41e4a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .ci/doc/test-snippet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e

if [ ${1: -3} == ".kt" ]
then
cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/kotlin-project/libs/ && cp $1 /mnt/.ci/doc/kotlin-project/src/main/java/SnippetTest.kt && cd /mnt/.ci/doc/kotlin-project/ && ./gradlew build && java -classpath 'libs/sdk-jvm-1.2.1-without-dependencies.jar:' -jar build/libs/project-1.jar
cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/kotlin-project/libs/ && cp $1 /mnt/.ci/doc/kotlin-project/src/main/java/SnippetTest.kt && cd /mnt/.ci/doc/kotlin-project/ && ./gradlew build && java -classpath 'libs/sdk-jvm-1.2.2-without-dependencies.jar:' -jar build/libs/project-1.jar
else
cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/java-project/libs/ && cp $1 /mnt/.ci/doc/java-project/src/main/java/SnippetTest.java && cd /mnt/.ci/doc/java-project/ && ./gradlew build && java -classpath 'libs/jvm-1.2.1-without-dependencies.jar:' -jar build/libs/project-1.jar
cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/java-project/libs/ && cp $1 /mnt/.ci/doc/java-project/src/main/java/SnippetTest.java && cd /mnt/.ci/doc/java-project/ && ./gradlew build && java -classpath 'libs/jvm-1.2.2-without-dependencies.jar:' -jar build/libs/project-1.jar
fi
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ https://bintray.com/kuzzle/maven
<dependency>
<groupId>io.kuzzle</groupId>
<artifactId>sdk-jvm</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<type>pom</type>
</dependency>
```
Expand All @@ -54,19 +54,19 @@ https://bintray.com/kuzzle/maven
### Gradle

```groovy
compile 'io.kuzzle:sdk-jvm:1.2.1'
compile 'io.kuzzle:sdk-jvm:1.2.2'
```

For amd64:

```groovy
compile 'io.kuzzle:sdk-jvm:1.2.1'
compile 'io.kuzzle:sdk-jvm:1.2.2'
```

### Ivy

```html
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.1'>
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.2'>
<artifact name='sdk-jvm' ext='pom' ></artifact>
</dependency>
```
Expand Down
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ plugins {

val artifactName = "sdk-jvm"
val artifactGroup = "io.kuzzle"
val artifactVersion = "1.2.1"
val artifactVersion = "1.2.2"

val pomUrl = "https://github.com/kuzzleio/sdk-jvm"
val pomScmUrl = "https://github.com/kuzzleio/sdk-jvm"
Expand All @@ -35,7 +35,7 @@ val pomDeveloperId = "kuzzleio"
val pomDeveloperName = "kuzzle"

group = "io.kuzzle.sdk"
version = "1.2.1"
version = "1.2.2"
val ktorVersion = "1.5.2"

repositories {
Expand Down Expand Up @@ -90,12 +90,12 @@ application {
}

tasks.withType<Jar> {
archiveFileName.set("sdk-jvm-1.2.1-without-dependencies.jar")
archiveFileName.set("sdk-jvm-1.2.2-without-dependencies.jar")
}

tasks {
register("fatJar", Jar::class.java) {
archiveFileName.set("sdk-jvm-1.2.1.jar")
archiveFileName.set("sdk-jvm-1.2.2.jar")
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes("Main-Class" to application.mainClassName)
Expand Down
6 changes: 3 additions & 3 deletions doc/1/getting-started/java/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ To build the project, add the following lines:
<dependency>
<groupId>io.kuzzle</groupId>
<artifactId>sdk-jvm</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<type>pom</type>
</dependency>
```
Expand All @@ -75,14 +75,14 @@ repositories {
}
}
dependencies {
compile 'io.kuzzle:sdk-jvm:1.2.1'
compile 'io.kuzzle:sdk-jvm:1.2.2'
}
```

### Ivy

```html
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.1'>
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.2'>
<artifact name='sdk-jvm' ext='pom' ></artifact>
</dependency>
```
Expand Down
6 changes: 3 additions & 3 deletions doc/1/getting-started/kotlin/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ To build the project, add the following lines:
<dependency>
<groupId>io.kuzzle</groupId>
<artifactId>sdk-jvm</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<type>pom</type>
</dependency>
```
Expand All @@ -87,14 +87,14 @@ repositories {
}
}
dependencies {
compile 'io.kuzzle:sdk-jvm:1.2.1'
compile 'io.kuzzle:sdk-jvm:1.2.2'
}
```

### Ivy

```html
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.1'>
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.2'>
<artifact name='sdk-jvm' ext='pom' ></artifact>
</dependency>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CollectionController(kuzzle: Kuzzle) : BaseController(kuzzle) {
put("action", "create")
put("index", index)
put("collection", collection)
put("definition", definition)
put("body", definition)
}
)
.thenApplyAsync { null }
Expand Down
93 changes: 64 additions & 29 deletions src/main/kotlin/io/kuzzle/sdk/protocol/WebSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.net.ConnectException
import java.net.SocketException
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.concurrent.thread

open class WebSocket : AbstractProtocol {
Expand All @@ -33,7 +34,7 @@ open class WebSocket : AbstractProtocol {
private val autoReconnect: Boolean
private val reconnectionDelay: Long
private val reconnectionRetries: Long
private var retryCount: Long = 0
private val stopRetryingToConnect: AtomicBoolean = AtomicBoolean(false)

@KtorExperimentalAPI
protected open var client = HttpClient {
Expand Down Expand Up @@ -61,35 +62,47 @@ open class WebSocket : AbstractProtocol {
this.reconnectionRetries = reconnectionRetries
}

private fun tryToReconnect(): Boolean {
if (!autoReconnect)
return false
@KtorExperimentalAPI
private fun tryToReconnect(): CompletableFuture<Boolean> {
if (!autoReconnect || this.stopRetryingToConnect.get())
return CompletableFuture.completedFuture(false)

state = ProtocolState.RECONNECTING
trigger("networkStateChange", state.toString())
return CompletableFuture.supplyAsync(
fun(): Boolean {
var retryCount: Long = 0
while ((reconnectionRetries == -1L || retryCount < reconnectionRetries) && !this.stopRetryingToConnect.get()) {
// If not infinite, increment retryCount
if (reconnectionRetries != -1L)
retryCount++

while (retryCount < reconnectionRetries) {
retryCount++
Thread.sleep(reconnectionDelay)
try {
connect()
return true
} catch (e: Exception) {
// Nothing to do, just retry
Thread.sleep(reconnectionDelay)
try {
connect()
return true
} catch (e: Exception) {
// Nothing to do, just retry
}
}
this.stopRetryingToConnect.set(false)
return false
}
}
return false
)
}

@KtorExperimentalAPI
override fun connect() {
if (this.stopRetryingToConnect.get())
throw Exception("Connection Aborted")

val wait = CompletableFuture<Void>()
val block: suspend DefaultClientWebSocketSession.() -> Unit = {
ws = this
// @TODO Create enums for events
state = ProtocolState.OPEN
trigger("networkStateChange", ProtocolState.OPEN.toString())
retryCount = 0

thread(start = true) {
while (ws != null) {
val payload = queue.poll()
Expand All @@ -101,7 +114,7 @@ open class WebSocket : AbstractProtocol {
}
}
wait.complete(null)
var reconnected = false
var skip = false
try {
for (frame in incoming) {
when (frame) {
Expand All @@ -112,9 +125,20 @@ open class WebSocket : AbstractProtocol {
}
}
} catch (e: Exception) {
reconnected = tryToReconnect()
skip = true
tryToReconnect().thenApply(
fun (success: Boolean) {
if (!success) {
state = ProtocolState.CLOSE
trigger("networkStateChange", ProtocolState.CLOSE.toString())
ws = null
}
// reset stopRetryingToConnect
stopRetryingToConnect.set(false)
}
)
}
if (!reconnected) {
if (!skip) {
state = ProtocolState.CLOSE
trigger("networkStateChange", ProtocolState.CLOSE.toString())
ws = null
Expand All @@ -135,7 +159,10 @@ open class WebSocket : AbstractProtocol {
block = block
)
}
retryCount = 0

// On connection success
stopRetryingToConnect.set(false)

// This thread is here to let JAVA run until the socket is closed
// In Kotlin this is handled by the block function above but for some reason in JAVA it is
// non blocking.
Expand All @@ -148,11 +175,16 @@ open class WebSocket : AbstractProtocol {
is SocketException,
is IOException -> {
if (state != ProtocolState.RECONNECTING) {
if (!tryToReconnect()) {
wait.completeExceptionally(e)
} else {
wait.complete(null)
}
tryToReconnect().thenAcceptAsync(
fun (success: Boolean) {
if (success) {
wait.complete(null)
} else {
wait.completeExceptionally(e)
}
stopRetryingToConnect.set(false)
}
)
} else {
wait.completeExceptionally(e)
}
Expand All @@ -164,11 +196,14 @@ open class WebSocket : AbstractProtocol {
}

override fun disconnect() {
state = ProtocolState.CLOSE
trigger("networkStateChange", ProtocolState.CLOSE.toString())
GlobalScope.launch {
ws?.close()
ws = null
if (state != ProtocolState.CLOSE) {
state = ProtocolState.CLOSE
trigger("networkStateChange", ProtocolState.CLOSE.toString())
stopRetryingToConnect.set(true)
GlobalScope.launch {
ws?.close()
ws = null
}
}
}

Expand Down

0 comments on commit 7f41e4a

Please sign in to comment.