writeLock
This commit is contained in:
@@ -53,6 +53,8 @@ public abstract class AsyncConnection implements Channel, AutoCloseable {
|
|||||||
|
|
||||||
protected final int bufferCapacity;
|
protected final int bufferCapacity;
|
||||||
|
|
||||||
|
private final ReentrantLock writeLock = new ReentrantLock();
|
||||||
|
|
||||||
protected AsyncIOThread ioReadThread;
|
protected AsyncIOThread ioReadThread;
|
||||||
|
|
||||||
protected AsyncIOThread ioWriteThread;
|
protected AsyncIOThread ioWriteThread;
|
||||||
@@ -65,8 +67,6 @@ public abstract class AsyncConnection implements Channel, AutoCloseable {
|
|||||||
|
|
||||||
private Consumer<ByteBuffer> writeBufferConsumer;
|
private Consumer<ByteBuffer> writeBufferConsumer;
|
||||||
|
|
||||||
final ReentrantLock pipelineLock = new ReentrantLock();
|
|
||||||
|
|
||||||
private ByteBufferWriter pipelineWriter;
|
private ByteBufferWriter pipelineWriter;
|
||||||
|
|
||||||
private PipelineDataNode pipelineDataNode;
|
private PipelineDataNode pipelineDataNode;
|
||||||
@@ -205,6 +205,14 @@ public abstract class AsyncConnection implements Channel, AutoCloseable {
|
|||||||
return ioWriteThread;
|
return ioWriteThread;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final void lockWrite() {
|
||||||
|
writeLock.lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void unlockWrite() {
|
||||||
|
writeLock.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快速发送
|
* 快速发送
|
||||||
*
|
*
|
||||||
@@ -615,7 +623,7 @@ public abstract class AsyncConnection implements Channel, AutoCloseable {
|
|||||||
|
|
||||||
// 返回pipelineCount个数数据是否全部写入完毕
|
// 返回pipelineCount个数数据是否全部写入完毕
|
||||||
public boolean appendPipeline(int pipelineIndex, int pipelineCount, byte[] bs, int offset, int length) {
|
public boolean appendPipeline(int pipelineIndex, int pipelineCount, byte[] bs, int offset, int length) {
|
||||||
pipelineLock.lock();
|
writeLock.lock();
|
||||||
try {
|
try {
|
||||||
ByteBufferWriter writer = this.pipelineWriter;
|
ByteBufferWriter writer = this.pipelineWriter;
|
||||||
if (writer == null) {
|
if (writer == null) {
|
||||||
@@ -645,7 +653,7 @@ public abstract class AsyncConnection implements Channel, AutoCloseable {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
pipelineLock.unlock();
|
writeLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -672,7 +680,7 @@ public abstract class AsyncConnection implements Channel, AutoCloseable {
|
|||||||
byte[] bodyContent,
|
byte[] bodyContent,
|
||||||
int bodyOffset,
|
int bodyOffset,
|
||||||
int bodyLength) {
|
int bodyLength) {
|
||||||
pipelineLock.lock();
|
writeLock.lock();
|
||||||
try {
|
try {
|
||||||
ByteBufferWriter writer = this.pipelineWriter;
|
ByteBufferWriter writer = this.pipelineWriter;
|
||||||
if (writer == null) {
|
if (writer == null) {
|
||||||
@@ -703,7 +711,7 @@ public abstract class AsyncConnection implements Channel, AutoCloseable {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
pipelineLock.unlock();
|
writeLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -160,13 +160,13 @@ abstract class AsyncNioConnection extends AsyncConnection {
|
|||||||
@Override
|
@Override
|
||||||
public final void pipelineWrite(PipelinePacket... packets) {
|
public final void pipelineWrite(PipelinePacket... packets) {
|
||||||
if (pipelineWriteQueue == null) {
|
if (pipelineWriteQueue == null) {
|
||||||
pipelineLock.lock();
|
writeLock.lock();
|
||||||
try {
|
try {
|
||||||
if (pipelineWriteQueue == null) {
|
if (pipelineWriteQueue == null) {
|
||||||
pipelineWriteQueue = new ConcurrentLinkedDeque<>();
|
pipelineWriteQueue = new ConcurrentLinkedDeque<>();
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
pipelineLock.unlock();
|
writeLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (PipelinePacket packet : packets) {
|
for (PipelinePacket packet : packets) {
|
||||||
@@ -241,7 +241,7 @@ abstract class AsyncNioConnection extends AsyncConnection {
|
|||||||
handler.failed(exc, attachment);
|
handler.failed(exc, attachment);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
this.writeCompletionHandler = (CompletionHandler) newHandler;
|
this.writeCompletionHandler = newHandler;
|
||||||
doWrite(); // 如果不是true,则bodyCallback的执行可能会切换线程
|
doWrite(); // 如果不是true,则bodyCallback的执行可能会切换线程
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,7 +260,7 @@ abstract class AsyncNioConnection extends AsyncConnection {
|
|||||||
this.writePending = true;
|
this.writePending = true;
|
||||||
this.writeByteBuffer = src;
|
this.writeByteBuffer = src;
|
||||||
this.writeAttachment = attachment;
|
this.writeAttachment = attachment;
|
||||||
this.writeCompletionHandler = (CompletionHandler) handler;
|
this.writeCompletionHandler = handler;
|
||||||
doWrite();
|
doWrite();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,7 +282,7 @@ abstract class AsyncNioConnection extends AsyncConnection {
|
|||||||
this.writeBuffersOffset = offset;
|
this.writeBuffersOffset = offset;
|
||||||
this.writeBuffersLength = length;
|
this.writeBuffersLength = length;
|
||||||
this.writeAttachment = attachment;
|
this.writeAttachment = attachment;
|
||||||
this.writeCompletionHandler = (CompletionHandler) handler;
|
this.writeCompletionHandler = handler;
|
||||||
doWrite();
|
doWrite();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import java.nio.channels.*;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.*;
|
import java.util.concurrent.atomic.*;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import org.redkale.annotation.*;
|
import org.redkale.annotation.*;
|
||||||
@@ -38,8 +37,6 @@ public abstract class ClientConnection<R extends ClientRequest, P extends Client
|
|||||||
|
|
||||||
protected final LongAdder doneResponseCounter = new LongAdder();
|
protected final LongAdder doneResponseCounter = new LongAdder();
|
||||||
|
|
||||||
protected final ReentrantLock writeLock = new ReentrantLock();
|
|
||||||
|
|
||||||
protected final ByteArray writeArray = new ByteArray();
|
protected final ByteArray writeArray = new ByteArray();
|
||||||
|
|
||||||
protected final ByteBuffer writeBuffer;
|
protected final ByteBuffer writeBuffer;
|
||||||
@@ -139,7 +136,7 @@ public abstract class ClientConnection<R extends ClientRequest, P extends Client
|
|||||||
}
|
}
|
||||||
respWaitingCounter.add(respFutures.length); // 放在writeChannelInWriteThread计数会延迟,导致不准确
|
respWaitingCounter.add(respFutures.length); // 放在writeChannelInWriteThread计数会延迟,导致不准确
|
||||||
|
|
||||||
writeLock.lock();
|
channel.lockWrite();
|
||||||
try {
|
try {
|
||||||
if (pauseWriting.get()) {
|
if (pauseWriting.get()) {
|
||||||
for (ClientFuture respFuture : respFutures) {
|
for (ClientFuture respFuture : respFutures) {
|
||||||
@@ -150,15 +147,15 @@ public abstract class ClientConnection<R extends ClientRequest, P extends Client
|
|||||||
for (ClientFuture respFuture : respFutures) {
|
for (ClientFuture respFuture : respFutures) {
|
||||||
offerRespFuture(respFuture);
|
offerRespFuture(respFuture);
|
||||||
}
|
}
|
||||||
sendRequestInLocking(respFutures);
|
sendRequestInLock(respFutures);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
writeLock.unlock();
|
channel.unlockWrite();
|
||||||
}
|
}
|
||||||
return respFutures;
|
return respFutures;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void sendRequestInLocking(ClientFuture... respFutures) {
|
protected void sendRequestInLock(ClientFuture... respFutures) {
|
||||||
sendRequestToChannel(respFutures);
|
sendRequestToChannel(respFutures);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +190,7 @@ public abstract class ClientConnection<R extends ClientRequest, P extends Client
|
|||||||
|
|
||||||
// 发送半包和积压的请求数据包
|
// 发送半包和积压的请求数据包
|
||||||
void sendHalfWriteInReadThread(R halfRequest, Throwable halfException) {
|
void sendHalfWriteInReadThread(R halfRequest, Throwable halfException) {
|
||||||
writeLock.lock();
|
channel.lockWrite();
|
||||||
try {
|
try {
|
||||||
pauseWriting.set(false);
|
pauseWriting.set(false);
|
||||||
ClientFuture respFuture = this.currHalfWriteFuture;
|
ClientFuture respFuture = this.currHalfWriteFuture;
|
||||||
@@ -210,7 +207,7 @@ public abstract class ClientConnection<R extends ClientRequest, P extends Client
|
|||||||
sendRequestToChannel(respFuture);
|
sendRequestToChannel(respFuture);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
writeLock.unlock();
|
channel.unlockWrite();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,14 +216,15 @@ public abstract class ClientConnection<R extends ClientRequest, P extends Client
|
|||||||
return CompletableFuture.failedFuture(
|
return CompletableFuture.failedFuture(
|
||||||
new RuntimeException("ClientVirtualRequest must be virtualType = true"));
|
new RuntimeException("ClientVirtualRequest must be virtualType = true"));
|
||||||
}
|
}
|
||||||
|
AsyncConnection conn = channel;
|
||||||
ClientFuture<R, P> respFuture = client.createClientFuture(this, request);
|
ClientFuture<R, P> respFuture = client.createClientFuture(this, request);
|
||||||
writeLock.lock();
|
conn.lockWrite();
|
||||||
try {
|
try {
|
||||||
offerRespFuture(respFuture);
|
offerRespFuture(respFuture);
|
||||||
} finally {
|
} finally {
|
||||||
writeLock.unlock();
|
conn.unlockWrite();
|
||||||
}
|
}
|
||||||
channel.readRegister(getCodec()); // 不能在创建连接时注册读事件
|
conn.readRegister(getCodec()); // 不能在创建连接时注册读事件
|
||||||
return respFuture;
|
return respFuture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user