Don Mitchell
04/25/2025, 2:27 PMCompletableFuture
. I was shocked that the compiler didn't flag illegal casts and that in runtime, the caller logged but swallowed the cast exception. IDK if it was bc it was on a future or something up the stack from the caller that swallowed it. Here's the erroneous code (pool
is suspend
). The fix is to use RedisClusterCommands
not its subclass RedisCommands
fun <T> withBlockingConnection(body: (RedisCommands<String, String>) -> T): CompletableFuture<T> =
CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).future {
val connection = pool.borrowObject()
try {
val commands = when (connection) {
is StatefulRedisConnection -> connection.sync()
is StatefulRedisClusterConnection -> connection.sync()
else -> throw IllegalStateException("Redis connection is not a known type $connection")
} as RedisCommands<String, String>
body(commands)
} finally {
pool.returnObject(connection)
}
}
Joffrey
04/25/2025, 7:18 PM