Skip to content

Commit

Permalink
Configurable mongo timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
pitagoras3 committed Apr 12, 2024
1 parent 2219153 commit 38efb69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 38efb69

Please sign in to comment.