diff --git a/mongo-migration-stream-core/src/main/kotlin/pl/allegro/tech/mongomigrationstream/configuration/MongoProperties.kt b/mongo-migration-stream-core/src/main/kotlin/pl/allegro/tech/mongomigrationstream/configuration/MongoProperties.kt index 28aeb8a..e27c717 100644 --- a/mongo-migration-stream-core/src/main/kotlin/pl/allegro/tech/mongomigrationstream/configuration/MongoProperties.kt +++ b/mongo-migration-stream-core/src/main/kotlin/pl/allegro/tech/mongomigrationstream/configuration/MongoProperties.kt @@ -1,9 +1,14 @@ package pl.allegro.tech.mongomigrationstream.configuration +import java.time.Duration + +private val DEFAULT_MONGO_TIMEOUT = Duration.ofSeconds(30) + data class MongoProperties( val uri: String, val dbName: String, - val authenticationProperties: MongoAuthenticationProperties? = null + val authenticationProperties: MongoAuthenticationProperties? = null, + val timeoutProperties: MongoTimeoutProperties = MongoTimeoutProperties.DEFAULT, // TODO: Add possibility to read those values from properties file ) { override fun toString(): String = uri @@ -12,4 +17,18 @@ data class MongoProperties( val password: String, val authDbName: String ) + + data class MongoTimeoutProperties( + val connectTimeout: Duration, + val readTimeout: Duration, + val serverSelectionTimeout: Duration, + ) { + companion object { + val DEFAULT = MongoTimeoutProperties( + connectTimeout = DEFAULT_MONGO_TIMEOUT, + readTimeout = DEFAULT_MONGO_TIMEOUT, + serverSelectionTimeout = DEFAULT_MONGO_TIMEOUT, + ) + } + } } diff --git a/mongo-migration-stream-core/src/main/kotlin/pl/allegro/tech/mongomigrationstream/infrastructure/mongo/MongoClientFactory.kt b/mongo-migration-stream-core/src/main/kotlin/pl/allegro/tech/mongomigrationstream/infrastructure/mongo/MongoClientFactory.kt index 5538d65..4fe1a83 100644 --- a/mongo-migration-stream-core/src/main/kotlin/pl/allegro/tech/mongomigrationstream/infrastructure/mongo/MongoClientFactory.kt +++ b/mongo-migration-stream-core/src/main/kotlin/pl/allegro/tech/mongomigrationstream/infrastructure/mongo/MongoClientFactory.kt @@ -13,11 +13,7 @@ import java.util.concurrent.TimeUnit.SECONDS import com.mongodb.reactivestreams.client.MongoClient as ReactiveMongoClient import com.mongodb.reactivestreams.client.MongoClients as ReactiveMongoClients -private const val TIMEOUT = 10 - internal object MongoClientFactory { - private val mongoTimeout = TIMEOUT to SECONDS - internal fun buildClient( mongoProperties: MongoProperties, meterRegistry: MeterRegistry @@ -34,11 +30,11 @@ internal object MongoClientFactory { ): MongoClientSettings = MongoClientSettings.builder() .applyConnectionString(ConnectionString(mongoProperties.uri)) .applyToSocketSettings { - it.connectTimeout(mongoTimeout.first, mongoTimeout.second) - it.readTimeout(mongoTimeout.first, mongoTimeout.second) + it.connectTimeout(mongoProperties.timeoutProperties.connectTimeout.seconds.toInt(), SECONDS) + it.readTimeout(mongoProperties.timeoutProperties.readTimeout.seconds.toInt(), SECONDS) } .applyToClusterSettings { - it.serverSelectionTimeout(mongoTimeout.first.toLong(), mongoTimeout.second) + it.serverSelectionTimeout(mongoProperties.timeoutProperties.serverSelectionTimeout.seconds, SECONDS) } .addCommandListener(MongoMetricsCommandListener(meterRegistry)) .applyToConnectionPoolSettings {