Skip to content

Commit

Permalink
Use Cluster write connections for read commands when using ReadFrom.M…
Browse files Browse the repository at this point in the history
…ASTER #923

Lettuce now reuses master connections when ReadFrom is configured to MASTER (which is the default configuration). By reusing the master connection, we can reduce the number of used connections by up to 50% as we don't need an additional read connection while talking to the same node.
  • Loading branch information
mp911de committed Nov 24, 2018
1 parent 14db4fa commit ef0b15e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ public CompletableFuture<StatefulRedisConnection<K, V>> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> connection = sut.getConnection(Intent.READ, 1);

assertThat(connection).isSameAs(nodeConnectionMock);
verify(connection).setAutoFlushCommands(true);
verifyNoMoreInteractions(connection);
}

@Test
void shouldObtainConnectionReadFromSlave() {

Expand Down

0 comments on commit ef0b15e

Please sign in to comment.