Skip to content

Commit

Permalink
fix multiplatform time measure
Browse files Browse the repository at this point in the history
  • Loading branch information
lepicekmichal committed Dec 31, 2022
1 parent 55f5457 commit fc22c8b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ automaticReconnect = AutomaticReconnect.Active
* Extra reconnect policy
* Each attempt to reconnect is suspended with delay of exponential backoff time
* In default settings
* initially waits 1 second, then 1.5 times more seconds each time and at most 15 tries with highest delay of 60 seconds
* initially waits 1 second, then 1.5 times more seconds each time
* at most 15 tries with highest delay of 60 seconds
* all can be adjusted
*/
automaticReconnect = AutomaticReconnect.exponentialBackoff()
Expand All @@ -239,8 +240,9 @@ automaticReconnect = AutomaticReconnect.exponentialBackoff()
* You can implement anything you find plausible
*/
automaticReconnect = AutomaticReconnect.Custom { previousRetryCount, elapsedTime ->
// before each attempt wait random time but at most 60 seconds
delay(Random.nextLong(60_000))
// before each attempt wait random time
// but at most only the time that we are already trying to reconnect
delay(Random.nextLong(elapsedTime.inWholeMilliseconds))
}
```

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RELEASE_SIGNING_ENABLED=true

GROUP=eu.lepicekmichal.signalrkore
POM_ARTIFACT_ID=signalrkore
VERSION_NAME=0.4.0
VERSION_NAME=0.5.0

POM_NAME=SignalR Kore
POM_DESCRIPTION=Connect to SignalR Core server with library written in Kotlin and coroutines.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package eu.lepicekmichal.signalrkore

import eu.lepicekmichal.signalrkore.AutomaticReconnect.Custom
import kotlinx.datetime.DateTimePeriod
import kotlin.math.pow
import kotlin.time.Duration

sealed interface AutomaticReconnect {
object Inactive : AutomaticReconnect
fun interface Custom : AutomaticReconnect {
suspend fun invoke(previousRetryCount: Int, elapsedTime: DateTimePeriod): Long?
suspend fun invoke(previousRetryCount: Int, elapsedTime: Duration): Long?
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.serializer
import kotlin.reflect.KClass
import kotlin.system.measureNanoTime
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime
import kotlin.time.TimeSource
import kotlin.time.measureTime

@OptIn(ExperimentalCoroutinesApi::class)
class HubConnection private constructor(
Expand Down Expand Up @@ -275,6 +277,7 @@ class HubConnection private constructor(
}
}

@OptIn(ExperimentalTime::class)
private suspend fun reconnect(errorMessage: String? = null) {
stop(errorMessage)

Expand All @@ -283,13 +286,13 @@ class HubConnection private constructor(
_connectionState.value = HubConnectionState.RECONNECTING

scope.launch {
val startTime = System.nanoTime()
val mark = TimeSource.Monotonic.markNow()
var retryCount = 0

while (true) {
val delayTime = automaticReconnect.invoke(
previousRetryCount = retryCount++,
elapsedTime = DateTimePeriod(nanoseconds = System.nanoTime() - startTime),
elapsedTime = mark.elapsedNow(),
)

delay(timeMillis = delayTime ?: break)
Expand Down

0 comments on commit fc22c8b

Please sign in to comment.