修复writeVirtualRequest模式问题
This commit is contained in:
@@ -176,7 +176,7 @@ public abstract class Client<C extends ClientConnection<R, P>, R extends ClientR
|
|||||||
|
|
||||||
protected abstract C createClientConnection(final int index, AsyncConnection channel);
|
protected abstract C createClientConnection(final int index, AsyncConnection channel);
|
||||||
|
|
||||||
//创建连接后先从服务器拉取数据构建的虚拟请求,返回null表示连上服务器后不读取数据
|
//创建连接后会立马从服务器拉取数据构建的虚拟请求,返回null表示连上服务器后不会立马读取数据
|
||||||
protected R createVirtualRequestAfterConnect() {
|
protected R createVirtualRequestAfterConnect() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -298,14 +298,6 @@ public abstract class Client<C extends ClientConnection<R, P>, R extends ClientR
|
|||||||
return conn.writeChannel(requests, respTransfer);
|
return conn.writeChannel(requests, respTransfer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private C createConnection(int index, AsyncConnection channel) {
|
|
||||||
C conn = createClientConnection(index, channel);
|
|
||||||
if (!channel.isReadPending()) {
|
|
||||||
channel.readRegister(conn.getCodec()); //不用readRegisterInIOThread,因executeRead可能会异步
|
|
||||||
}
|
|
||||||
return conn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final CompletableFuture<C> connect() {
|
public final CompletableFuture<C> connect() {
|
||||||
final int size = this.connArray.length;
|
final int size = this.connArray.length;
|
||||||
WorkThread workThread = WorkThread.currentWorkThread();
|
WorkThread workThread = WorkThread.currentWorkThread();
|
||||||
@@ -317,10 +309,15 @@ public abstract class Client<C extends ClientConnection<R, P>, R extends ClientR
|
|||||||
final Queue<CompletableFuture<C>> waitQueue = this.connAcquireWaitings[connIndex];
|
final Queue<CompletableFuture<C>> waitQueue = this.connAcquireWaitings[connIndex];
|
||||||
if (this.connOpenStates[connIndex].compareAndSet(false, true)) {
|
if (this.connOpenStates[connIndex].compareAndSet(false, true)) {
|
||||||
CompletableFuture<C> future = group.createClient(tcp, this.address.randomAddress(), readTimeoutSeconds, writeTimeoutSeconds)
|
CompletableFuture<C> future = group.createClient(tcp, this.address.randomAddress(), readTimeoutSeconds, writeTimeoutSeconds)
|
||||||
.thenApply(c -> (C) createConnection(connIndex, c).setMaxPipelines(maxPipelines));
|
.thenApply(c -> (C) createClientConnection(connIndex, c).setMaxPipelines(maxPipelines));
|
||||||
R virtualReq = createVirtualRequestAfterConnect();
|
R virtualReq = createVirtualRequestAfterConnect();
|
||||||
if (virtualReq != null) {
|
if (virtualReq != null) {
|
||||||
future = future.thenCompose(conn -> conn.writeVirtualRequest(virtualReq).thenApply(v -> conn));
|
future = future.thenCompose(conn -> conn.writeVirtualRequest(virtualReq).thenApply(v -> conn));
|
||||||
|
} else {
|
||||||
|
future = future.thenApply(conn -> {
|
||||||
|
conn.channel.readRegister(conn.getCodec()); //不用readRegisterInIOThread,因executeRead可能会异步
|
||||||
|
return conn;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (authenticate != null) {
|
if (authenticate != null) {
|
||||||
future = future.thenCompose(authenticate);
|
future = future.thenCompose(authenticate);
|
||||||
@@ -362,10 +359,15 @@ public abstract class Client<C extends ClientConnection<R, P>, R extends ClientR
|
|||||||
final Queue<CompletableFuture<C>> waitQueue = entry.connAcquireWaitings;
|
final Queue<CompletableFuture<C>> waitQueue = entry.connAcquireWaitings;
|
||||||
if (entry.connOpenState.compareAndSet(false, true)) {
|
if (entry.connOpenState.compareAndSet(false, true)) {
|
||||||
CompletableFuture<C> future = group.createClient(tcp, addr, readTimeoutSeconds, writeTimeoutSeconds)
|
CompletableFuture<C> future = group.createClient(tcp, addr, readTimeoutSeconds, writeTimeoutSeconds)
|
||||||
.thenApply(c -> (C) createConnection(-1, c).setMaxPipelines(maxPipelines));
|
.thenApply(c -> (C) createClientConnection(-1, c).setMaxPipelines(maxPipelines));
|
||||||
R virtualReq = createVirtualRequestAfterConnect();
|
R virtualReq = createVirtualRequestAfterConnect();
|
||||||
if (virtualReq != null) {
|
if (virtualReq != null) {
|
||||||
future = future.thenCompose(conn -> conn.writeVirtualRequest(virtualReq).thenApply(v -> conn));
|
future = future.thenCompose(conn -> conn.writeVirtualRequest(virtualReq).thenApply(v -> conn));
|
||||||
|
} else {
|
||||||
|
future = future.thenApply(conn -> {
|
||||||
|
conn.channel.readRegister(conn.getCodec()); //不用readRegisterInIOThread,因executeRead可能会异步
|
||||||
|
return conn;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (authenticate != null) {
|
if (authenticate != null) {
|
||||||
future = future.thenCompose(authenticate);
|
future = future.thenCompose(authenticate);
|
||||||
|
|||||||
@@ -74,6 +74,10 @@ public abstract class ClientCodec<R extends ClientRequest, P> implements Complet
|
|||||||
} else {
|
} else {
|
||||||
ClientFuture<R, P> respFuture = connection.pollRespFuture(cr.getRequestid());
|
ClientFuture<R, P> respFuture = connection.pollRespFuture(cr.getRequestid());
|
||||||
if (respFuture != null) {
|
if (respFuture != null) {
|
||||||
|
if (respFuture.request != cr.request) {
|
||||||
|
connection.dispose(new RedkaleException("request pipeline error"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
responseComplete(false, respFuture, cr.message, cr.exc);
|
responseComplete(false, respFuture, cr.message, cr.exc);
|
||||||
}
|
}
|
||||||
respPool.accept(cr);
|
respPool.accept(cr);
|
||||||
|
|||||||
@@ -258,6 +258,7 @@ public abstract class ClientConnection<R extends ClientRequest, P> implements Co
|
|||||||
} finally {
|
} finally {
|
||||||
writeLock.unlock();
|
writeLock.unlock();
|
||||||
}
|
}
|
||||||
|
channel.readRegister(getCodec()); //不能在创建连接时注册读事件
|
||||||
return respFuture;
|
return respFuture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user