perf[net]: 优化异常处理为抛出事件

This commit is contained in:
meiwei committed 2022-08-01 19:53:22 +08:00
1 parent b2b60f73b1
commit 1e3f3654d4
5 files changed
+76 -150

No files matched your search

@@ -0,0 +1,72 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.core.tcp.model;
import com.zfoo.event.model.event.IEvent;
import com.zfoo.net.router.attachment.IAttachment;
import com.zfoo.net.session.model.Session;
import com.zfoo.protocol.IPacket;
/**
* @author meiw
* @version 3.0
*/
public class ServerExceptionEvent implements IEvent {
private Session session;
private IPacket packet;
private IAttachment attachment;
private Exception exception;
public static ServerExceptionEvent valueOf(Session session, IPacket packet, IAttachment attachment, Exception exception) {
var event = new ServerExceptionEvent();
event.session = session;
event.packet = packet;
event.attachment = attachment;
event.exception = exception;
return event;
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public IPacket getPacket() {
return packet;
}
public void setPacket(IPacket packet) {
this.packet = packet;
}
public IAttachment getAttachment() {
return attachment;
}
public void setAttachment(IAttachment attachment) {
this.attachment = attachment;
}
public Exception getException() {
return exception;
}
public void setException(Exception exception) {
this.exception = exception;
}
}
@@ -1,86 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.packet.common;
import com.baidu.bjf.remoting.protobuf.annotation.Ignore;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.zfoo.protocol.IPacket;
/**
* @author meiw
* @version 3.0
*/
@ProtobufClass
public class ErrorCode implements IPacket {
@Ignore
public static final transient short PROTOCOL_ID = 105;
@Protobuf(order = 1)
private int protocolId;
@Protobuf(order = 2)
private int errorCode;
@Protobuf(order = 3)
private String errorMessage;
@Override
public short protocolId() {
return PROTOCOL_ID;
}
public static ErrorCode valueOf(int protocolId, int errorCode, String errorMessage) {
ErrorCode response = new ErrorCode();
response.protocolId = protocolId;
response.errorCode = errorCode;
response.errorMessage = errorMessage;
return response;
}
public static ErrorCode valueOf(IPacket packet, int errorCode, String errorMessage) {
ErrorCode response = new ErrorCode();
response.protocolId = packet.protocolId();
response.errorCode = errorCode;
response.errorMessage = errorMessage;
return response;
}
public static ErrorCode valueOf(IPacket packet, int errorCode) {
return valueOf(packet, errorCode, "");
}
public int getProtocolId() {
return protocolId;
}
public void setProtocolId(int protocolId) {
this.protocolId = protocolId;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
@@ -18,8 +18,8 @@ import com.zfoo.net.NetContext;
import com.zfoo.net.core.gateway.model.AuthUidToGatewayCheck;
import com.zfoo.net.core.gateway.model.AuthUidToGatewayConfirm;
import com.zfoo.net.core.gateway.model.AuthUidToGatewayEvent;
import com.zfoo.net.core.tcp.model.ServerExceptionEvent;
import com.zfoo.net.packet.common.Error;
import com.zfoo.net.packet.common.ErrorCode;
import com.zfoo.net.packet.common.Heartbeat;
import com.zfoo.net.packet.model.EncodedPacketInfo;
import com.zfoo.net.router.answer.AsyncAnswer;
@@ -27,7 +27,6 @@ import com.zfoo.net.router.answer.SyncAnswer;
import com.zfoo.net.router.attachment.GatewayAttachment;
import com.zfoo.net.router.attachment.IAttachment;
import com.zfoo.net.router.attachment.SignalAttachment;
import com.zfoo.net.router.exception.ErrorRequestException;
import com.zfoo.net.router.exception.ErrorResponseException;
import com.zfoo.net.router.exception.NetTimeOutException;
import com.zfoo.net.router.exception.UnexpectedProtocolException;
@@ -344,7 +343,7 @@ public class Router implements IRouter {
// 这个在哪个线程处理取决于:这个上层的PacketReceiverTask被丢到了哪个线程中
PacketBus.submit(session, packet, attachment);
} catch (Exception e) {
handleException(session, packet, attachment, e);
EventBus.syncSubmit(ServerExceptionEvent.valueOf(session, packet, attachment, e));
logger.error(StringUtils.format("e[uid:{}][sid:{}]未知exception异常", session.getAttribute(AttributeType.UID), session.getSid(), e.getMessage()), e);
} catch (Throwable t) {
logger.error(StringUtils.format("e[uid:{}][sid:{}]未知error错误", session.getAttribute(AttributeType.UID), session.getSid(), t.getMessage()), t);
@@ -362,17 +361,4 @@ public class Router implements IRouter {
}
}
private void handleException(Session session, IPacket packet, IAttachment attachment, Exception exception) {
if (exception instanceof ErrorRequestException) {
ErrorRequestException requestException = (ErrorRequestException) exception;
int errorCode = requestException.getErrorCode();
String msg = requestException.getErrorMsg();
var resp = ErrorCode.valueOf(packet, errorCode, msg);
NetContext.getRouter().send(session, resp, attachment);
return;
}
var resp = ErrorCode.valueOf(packet, 0);
NetContext.getRouter().send(session, resp, attachment);
}
}
@@ -1,45 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.router.exception;
/**
* @author meiw
* @version 3.0
*/
public class ErrorRequestException extends RuntimeException {
private final int errorCode;
private final String errorMsg;
public ErrorRequestException(int errorCode, String errorMsg) {
super(errorMsg);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public ErrorRequestException(int errorCode) {
this.errorCode = errorCode;
this.errorMsg = "";
}
public int getErrorCode() {
return errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
}
@@ -29,6 +29,7 @@ import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.collection.ArrayUtils;
import com.zfoo.protocol.exception.RunException;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.registration.ProtocolAnalysis;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.ReflectionUtils;
import com.zfoo.protocol.util.StringUtils;
@@ -125,9 +126,7 @@ public abstract class PacketBus {
}
try {
var protocolIdField = packetClazz.getDeclaredField(ProtocolManager.PROTOCOL_ID);
ReflectionUtils.makeAccessible(protocolIdField);
var protocolId = (short) protocolIdField.get(null);
var protocolId = ProtocolAnalysis.getProtocolIdByClass(packetClazz);
var receiverDefinition = new PacketReceiverDefinition(bean, method, packetClazz, attachmentClazz);
var enhanceReceiverDefinition = EnhanceUtils.createPacketReceiver(receiverDefinition);