Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix object borrowing #76

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins {

allprojects {
group = "com.himadieiev"
version = "1.3.1"
version = "1.3.2"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ import org.apache.commons.pool2.impl.GenericObjectPool
abstract class AbstractLettucePooled<K, V, T : StatefulConnection<K, V>>(
protected val connectionPool: GenericObjectPool<T>,
) : AutoCloseable, LettuceUnified<K, V> {
protected val logger = KotlinLogging.logger { }

init {
connectionPool.borrowObject().use { connection ->
if (connection !is StatefulRedisConnection<*, *> && connection !is StatefulRedisClusterConnection<*, *>) {
throw IllegalArgumentException(
"Connection pool must be of type StatefulRedisConnection or StatefulRedisClusterConnection.",
)
}
val connection = connectionPool.borrowObject()
if (connection !is StatefulRedisConnection<*, *> && connection !is StatefulRedisClusterConnection<*, *>) {
throw IllegalArgumentException(
"Connection pool must be of type StatefulRedisConnection or StatefulRedisClusterConnection.",
)
}
connectionPool.returnObject(connection)
}

override fun close() {
Expand Down Expand Up @@ -56,11 +58,14 @@ abstract class AbstractLettucePooled<K, V, T : StatefulConnection<K, V>>(
try {
connection.sync().discard()
} catch (e: Exception) {
val logger = KotlinLogging.logger { }
logger.error(e) { "Could not discard transaction." }
logger.error(e) { "Could not discard a transaction." }
}
}
connectionPool.returnObject(connection)
try {
connectionPool.returnObject(connection)
} catch (e: IllegalStateException) {
logger.info { "Failed to return connection to the pool. Skipping error: " + e.message }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class LettucePooledTest {

verify(exactly = 1) {
connection.sync()
connectionPool.returnObject(eq(connection))
}
verify(exactly = 2) {
connectionPool.returnObject(eq(connection))
connectionPool.borrowObject()
}
}
Expand All @@ -54,10 +54,10 @@ class LettucePooledTest {

verify(exactly = 1) {
sync.discard()
connectionPool.returnObject(eq(connection))
}
verify(exactly = 2) {
connectionPool.borrowObject()
connectionPool.returnObject(eq(connection))
connection.sync()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class LettucePubSubPooledTest {

verify(exactly = 1) {
connection.sync()
connectionPool.returnObject(eq(connection))
}
verify(exactly = 2) {
connectionPool.borrowObject()
connectionPool.returnObject(eq(connection))
}
}

Expand All @@ -54,11 +54,11 @@ class LettucePubSubPooledTest {

verify(exactly = 1) {
sync.discard()
connectionPool.returnObject(eq(connection))
}
verify(exactly = 2) {
connection.sync()
connectionPool.borrowObject()
connectionPool.returnObject(eq(connection))
}
}

Expand Down
Loading