diff --git a/src/main/java/io/lettuce/core/cluster/PooledClusterConnectionProvider.java b/src/main/java/io/lettuce/core/cluster/PooledClusterConnectionProvider.java index 96356f4643..96299c24ca 100644 --- a/src/main/java/io/lettuce/core/cluster/PooledClusterConnectionProvider.java +++ b/src/main/java/io/lettuce/core/cluster/PooledClusterConnectionProvider.java @@ -104,9 +104,10 @@ public CompletableFuture> getConnectionAsync(Inten logger.debug("getConnection(" + intent + ", " + slot + ")"); } - if (intent == Intent.READ && readFrom != null) { + if (intent == Intent.READ && readFrom != null && readFrom != ReadFrom.MASTER) { return getReadConnection(slot); } + return getWriteConnection(slot).toCompletableFuture(); } diff --git a/src/test/java/io/lettuce/core/cluster/PooledClusterConnectionProviderUnitTests.java b/src/test/java/io/lettuce/core/cluster/PooledClusterConnectionProviderUnitTests.java index d3167bd331..0ed404f104 100644 --- a/src/test/java/io/lettuce/core/cluster/PooledClusterConnectionProviderUnitTests.java +++ b/src/test/java/io/lettuce/core/cluster/PooledClusterConnectionProviderUnitTests.java @@ -125,6 +125,21 @@ void shouldObtainConnection() { verifyNoMoreInteractions(connection); } + @Test + void shouldReuseMasterConnectionForReadFromMaster() { + + when(clientMock.connectToNodeAsync(eq(CODEC), eq("localhost:1"), any(), any())).thenReturn( + ConnectionFuture.from(socketAddressMock, CompletableFuture.completedFuture(nodeConnectionMock))); + + sut.setReadFrom(ReadFrom.MASTER); + + StatefulRedisConnection connection = sut.getConnection(Intent.READ, 1); + + assertThat(connection).isSameAs(nodeConnectionMock); + verify(connection).setAutoFlushCommands(true); + verifyNoMoreInteractions(connection); + } + @Test void shouldObtainConnectionReadFromSlave() {