Prevent multiple instances within the same JVM from trying to start aeron at the same time

This commit is contained in:
Robinson 2022-03-23 18:31:38 +01:00
parent 3cac148db6
commit 5c854c4fab
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C

View File

@ -11,6 +11,8 @@ import io.aeron.Subscription
import io.aeron.driver.MediaDriver
import io.aeron.exceptions.DriverTimeoutException
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import mu.KLogger
import mu.KotlinLogging
import org.agrona.concurrent.BackoffIdleStrategy
@ -54,6 +56,9 @@ class AeronDriver(
// AERON_PUBLICATION_LINGER_TIMEOUT, 5s by default (this can also be set as a URI param)
private const val AERON_PUBLICATION_LINGER_TIMEOUT = 5_000L // in MS
// prevents multiple instances, within the same JVM, from starting at the exact same time.
private val startMutex = Mutex()
private fun setConfigDefaults(config: Configuration, logger: KLogger) {
// explicitly don't set defaults if we already have the context defined!
if (config.contextDefined) {
@ -312,25 +317,25 @@ class AeronDriver(
return context
}
/**
* If the driver is not already running, this will start the driver
*
* @throws Exception if there is a problem starting the media driver
*/
@Synchronized
fun start() {
if (closeRequested.value) {
logger.debug("Resetting media driver context")
suspend fun start() {
startMutex.withLock {
if (closeRequested.value) {
logger.debug("Resetting media driver context")
// close was requested previously. we have to reset a few things
context = createContext()
// close was requested previously. we have to reset a few things
context = createContext()
// we want to be able to "restart" aeron, as necessary, after a [close] method call
closeRequested.value = false
// we want to be able to "restart" aeron, as necessary, after a [close] method call
closeRequested.value = false
}
restart()
}
restart()
}
/**