优化net模块
This commit is contained in:
@@ -92,7 +92,7 @@ public class AsyncIOThread extends WorkThread {
|
|||||||
* @param command 操作
|
* @param command 操作
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void execute(Runnable command) {
|
public final void execute(Runnable command) {
|
||||||
commandQueue.offer(command);
|
commandQueue.offer(command);
|
||||||
selector.wakeup();
|
selector.wakeup();
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,7 @@ public class AsyncIOThread extends WorkThread {
|
|||||||
* @param commands 操作
|
* @param commands 操作
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void execute(Runnable... commands) {
|
public final void execute(Runnable... commands) {
|
||||||
for (Runnable command : commands) {
|
for (Runnable command : commands) {
|
||||||
commandQueue.offer(command);
|
commandQueue.offer(command);
|
||||||
}
|
}
|
||||||
@@ -116,7 +116,7 @@ public class AsyncIOThread extends WorkThread {
|
|||||||
* @param commands 操作
|
* @param commands 操作
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void execute(Collection<Runnable> commands) {
|
public final void execute(Collection<Runnable> commands) {
|
||||||
if (commands != null) {
|
if (commands != null) {
|
||||||
for (Runnable command : commands) {
|
for (Runnable command : commands) {
|
||||||
commandQueue.offer(command);
|
commandQueue.offer(command);
|
||||||
|
|||||||
@@ -86,6 +86,11 @@ public class WorkThread extends Thread implements Executor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//与execute的区别在于子类AsyncIOThread中execute会被重载,确保在IO线程中执行
|
||||||
|
public final void runWork(Runnable command) {
|
||||||
|
execute(command);
|
||||||
|
}
|
||||||
|
|
||||||
public void execute(Runnable... commands) {
|
public void execute(Runnable... commands) {
|
||||||
if (workExecutor == null) {
|
if (workExecutor == null) {
|
||||||
for (Runnable command : commands) {
|
for (Runnable command : commands) {
|
||||||
@@ -113,22 +118,6 @@ public class WorkThread extends Thread implements Executor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void runWork(Runnable command) {
|
|
||||||
if (workExecutor == null) {
|
|
||||||
command.run();
|
|
||||||
} else {
|
|
||||||
workExecutor.execute(command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void runAsync(Runnable command) {
|
|
||||||
if (workExecutor == null) {
|
|
||||||
ForkJoinPool.commonPool().execute(command);
|
|
||||||
} else {
|
|
||||||
workExecutor.execute(command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutorService getWorkExecutor() {
|
public ExecutorService getWorkExecutor() {
|
||||||
return workExecutor;
|
return workExecutor;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -123,21 +123,11 @@ public abstract class ClientCodec<R extends ClientRequest, P> implements Complet
|
|||||||
connection.preComplete(message, (R) request, exc);
|
connection.preComplete(message, (R) request, exc);
|
||||||
|
|
||||||
if (exc != null) {
|
if (exc != null) {
|
||||||
if (workThread == readThread) {
|
if (workThread.inIO()) {
|
||||||
workThread.runWork(() -> {
|
workThread.execute(() -> {
|
||||||
Traces.currentTraceid(request.traceid);
|
Traces.currentTraceid(request.traceid);
|
||||||
respFuture.completeExceptionally(exc);
|
respFuture.completeExceptionally(exc);
|
||||||
});
|
});
|
||||||
} else if (workThread.getState() == Thread.State.RUNNABLE) {
|
|
||||||
if (workThread.inIO()) {
|
|
||||||
Traces.currentTraceid(request.traceid);
|
|
||||||
respFuture.completeExceptionally(exc);
|
|
||||||
} else {
|
|
||||||
workThread.execute(() -> {
|
|
||||||
Traces.currentTraceid(request.traceid);
|
|
||||||
respFuture.completeExceptionally(exc);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
workThread.runWork(() -> {
|
workThread.runWork(() -> {
|
||||||
Traces.currentTraceid(request.traceid);
|
Traces.currentTraceid(request.traceid);
|
||||||
@@ -146,21 +136,11 @@ public abstract class ClientCodec<R extends ClientRequest, P> implements Complet
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
final P rs = request.respTransfer == null ? message : (P) request.respTransfer.apply(message);
|
final P rs = request.respTransfer == null ? message : (P) request.respTransfer.apply(message);
|
||||||
if (workThread == readThread) {
|
if (workThread.inIO()) {
|
||||||
workThread.runWork(() -> {
|
workThread.execute(() -> {
|
||||||
Traces.currentTraceid(request.traceid);
|
Traces.currentTraceid(request.traceid);
|
||||||
respFuture.complete(rs);
|
respFuture.complete(rs);
|
||||||
});
|
});
|
||||||
} else if (workThread.getState() == Thread.State.RUNNABLE) {
|
|
||||||
if (workThread.inIO()) {
|
|
||||||
Traces.currentTraceid(request.traceid);
|
|
||||||
respFuture.complete(rs);
|
|
||||||
} else {
|
|
||||||
workThread.execute(() -> {
|
|
||||||
Traces.currentTraceid(request.traceid);
|
|
||||||
respFuture.complete(rs);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
workThread.runWork(() -> {
|
workThread.runWork(() -> {
|
||||||
Traces.currentTraceid(request.traceid);
|
Traces.currentTraceid(request.traceid);
|
||||||
|
|||||||
@@ -191,9 +191,11 @@ public abstract class ClientConnection<R extends ClientRequest, P> implements Co
|
|||||||
} else {
|
} else {
|
||||||
channel.write(writeArray, this, writeHandler);
|
channel.write(writeArray, this, writeHandler);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
writePending.compareAndSet(true, false);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
requestQueue.add(respFuture);
|
requestQueue.offer(respFuture);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ public abstract class WebSocket<G extends Serializable, T> {
|
|||||||
public static final int RETCODE_WSOFFLINE = 1 << 8; //256
|
public static final int RETCODE_WSOFFLINE = 1 << 8; //256
|
||||||
|
|
||||||
@Comment("WebSocket将延迟发送")
|
@Comment("WebSocket将延迟发送")
|
||||||
public static final int RETCODE_DEAYSEND = 1 << 9; //512
|
public static final int RETCODE_DELAYSEND = 1 << 9; //512
|
||||||
|
|
||||||
WebSocketEngine _engine; //不可能为空
|
WebSocketEngine _engine; //不可能为空
|
||||||
|
|
||||||
@@ -242,7 +242,7 @@ public abstract class WebSocket<G extends Serializable, T> {
|
|||||||
delayPackets = new ArrayList<>();
|
delayPackets = new ArrayList<>();
|
||||||
}
|
}
|
||||||
delayPackets.add(packet);
|
delayPackets.add(packet);
|
||||||
return CompletableFuture.completedFuture(RETCODE_DEAYSEND);
|
return CompletableFuture.completedFuture(RETCODE_DELAYSEND);
|
||||||
}
|
}
|
||||||
CompletableFuture<Integer> rs = this._writeHandler.send(packet); //this._writeIOThread.send(this, packet);
|
CompletableFuture<Integer> rs = this._writeHandler.send(packet); //this._writeIOThread.send(this, packet);
|
||||||
if (_engine.logger.isLoggable(Level.FINER) && packet != WebSocketPacket.DEFAULT_PING_PACKET) {
|
if (_engine.logger.isLoggable(Level.FINER) && packet != WebSocketPacket.DEFAULT_PING_PACKET) {
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ public class WebSocketWriteHandler implements CompletionHandler<Integer, Void> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void failed(Throwable exc, Void attachment) {
|
public void failed(Throwable exc, Void attachment) {
|
||||||
|
writePending.set(false);
|
||||||
WebSocketFuture req;
|
WebSocketFuture req;
|
||||||
try {
|
try {
|
||||||
while ((req = requestQueue.poll()) != null) {
|
while ((req = requestQueue.poll()) != null) {
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ public abstract class AbstractService implements Service {
|
|||||||
} else {
|
} else {
|
||||||
Thread thread = Thread.currentThread();
|
Thread thread = Thread.currentThread();
|
||||||
if (thread instanceof WorkThread) {
|
if (thread instanceof WorkThread) {
|
||||||
((WorkThread) thread).runAsync(command);
|
((WorkThread) thread).runWork(command);
|
||||||
} else {
|
} else {
|
||||||
ForkJoinPool.commonPool().execute(command);
|
ForkJoinPool.commonPool().execute(command);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -300,6 +300,7 @@ public final class Utility {
|
|||||||
if (methodName != null) {
|
if (methodName != null) {
|
||||||
return readFieldName(methodName);
|
return readFieldName(methodName);
|
||||||
} else {
|
} else {
|
||||||
|
//native-image环境下获取不到methodName
|
||||||
throw new RedkaleException("cannot found method-name from lambda " + func);
|
throw new RedkaleException("cannot found method-name from lambda " + func);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user