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.driver.MediaDriver
import io.aeron.exceptions.DriverTimeoutException import io.aeron.exceptions.DriverTimeoutException
import kotlinx.atomicfu.atomic import kotlinx.atomicfu.atomic
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import mu.KLogger import mu.KLogger
import mu.KotlinLogging import mu.KotlinLogging
import org.agrona.concurrent.BackoffIdleStrategy 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) // 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 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) { private fun setConfigDefaults(config: Configuration, logger: KLogger) {
// explicitly don't set defaults if we already have the context defined! // explicitly don't set defaults if we already have the context defined!
if (config.contextDefined) { if (config.contextDefined) {
@ -312,25 +317,25 @@ class AeronDriver(
return context return context
} }
/** /**
* If the driver is not already running, this will start the driver * If the driver is not already running, this will start the driver
* *
* @throws Exception if there is a problem starting the media driver * @throws Exception if there is a problem starting the media driver
*/ */
@Synchronized suspend fun start() {
fun start() { startMutex.withLock {
if (closeRequested.value) { if (closeRequested.value) {
logger.debug("Resetting media driver context") logger.debug("Resetting media driver context")
// close was requested previously. we have to reset a few things // close was requested previously. we have to reset a few things
context = createContext() context = createContext()
// we want to be able to "restart" aeron, as necessary, after a [close] method call // we want to be able to "restart" aeron, as necessary, after a [close] method call
closeRequested.value = false closeRequested.value = false
}
restart()
} }
restart()
} }
/** /**