Skip to content

Commit

Permalink
feat: sync ask with timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Oct 13, 2024
1 parent 37a30d9 commit 92fc58f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions net/src/main/java/com/zfoo/net/router/IRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public interface IRouter {
*/
<T> SyncAnswer<T> syncAsk(Session session, Object packet, @Nullable Class<T> answerClass, @Nullable Object argument) throws Exception;

<T> SyncAnswer<T> syncAsk(Session session, Object packet, @Nullable Class<T> answerClass, @Nullable Object argument, long timeoutMillis) throws Exception;

<T> AsyncAnswer<T> asyncAsk(Session session, Object packet, @Nullable Class<T> answerClass, @Nullable Object argument);

<T> AsyncAnswer<T> asyncAsk(Session session, Object packet, @Nullable Class<T> answerClass, @Nullable Object argument, long timeoutMillis);
Expand Down
12 changes: 8 additions & 4 deletions net/src/main/java/com/zfoo/net/router/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ public void send(Session session, Object packet) {

@Override
public <T> SyncAnswer<T> syncAsk(Session session, Object packet, @Nullable Class<T> answerClass, @Nullable Object argument) throws Exception {
return syncAsk(session, packet, answerClass, argument, DEFAULT_TIMEOUT);
}

@Override
public <T> SyncAnswer<T> syncAsk(Session session, Object packet, Class<T> answerClass, Object argument, long timeoutMillis) throws Exception {
var clientSignalAttachment = new SignalAttachment();
if (argument == null) {
clientSignalAttachment.setClient(SignalAttachment.SIGNAL_NATIVE_NO_ARGUMENT_CLIENT);
Expand All @@ -237,7 +242,7 @@ public <T> SyncAnswer<T> 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);
Expand All @@ -254,7 +259,6 @@ public <T> SyncAnswer<T> syncAsk(Session session, Object packet, @Nullable Class
} finally {
SignalBridge.removeSignalAttachment(clientSignalAttachment);
}

}

/**
Expand Down Expand Up @@ -384,15 +388,15 @@ 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();
EventBus.post(ServerExceptionEvent.valueOf(session, packet, attachment, e));
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);
Expand Down

0 comments on commit 92fc58f

Please sign in to comment.