-
Notifications
You must be signed in to change notification settings - Fork 986
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use commandBuffer for pipelining instead of channel write #92
Use the commandBuffer when autoFlushCommands is disabled instead of writing commands to a channel and write the whole buffer when flushing. This change slightly improves the throughput of lettuce. Motivation: netty maintains a promise for every written command and handles buffering on its own. Writing commands one by one but delaying the flush has less flavor of batching than buffering commands and writing them as batch.
- Loading branch information
Showing
4 changed files
with
178 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
package com.lambdaworks.redis.protocol; | ||
|
||
import java.nio.charset.Charset; | ||
import java.util.Collection; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.Channel; | ||
|
@@ -18,7 +19,7 @@ | |
* @author <a href="mailto:[email protected]">Mark Paluch</a> | ||
*/ | ||
@ChannelHandler.Sharable | ||
public class CommandEncoder extends MessageToByteEncoder<RedisCommand<?, ?, ?>> { | ||
public class CommandEncoder extends MessageToByteEncoder<Object> { | ||
|
||
private static final InternalLogger logger = InternalLoggerFactory.getInstance(CommandEncoder.class); | ||
|
||
|
@@ -43,12 +44,28 @@ public CommandEncoder(boolean preferDirect) { | |
} | ||
|
||
@Override | ||
protected void encode(ChannelHandlerContext ctx, RedisCommand<?, ?, ?> msg, ByteBuf out) throws Exception { | ||
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception { | ||
|
||
msg.encode(out); | ||
if (msg instanceof RedisCommand) { | ||
RedisCommand<?, ?, ?> command = (RedisCommand<?, ?, ?>) msg; | ||
encode(ctx, out, command); | ||
} | ||
|
||
if (msg instanceof Collection) { | ||
Collection<RedisCommand<?, ?, ?>> commands = (Collection<RedisCommand<?, ?, ?>>) msg; | ||
for (RedisCommand<?, ?, ?> command : commands) { | ||
if (command.isCancelled()) { | ||
continue; | ||
} | ||
encode(ctx, out, command); | ||
} | ||
} | ||
} | ||
|
||
private void encode(ChannelHandlerContext ctx, ByteBuf out, RedisCommand<?, ?, ?> command) { | ||
command.encode(out); | ||
if (debugEnabled) { | ||
logger.debug("{} writing command {}", logPrefix(ctx.channel()), msg); | ||
logger.debug("{} writing command {}", logPrefix(ctx.channel()), command); | ||
if (traceEnabled) { | ||
logger.trace("{} Sent: {}", logPrefix(ctx.channel()), out.toString(Charset.defaultCharset()).trim()); | ||
} | ||
|
@@ -60,5 +77,4 @@ private String logPrefix(Channel channel) { | |
buffer.append('[').append(ChannelLogDescriptor.logDescriptor(channel)).append(']'); | ||
return buffer.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.