io.infinitic.inMemory.InMemoryChannels.kt Maven / Gradle / Ivy
/**
* "Commons Clause" License Condition v1.0
*
* The Software is provided to you by the Licensor under the License, as defined below, subject to
* the following condition.
*
* Without limiting other conditions in the License, the grant of rights under the License will not
* include, and the License does not grant to you, the right to Sell the Software.
*
* For purposes of the foregoing, “Sell” means practicing any or all of the rights granted to you
* under the License to provide to third parties, for a fee or other consideration (including
* without limitation fees for hosting or consulting/ support services related to the Software), a
* product or service whose value derives, entirely or substantially, from the functionality of the
* Software. Any license notice or attribution required by the License must also include this
* Commons Clause License Condition notice.
*
* Software: Infinitic
*
* License: MIT License (https://opensource.org/licenses/MIT)
*
* Licensor: infinitic.io
*/
package io.infinitic.inMemory
import io.infinitic.common.clients.messages.ClientMessage
import io.infinitic.common.data.MillisDuration
import io.infinitic.common.exceptions.thisShouldNotHappen
import io.infinitic.common.messages.Message
import io.infinitic.common.tasks.events.messages.ServiceEventMessage
import io.infinitic.common.tasks.executors.messages.ServiceExecutorMessage
import io.infinitic.common.tasks.tags.messages.ServiceTagMessage
import io.infinitic.common.transport.ClientTopic
import io.infinitic.common.transport.DelayedServiceExecutorTopic
import io.infinitic.common.transport.DelayedWorkflowEngineTopic
import io.infinitic.common.transport.DelayedWorkflowTaskExecutorTopic
import io.infinitic.common.transport.ServiceEventsTopic
import io.infinitic.common.transport.ServiceExecutorTopic
import io.infinitic.common.transport.ServiceTagTopic
import io.infinitic.common.transport.Topic
import io.infinitic.common.transport.WorkflowCmdTopic
import io.infinitic.common.transport.WorkflowEngineTopic
import io.infinitic.common.transport.WorkflowEventsTopic
import io.infinitic.common.transport.WorkflowTagTopic
import io.infinitic.common.transport.WorkflowTaskEventsTopic
import io.infinitic.common.transport.WorkflowTaskExecutorTopic
import io.infinitic.common.workflows.engine.messages.WorkflowEngineMessage
import io.infinitic.common.workflows.engine.messages.WorkflowEventMessage
import io.infinitic.common.workflows.tags.messages.WorkflowTagMessage
import kotlinx.coroutines.channels.Channel
import java.util.concurrent.ConcurrentHashMap
class InMemoryChannels {
private val clientChannels =
ConcurrentHashMap>()
private val serviceTagChannels =
ConcurrentHashMap>()
private val workflowTagChannels =
ConcurrentHashMap>()
private val serviceExecutorChannels =
ConcurrentHashMap>()
private val serviceEventsChannels =
ConcurrentHashMap>()
private val delayedServiceExecutorChannels =
ConcurrentHashMap>>()
private val workflowCmdChannels =
ConcurrentHashMap>()
private val workflowEngineChannels =
ConcurrentHashMap>()
private val delayedWorkflowEngineChannels =
ConcurrentHashMap>>()
private val workflowEventsChannels =
ConcurrentHashMap>()
private val workflowTaskExecutorChannels =
ConcurrentHashMap>()
private val workflowTaskEventsChannels =
ConcurrentHashMap>()
private val delayedWorkflowTaskExecutorChannels =
ConcurrentHashMap>>()
@Suppress("UNCHECKED_CAST")
fun Topic.channel(entity: String): Channel = when (this) {
ClientTopic -> clientChannels.getOrPut(entity, newChannel())
ServiceTagTopic -> serviceTagChannels.getOrPut(entity, newChannel())
WorkflowTagTopic -> workflowTagChannels.getOrPut(entity, newChannel())
ServiceExecutorTopic -> serviceExecutorChannels.getOrPut(entity, newChannel())
ServiceEventsTopic -> serviceEventsChannels.getOrPut(entity, newChannel())
WorkflowCmdTopic -> workflowCmdChannels.getOrPut(entity, newChannel())
WorkflowEngineTopic -> workflowEngineChannels.getOrPut(entity, newChannel())
WorkflowEventsTopic -> workflowEventsChannels.getOrPut(entity, newChannel())
WorkflowTaskExecutorTopic -> workflowTaskExecutorChannels.getOrPut(entity, newChannel())
WorkflowTaskEventsTopic -> workflowTaskEventsChannels.getOrPut(entity, newChannel())
else -> thisShouldNotHappen()
} as Channel
@Suppress("UNCHECKED_CAST")
fun Topic.channelForDelayed(entity: String): Channel> {
return when (this) {
DelayedServiceExecutorTopic -> delayedServiceExecutorChannels.getOrPut(entity, newChannel())
DelayedWorkflowEngineTopic -> delayedWorkflowEngineChannels.getOrPut(entity, newChannel())
DelayedWorkflowTaskExecutorTopic -> delayedWorkflowTaskExecutorChannels.getOrPut(
entity,
newChannel(),
)
else -> thisShouldNotHappen()
} as Channel>
}
private fun newChannel(): () -> Channel = { Channel(Channel.UNLIMITED) }
}
internal val Channel<*>.id
get() = System.identityHashCode(this)
data class DelayedMessage(
val message: T,
val after: MillisDuration
)