Skip to content

Commit d24127c

Browse files
Merge pull request #132 from leanix/feature/CID-3977/fix-concurrency-issue-when-sending-a-message
CID-3977: Fix concurrency issue by synchronizing server message sends
2 parents 9e94561 + df02ddf commit d24127c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/main/kotlin/net/leanix/githubagent/exceptions/Exceptions.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ class GraphQLApiException(errors: List<GraphQLClientError>) :
1414
class WebhookSecretNotSetException : RuntimeException("Webhook secret not set")
1515
class InvalidEventSignatureException : RuntimeException("Invalid event signature")
1616
class ManifestFileNotFoundException : RuntimeException("Manifest File Not Found")
17-
class UnableToSendMessageException : RuntimeException("Unable to send message to the backend")
17+
class UnableToSendMessageException(exception: Exception) :
18+
RuntimeException(
19+
"Unable to send message to the backend",
20+
exception
21+
)

src/main/kotlin/net/leanix/githubagent/services/WebSocketService.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import net.leanix.githubagent.shared.TOPIC_PREFIX
88
import org.slf4j.LoggerFactory
99
import org.springframework.messaging.simp.stomp.StompSession
1010
import org.springframework.stereotype.Service
11+
import java.util.concurrent.locks.ReentrantLock
1112

1213
@Service
1314
class WebSocketService(
@@ -18,6 +19,7 @@ class WebSocketService(
1819

1920
private val logger = LoggerFactory.getLogger(WebSocketService::class.java)
2021
var stompSession: StompSession? = null
22+
private val stompSendLock = ReentrantLock()
2123

2224
fun initSession() {
2325
logger.info("Initializing websocket session")
@@ -35,10 +37,13 @@ class WebSocketService(
3537
if (!brokerStompSessionHandler.isConnected() && cachingService.get("runId") == null) {
3638
return
3739
}
38-
runCatching {
40+
stompSendLock.lock()
41+
try {
3942
stompSession!!.send("$TOPIC_PREFIX$topic", data)
40-
}.onFailure {
41-
throw UnableToSendMessageException()
43+
} catch (e: Exception) {
44+
throw UnableToSendMessageException(e)
45+
} finally {
46+
stompSendLock.unlock()
4247
}
4348
}
4449
}

0 commit comments

Comments
 (0)