From 92fc58fcd07c2876b36fa785539543c54db3a88b Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 13 Oct 2024 11:51:08 +0800 Subject: [PATCH] feat: sync ask with timeout --- net/src/main/java/com/zfoo/net/router/IRouter.java | 2 ++ net/src/main/java/com/zfoo/net/router/Router.java | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/net/src/main/java/com/zfoo/net/router/IRouter.java b/net/src/main/java/com/zfoo/net/router/IRouter.java index c4433b574..d0fac026f 100644 --- a/net/src/main/java/com/zfoo/net/router/IRouter.java +++ b/net/src/main/java/com/zfoo/net/router/IRouter.java @@ -56,6 +56,8 @@ public interface IRouter { */ SyncAnswer syncAsk(Session session, Object packet, @Nullable Class answerClass, @Nullable Object argument) throws Exception; + SyncAnswer syncAsk(Session session, Object packet, @Nullable Class answerClass, @Nullable Object argument, long timeoutMillis) throws Exception; + AsyncAnswer asyncAsk(Session session, Object packet, @Nullable Class answerClass, @Nullable Object argument); AsyncAnswer asyncAsk(Session session, Object packet, @Nullable Class answerClass, @Nullable Object argument, long timeoutMillis); diff --git a/net/src/main/java/com/zfoo/net/router/Router.java b/net/src/main/java/com/zfoo/net/router/Router.java index 94fa2e775..665142746 100644 --- a/net/src/main/java/com/zfoo/net/router/Router.java +++ b/net/src/main/java/com/zfoo/net/router/Router.java @@ -223,6 +223,11 @@ public void send(Session session, Object packet) { @Override public SyncAnswer syncAsk(Session session, Object packet, @Nullable Class answerClass, @Nullable Object argument) throws Exception { + return syncAsk(session, packet, answerClass, argument, DEFAULT_TIMEOUT); + } + + @Override + public SyncAnswer syncAsk(Session session, Object packet, Class answerClass, Object argument, long timeoutMillis) throws Exception { var clientSignalAttachment = new SignalAttachment(); if (argument == null) { clientSignalAttachment.setClient(SignalAttachment.SIGNAL_NATIVE_NO_ARGUMENT_CLIENT); @@ -237,7 +242,7 @@ public SyncAnswer syncAsk(Session session, Object packet, @Nullable Class // 里面调用的依然是:send方法发送消息 send(session, packet, clientSignalAttachment); - Object responsePacket = clientSignalAttachment.getResponseFuture().get(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); + Object responsePacket = clientSignalAttachment.getResponseFuture().get(timeoutMillis, TimeUnit.MILLISECONDS); if (responsePacket.getClass() == Error.class) { throw new ErrorResponseException((Error) responsePacket); @@ -254,7 +259,6 @@ public SyncAnswer syncAsk(Session session, Object packet, @Nullable Class } finally { SignalBridge.removeSignalAttachment(clientSignalAttachment); } - } /** @@ -384,7 +388,7 @@ public void atReceiver(PacketReceiverTask packetReceiverTask) { } } - protected void exceptionHandler(Exception e, PacketReceiverTask packetReceiverTask){ + protected void exceptionHandler(Exception e, PacketReceiverTask packetReceiverTask) { var session = packetReceiverTask.getSession(); var packet = packetReceiverTask.getPacket(); var attachment = packetReceiverTask.getAttachment(); @@ -392,7 +396,7 @@ protected void exceptionHandler(Exception e, PacketReceiverTask packetReceiverTa logger.error("at{} e[uid:{}][sid:{}] invoke exception", StringUtils.capitalize(packet.getClass().getSimpleName()), session.getUid(), session.getSid(), e); } - protected void throwableHandler(Throwable t, PacketReceiverTask packetReceiverTask){ + protected void throwableHandler(Throwable t, PacketReceiverTask packetReceiverTask) { var session = packetReceiverTask.getSession(); var packet = packetReceiverTask.getPacket(); logger.error("at{} e[uid:{}][sid:{}] invoke error", StringUtils.capitalize(packet.getClass().getSimpleName()), session.getUid(), session.getSid(), t);