From 653a28ed4f1691dc19e5a925d6a7634203ab0fc0 Mon Sep 17 00:00:00 2001 From: Lazzexus <54175295+ScuffedLazzexus@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:00:24 +0100 Subject: [PATCH] Only accept syslog connections once packets can be sent to Kafka The syslog socket was bound as soon as SyslogSocketServer was created, and the Kafka producer started collecting the messages afterwards. A SharedFlow drops what's emitted while nothing collects it, so a client that connected (or reconnected) while events-server-syslog started up could lose its first messages. Now the socket is bound in start(), after the producer is collecting. Co-Authored-By: Claude Opus 5.5 --- .../processor/syslog/SyslogSocketServer.kt | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/modules/events-server-syslog/src/main/kotlin/dev/adamko/kafkatorio/processor/syslog/SyslogSocketServer.kt b/modules/events-server-syslog/src/main/kotlin/dev/adamko/kafkatorio/processor/syslog/SyslogSocketServer.kt index 1b44f7d..c093706 100644 --- a/modules/events-server-syslog/src/main/kotlin/dev/adamko/kafkatorio/processor/syslog/SyslogSocketServer.kt +++ b/modules/events-server-syslog/src/main/kotlin/dev/adamko/kafkatorio/processor/syslog/SyslogSocketServer.kt @@ -19,6 +19,7 @@ import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.isActive @@ -32,13 +33,8 @@ class SyslogSocketServer( appProps: ApplicationProperties, ) { - /** Incoming messages */ - private val serverSocketTcp = aSocket(SelectorManager(Dispatchers.IO)) - .tcp() - .bind( - appProps.socketServerHost, - appProps.socketServerPort, - ) + private val host = appProps.socketServerHost + private val port = appProps.socketServerPort private val syslogParser: SyslogParser> = @@ -53,12 +49,21 @@ class SyslogSocketServer( ).build() + /** Incoming messages */ private val _messages: MutableSharedFlow = MutableSharedFlow() val messages: SharedFlow get() = _messages.asSharedFlow() suspend fun start(): Unit = withContext(Dispatchers.IO) { + // A SharedFlow drops messages that are emitted before anything collects them, so only accept + // connections once the Kafka producer is collecting. Otherwise, clients that (re)connect + // while this starts up lose their first messages. + _messages.subscriptionCount.first { it > 0 } + + val serverSocketTcp = aSocket(SelectorManager(Dispatchers.IO)) + .tcp() + .bind(host, port) log("started listening: ${serverSocketTcp.localAddress}") while (isActive) {