-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize Kafka code into BrokerClient/BrokerConnection (#7)
- Loading branch information
1 parent
873e17f
commit 27ddd03
Showing
14 changed files
with
343 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
latte/src/main/java/gg/beemo/latte/broker/BaseBrokerMessageHeaders.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package gg.beemo.latte.broker | ||
|
||
import java.util.* | ||
|
||
open class BaseBrokerMessageHeaders( | ||
val clientId: String, | ||
val sourceCluster: String, | ||
targetClusters: Set<String>?, // // Empty = No target restriction | ||
requestId: String?, | ||
) { | ||
|
||
val targetClusters: Set<String> = targetClusters ?: emptySet() | ||
val requestId: String = requestId ?: UUID.randomUUID().toString() | ||
|
||
companion object { | ||
|
||
const val HEADER_CLIENT_ID = "client-id" | ||
const val HEADER_REQUEST_ID = "request-id" | ||
const val HEADER_SOURCE_CLUSTER = "source-cluster" | ||
const val HEADER_TARGET_CLUSTERS = "target-clusters" | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
latte/src/main/java/gg/beemo/latte/broker/BrokerConnection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package gg.beemo.latte.broker | ||
|
||
import gg.beemo.latte.logging.log | ||
import kotlinx.coroutines.* | ||
import java.util.* | ||
import java.util.concurrent.CopyOnWriteArraySet | ||
|
||
fun interface TopicListener { | ||
suspend fun onMessage(key: String, value: String, headers: BaseBrokerMessageHeaders) | ||
} | ||
|
||
abstract class BrokerConnection { | ||
|
||
abstract val clientId: String | ||
abstract val clusterId: String | ||
|
||
protected val topicListeners: MutableMap<String, MutableSet<TopicListener>> = Collections.synchronizedMap(HashMap()) | ||
|
||
private val eventDispatcherScope = CoroutineScope(Dispatchers.Default + SupervisorJob()) | ||
private val eventDispatcherErrorHandler = CoroutineExceptionHandler { _, t -> | ||
log.error("Uncaught error in BrokerConnection event handler", t) | ||
} | ||
|
||
abstract fun start() | ||
open fun destroy() { | ||
topicListeners.clear() | ||
eventDispatcherScope.cancel() | ||
} | ||
|
||
abstract suspend fun send( | ||
topic: String, | ||
key: String, | ||
value: String, | ||
headers: BaseBrokerMessageHeaders, | ||
blocking: Boolean = true, | ||
): String | ||
|
||
abstract fun createHeaders( | ||
targetClusters: Set<String>? = null, | ||
requestId: String? = null | ||
): BaseBrokerMessageHeaders | ||
|
||
open fun on(topic: String, cb: TopicListener) { | ||
topicListeners.computeIfAbsent(topic) { CopyOnWriteArraySet() }.add(cb) | ||
} | ||
|
||
open fun off(topic: String, cb: TopicListener) { | ||
topicListeners.computeIfPresent(topic) { _, listeners -> | ||
listeners.remove(cb) | ||
if (listeners.size == 0) { | ||
null | ||
} else { | ||
listeners | ||
} | ||
} | ||
} | ||
|
||
protected fun shouldDispatchExternallyAfterShortCircuit( | ||
topic: String, | ||
key: String, | ||
value: String, | ||
headers: BaseBrokerMessageHeaders | ||
): Boolean { | ||
val targets = headers.targetClusters | ||
val isLocalClusterInTargets = clusterId in targets | ||
|
||
// If the message is meant for ourselves (amongst other clusters), | ||
// immediately dispatch it to the listeners. | ||
if (targets.isEmpty() || isLocalClusterInTargets) { | ||
invokeCallbacks(topic, key, value, headers) | ||
} | ||
|
||
val isLocalClusterTheOnlyTarget = targets.size == 1 && isLocalClusterInTargets | ||
// Return whether implementers should dispatch this message to external services | ||
return targets.isEmpty() || !isLocalClusterTheOnlyTarget | ||
} | ||
|
||
protected fun handleIncomingMessage( | ||
topic: String, | ||
key: String, | ||
value: String, | ||
headers: BaseBrokerMessageHeaders | ||
) { | ||
if (headers.targetClusters.isNotEmpty() && clusterId !in headers.targetClusters) { | ||
// If there is a target cluster restriction and this message wasn't meant for us, | ||
// discard it immediately without notifying any listeners. | ||
return | ||
} | ||
if (headers.sourceCluster == clusterId) { | ||
// If this message was sent by ourselves, discard it too, as we already dispatch events | ||
// to our listeners in `send()` to avoid the round trip through an external service. | ||
return | ||
} | ||
invokeCallbacks(topic, key, value, headers) | ||
} | ||
|
||
private fun invokeCallbacks(topic: String, key: String, value: String, headers: BaseBrokerMessageHeaders) { | ||
eventDispatcherScope.launch(eventDispatcherErrorHandler) { | ||
val listeners = topicListeners[topic] | ||
for (listener in listeners ?: return@launch) { | ||
try { | ||
listener.onMessage(key, value, headers) | ||
} catch (t: Throwable) { | ||
log.error("Uncaught error in BrokerConnection listener", t) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
latte/src/main/java/gg/beemo/latte/broker/BrokerMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package gg.beemo.latte.broker | ||
|
||
data class BrokerMessage<T : Any>( | ||
val client: BrokerClient<T>, | ||
val key: String, | ||
val value: T?, | ||
val headers: BaseBrokerMessageHeaders | ||
) { | ||
|
||
suspend fun respond(data: T?, blocking: Boolean = true) { | ||
client.respond(this, data, blocking) | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
latte/src/main/java/gg/beemo/latte/broker/LocalConnection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package gg.beemo.latte.broker | ||
|
||
class LocalConnection( | ||
override val clusterId: String = "local-cluster-id", | ||
) : BrokerConnection() { | ||
|
||
override val clientId = "local-client-id" | ||
|
||
override suspend fun send( | ||
topic: String, | ||
key: String, | ||
value: String, | ||
headers: BaseBrokerMessageHeaders, | ||
blocking: Boolean, | ||
): String { | ||
if (shouldDispatchExternallyAfterShortCircuit(topic, key, value, headers) && headers.targetClusters.isNotEmpty()) { | ||
throw IllegalArgumentException("Attempting to send message to other cluster(s) ${headers.targetClusters} in a LocalConnection") | ||
} | ||
|
||
return headers.requestId | ||
} | ||
|
||
override fun start() { | ||
// Nothing to start :) | ||
} | ||
|
||
override fun createHeaders(targetClusters: Set<String>?, requestId: String?): BaseBrokerMessageHeaders { | ||
return BaseBrokerMessageHeaders(this.clientId, this.clusterId, targetClusters, requestId) | ||
} | ||
|
||
} |
Oops, something went wrong.