Skip to content

Commit

Permalink
Prevent exceptions due to heartbeats
Browse files Browse the repository at this point in the history
Resolves:
#173
  • Loading branch information
joffrey-bion committed Jan 16, 2022
1 parent fe099b9 commit 604c28a
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.hildan.krossbow.stomp.headers.*
import org.hildan.krossbow.stomp.heartbeats.HeartBeater
import org.hildan.krossbow.stomp.heartbeats.NO_HEART_BEATS
import org.hildan.krossbow.utils.generateUuid
import org.hildan.krossbow.websocket.WebSocketException
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.cancellation.CancellationException
Expand All @@ -35,7 +36,15 @@ internal class BaseStompSession(
HeartBeater(
heartBeat = heartBeat,
tolerance = config.heartBeatTolerance,
sendHeartBeat = { stompSocket.sendHeartBeat() },
sendHeartBeat = {
// The web socket could have errored or be closed, and the heart beater's job not yet be cancelled.
// In this case, we don't want the heart beater to crash
try {
stompSocket.sendHeartBeat()
} catch(e : WebSocketException) {
shutdown("STOMP session failed: heart beat couldn't be sent", cause = e)
}
},
onMissingHeartBeat = {
val cause = MissingHeartBeatException(heartBeat.expectedPeriod)
sharedStompEvents.emit(StompEvent.Error(cause))
Expand Down

0 comments on commit 604c28a

Please sign in to comment.