Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix connect method not throwing on failure #49

Merged
merged 11 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0-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.1-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.0-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.1-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.0</version>
<version>1.2.1</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.0'
compile 'io.kuzzle:sdk-jvm:1.2.1'
```

For amd64:

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

### Ivy

```html
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.0'>
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.1'>
<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.0"
val artifactVersion = "1.2.1"

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.0"
version = "1.2.1"
val ktorVersion = "1.5.2"

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

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

tasks {
register("fatJar", Jar::class.java) {
archiveFileName.set("sdk-jvm-1.2.0.jar")
archiveFileName.set("sdk-jvm-1.2.1.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.0</version>
<version>1.2.1</version>
<type>pom</type>
</dependency>
```
Expand All @@ -75,14 +75,14 @@ repositories {
}
}
dependencies {
compile 'io.kuzzle:sdk-jvm:1.2.0'
compile 'io.kuzzle:sdk-jvm:1.2.1'
}
```

### Ivy

```html
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.0'>
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.1'>
<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.0</version>
<version>1.2.1</version>
<type>pom</type>
</dependency>
```
Expand All @@ -87,14 +87,14 @@ repositories {
}
}
dependencies {
compile 'io.kuzzle:sdk-jvm:1.2.0'
compile 'io.kuzzle:sdk-jvm:1.2.1'
}
```

### Ivy

```html
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.0'>
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.2.1'>
<artifact name='sdk-jvm' ext='pom' ></artifact>
</dependency>
```
Expand Down
43 changes: 31 additions & 12 deletions src/main/kotlin/io/kuzzle/sdk/protocol/WebSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,36 @@ open class WebSocket : AbstractProtocol {
this.reconnectionRetries = reconnectionRetries
}

private fun tryToReconnect() {
if (retryCount < reconnectionRetries) {
private fun tryToReconnect(): Boolean {
if (!autoReconnect)
return false

state = ProtocolState.RECONNECTING
trigger("networkStateChange", state.toString())

while (retryCount < reconnectionRetries) {
retryCount++
state = ProtocolState.RECONNECTING
trigger("networkStateChange", state.toString())
Thread.sleep(reconnectionDelay)
thread(start = true) {
try {
connect()
return true
} catch (e: Exception) {
// Nothing to do, just retry
}
}
return false
}

@KtorExperimentalAPI
override fun connect() {
println(retryCount)
Shiranuit marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -92,6 +102,7 @@ open class WebSocket : AbstractProtocol {
}
}
wait.complete(null)
var reconnected = false
try {
for (frame in incoming) {
when (frame) {
Expand All @@ -102,11 +113,13 @@ open class WebSocket : AbstractProtocol {
}
}
} catch (e: Exception) {
tryToReconnect()
reconnected = tryToReconnect()
}
if (! reconnected) {
jenow marked this conversation as resolved.
Show resolved Hide resolved
state = ProtocolState.CLOSE
trigger("networkStateChange", ProtocolState.CLOSE.toString())
ws = null
}
state = ProtocolState.CLOSE
trigger("networkStateChange", ProtocolState.CLOSE.toString())
ws = null
}
GlobalScope.launch {
try {
Expand All @@ -123,7 +136,6 @@ open class WebSocket : AbstractProtocol {
block = block
)
}
retryCount = 0
// 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 @@ -135,8 +147,15 @@ open class WebSocket : AbstractProtocol {
is ConnectException,
is SocketException,
is IOException -> {
tryToReconnect()
wait.complete(null)
if (state != ProtocolState.RECONNECTING) {
if (!tryToReconnect()) {
wait.completeExceptionally(e)
} else {
wait.complete(null)
}
} else {
wait.completeExceptionally(e)
}
} else -> throw e
}
}
Expand Down