Skip to content

Commit 4d1ee6e

Browse files
committed
fix: handle ws reconnection in loop
1 parent 1457ffb commit 4d1ee6e

4 files changed

Lines changed: 19 additions & 6 deletions

File tree

ocpp-wamp/src/main/kotlin/com/izivia/ocpp/wamp/client/autoreconnect/AutoReconnectHandler.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ internal class AutoReconnectHandler(
4343
planConnectionAttempt(clock.now())
4444
}
4545

46+
fun planConnectionAttemptAfterDelay(delay: Duration) {
47+
planConnectionAttempt(clock.now() + delay)
48+
}
49+
4650
fun planConnectionAttempt(scheduleAt: Instant, checkCurrentAttempt: Boolean = true) {
4751
withLock {
4852
val lastAttemptOrNull = lastConnectionAttempts.lastOrNull()

ocpp-wamp/src/main/kotlin/com/izivia/ocpp/wamp/client/autoreconnect/AutoReconnectOcppWampClient.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class AutoReconnectOcppWampClient(
103103
autoReconnectState.onDisconnected()
104104
when (autoReconnectState) {
105105
is AutoReconnectIdleState, is AutoReconnectConnectingState -> {}
106-
is AutoReconnectConnectedState -> moveTo(autoReconnectState, connectingState())
106+
is AutoReconnectConnectedState -> moveTo(autoReconnectState, connectingState(isReconnect = true))
107107
}
108108
connectionListener?.onConnectionLost(t)
109109
}
@@ -117,7 +117,7 @@ class AutoReconnectOcppWampClient(
117117
connectionListener?.onConnectionFailure(t)
118118
}
119119

120-
private fun connectingState() =
120+
private fun connectingState(isReconnect: Boolean = false) =
121121
AutoReconnectConnectingState(
122122
debugContext,
123123
config,
@@ -134,7 +134,8 @@ class AutoReconnectOcppWampClient(
134134
override fun onConnectionLost(t: Throwable?) {
135135
emitConnectionLost(t)
136136
}
137-
}
137+
},
138+
isReconnect = isReconnect
138139
)
139140

140141
private fun moveTo(from: AutoReconnectState, to: AutoReconnectState) {

ocpp-wamp/src/main/kotlin/com/izivia/ocpp/wamp/client/autoreconnect/AutoReconnectState.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ internal class AutoReconnectConnectingState(
4949
private val debugContext: String,
5050
private val config: AutoReconnectConfig,
5151
doConnect: (l: ConnectionListener) -> Unit,
52-
connectionListener: ConnectionListener
52+
connectionListener: ConnectionListener,
53+
private val isReconnect: Boolean = false
5354
) : AutoReconnectState {
5455
private val handler = AutoReconnectHandler(
5556
debugContext,
@@ -83,7 +84,11 @@ internal class AutoReconnectConnectingState(
8384
}
8485

8586
logger.info("[$debugContext] connecting [auto-reconnect ON]")
86-
handler.planConnectionAttemptNow()
87+
if (isReconnect) {
88+
handler.planConnectionAttemptAfterDelay(config.baseAutoReconnectDelay)
89+
} else {
90+
handler.planConnectionAttemptNow()
91+
}
8792
}
8893

8994
override fun onConnected() {

ocpp-wamp/src/main/kotlin/com/izivia/ocpp/wamp/client/impl/OkHttpOcppWampClient.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,15 @@ class OkHttpOcppWampClient(
141141
logger.warn("[$ocppId] web socket closing $code $reason - state = $connectionState")
142142
if (code == CLEANUP_CLOSURE_STATUS) {
143143
logger.info("[$ocppId] connection closed to $serverUri due to reconnection")
144+
closingWebSocket.close(code, reason)
144145
return
145146
}
146147

148+
closingWebSocket.close(code, reason)
149+
val previousState = connectionState
147150
connectionState = ConnectionState.DISCONNECTED
148151
wampConnection = null
149-
when (connectionState) {
152+
when (previousState) {
150153
ConnectionState.CONNECTING -> {
151154
listener.onConnectionFailure(
152155
IOException(

0 commit comments

Comments
 (0)