Skip to content

Commit

Permalink
Guard write rejection checks #258
Browse files Browse the repository at this point in the history
Protect rejection checks agains visibility and race issues in case a disconnect is right now in progress while a command gets written.
  • Loading branch information
mp911de committed May 31, 2016
1 parent 8971f88 commit 513729d
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/main/java/com/lambdaworks/redis/protocol/CommandHandler.java
Expand Up @@ -211,27 +211,28 @@ public <T> RedisCommand<K, V, T> write(RedisCommand<K, V, T> command) {

checkArgument(command != null, "command must not be null");

if (lifecycleState == LifecycleState.CLOSED) {
throw new RedisException("Connection is closed");
}

if (commandBuffer.size() + queue.size() >= clientOptions.getRequestQueueSize()) {
throw new RedisException("Request queue size exceeded: " + clientOptions.getRequestQueueSize()
+ ". Commands are not accepted until the queue size drops.");
}

if ((channel == null || !isConnected()) && !clientOptions.isAutoReconnect()) {
throw new RedisException(
"Connection is in a disconnected state and reconnect is disabled. Commands are not accepted.");
}

try {
/**
* This lock causes safety for connection activation and somehow netty gets more stable and predictable performance
* than without a lock and all threads are hammering towards writeAndFlush.
*/

writeLock.lock();

if (lifecycleState == LifecycleState.CLOSED) {
throw new RedisException("Connection is closed");
}

if (commandBuffer.size() + queue.size() >= clientOptions.getRequestQueueSize()) {
throw new RedisException("Request queue size exceeded: " + clientOptions.getRequestQueueSize()
+ ". Commands are not accepted until the queue size drops.");
}

if ((channel == null || !isConnected()) && !clientOptions.isAutoReconnect()) {
throw new RedisException(
"Connection is in a disconnected state and reconnect is disabled. Commands are not accepted.");
}

Channel channel = this.channel;
if (autoFlushCommands) {

Expand Down

0 comments on commit 513729d

Please sign in to comment.