mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-19 05:26:41 +00:00
perf[log]: simple log
This commit is contained in:
@@ -15,7 +15,6 @@ package com.zfoo.net.handler;
|
||||
|
||||
import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.packet.model.DecodedPacketInfo;
|
||||
import com.zfoo.net.session.model.AttributeType;
|
||||
import com.zfoo.net.session.model.Session;
|
||||
import com.zfoo.net.util.SessionUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
@@ -46,15 +45,6 @@ public class BaseRouteHandler extends ChannelInboundHandlerAdapter {
|
||||
channel.close();
|
||||
throw new RuntimeException(StringUtils.format("无法设置[channel:{}]的session", channel));
|
||||
}
|
||||
|
||||
try {
|
||||
session.putAttribute(AttributeType.CHANNEL_REMOTE_ADDRESS, StringUtils.substringAfterFirst(channel.remoteAddress().toString(), StringUtils.SLASH));
|
||||
} catch (Throwable t) {
|
||||
// do nothing
|
||||
// to avoid: io.netty.channel.unix.Errors$NativeIoException: readAddress(..) failed: Connection reset by peer
|
||||
// 有些情况当建立连接过后迅速关闭,这个时候取remoteAddress会有异常
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
@@ -71,7 +61,7 @@ public class BaseRouteHandler extends ChannelInboundHandlerAdapter {
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
try {
|
||||
logger.error("[session{}]未知异常", SessionUtils.sessionInfo(ctx), cause);
|
||||
logger.error("session exception caught {}", SessionUtils.sessionSimpleInfo(ctx), cause);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public class ClientRouteHandler extends BaseRouteHandler {
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
logger.info("client channel [{}] is active", SessionUtils.sessionInfo(ctx));
|
||||
logger.info("client channel is active {}", SessionUtils.sessionInfo(ctx));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,12 +57,12 @@ public class ClientRouteHandler extends BaseRouteHandler {
|
||||
NetContext.getConfigManager().getRegistry().checkConsumer();
|
||||
}
|
||||
|
||||
logger.warn("[channel:{}] is inactive", SessionUtils.sessionInfo(ctx));
|
||||
logger.warn("client channel is inactive {}", SessionUtils.sessionSimpleInfo(ctx));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
logger.error("[session{}]未知异常", SessionUtils.sessionInfo(ctx), cause);
|
||||
logger.error("client session exception caught {}", SessionUtils.sessionSimpleInfo(ctx), cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -74,8 +74,8 @@ public class GatewayRouteHandler extends ServerRouteHandler {
|
||||
|
||||
// 过滤非法包
|
||||
if (packetFilter != null && packetFilter.apply(session, packet)) {
|
||||
throw new IllegalArgumentException(StringUtils.format("[session:{}]发送了一个非法包[{}]"
|
||||
, SessionUtils.sessionInfo(ctx), JsonUtils.object2String(packet)));
|
||||
throw new IllegalArgumentException(StringUtils.format(" session:{}发送了一个非法包[{}]"
|
||||
, SessionUtils.sessionSimpleInfo(ctx), JsonUtils.object2String(packet)));
|
||||
}
|
||||
|
||||
var signalAttachment = (SignalAttachment) decodedPacketInfo.getAttachment();
|
||||
|
||||
@@ -36,7 +36,7 @@ public class ServerRouteHandler extends BaseRouteHandler {
|
||||
super.channelActive(ctx);
|
||||
var session = initChannel(ctx.channel());
|
||||
NetContext.getSessionManager().addServerSession(session);
|
||||
logger.info("server channel [{}] is active", SessionUtils.sessionInfo(ctx));
|
||||
logger.info("server channel is active {}", SessionUtils.sessionInfo(ctx));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,6 +49,6 @@ public class ServerRouteHandler extends BaseRouteHandler {
|
||||
}
|
||||
NetContext.getSessionManager().removeServerSession(session);
|
||||
EventBus.asyncSubmit(ServerSessionInactiveEvent.valueOf(session));
|
||||
logger.warn("[channel:{}] is inactive", SessionUtils.sessionInfo(ctx));
|
||||
logger.warn("server channel is inactive {}", SessionUtils.sessionSimpleInfo(ctx));
|
||||
}
|
||||
}
|
||||
|
||||
+11
-8
@@ -48,7 +48,7 @@ public class JProtobufTcpCodecHandler extends ByteToMessageCodec<EncodedPacketIn
|
||||
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws IOException {
|
||||
// 不够读一个int
|
||||
if (in.readableBytes() <= PacketService.PACKET_HEAD_LENGTH) {
|
||||
return;
|
||||
@@ -58,8 +58,7 @@ public class JProtobufTcpCodecHandler extends ByteToMessageCodec<EncodedPacketIn
|
||||
|
||||
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
|
||||
if (length < 0 || length > IOUtils.BYTES_PER_MB) {
|
||||
throw new IllegalArgumentException(StringUtils.format("[session:{}]的包头长度[length:{}]非法"
|
||||
, SessionUtils.sessionInfo(ctx), length));
|
||||
throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
|
||||
}
|
||||
|
||||
// ByteBuf里的数据太小
|
||||
@@ -74,22 +73,26 @@ public class JProtobufTcpCodecHandler extends ByteToMessageCodec<EncodedPacketIn
|
||||
DecodedPacketInfo packetInfo = read(tmpByteBuf);
|
||||
out.add(packetInfo);
|
||||
} catch (Exception e) {
|
||||
logger.error("[session:{}]解码exception异常", SessionUtils.sessionInfo(ctx), e);
|
||||
logger.error("decode exception {}", SessionUtils.sessionSimpleInfo(ctx), e);
|
||||
throw e;
|
||||
} catch (Throwable t) {
|
||||
logger.error("[session:{}]解码throwable错误", SessionUtils.sessionInfo(ctx), t);
|
||||
logger.error("decode throwable {}", SessionUtils.sessionSimpleInfo(ctx), t);
|
||||
throw t;
|
||||
} finally {
|
||||
ReferenceCountUtil.release(tmpByteBuf);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, EncodedPacketInfo packetInfo, ByteBuf out) {
|
||||
protected void encode(ChannelHandlerContext ctx, EncodedPacketInfo packetInfo, ByteBuf out) throws IOException {
|
||||
try {
|
||||
write(out, packetInfo.getPacket(), packetInfo.getAttachment());
|
||||
} catch (Exception e) {
|
||||
logger.error("[session:{}][{}]编码exception异常", SessionUtils.sessionInfo(ctx), packetInfo.getPacket().getClass().getSimpleName(), e);
|
||||
logger.error("[{}] encode exception {}", SessionUtils.sessionSimpleInfo(ctx), packetInfo.getPacket().getClass().getSimpleName(), e);
|
||||
throw e;
|
||||
} catch (Throwable t) {
|
||||
logger.error("[session:{}][{}]编码throwable错误", SessionUtils.sessionInfo(ctx), packetInfo.getPacket().getClass().getSimpleName(), t);
|
||||
logger.error("[{}] encode throwable {}", SessionUtils.sessionSimpleInfo(ctx), packetInfo.getPacket().getClass().getSimpleName(), t);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,8 +51,7 @@ public class TcpCodecHandler extends ByteToMessageCodec<EncodedPacketInfo> {
|
||||
|
||||
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
|
||||
if (length < 0 || length > IOUtils.BYTES_PER_MB) {
|
||||
throw new IllegalArgumentException(StringUtils.format("[session:{}]的包头长度[length:{}]非法"
|
||||
, SessionUtils.sessionInfo(ctx), length));
|
||||
throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
|
||||
}
|
||||
|
||||
// ByteBuf里的数据太小
|
||||
@@ -68,10 +67,10 @@ public class TcpCodecHandler extends ByteToMessageCodec<EncodedPacketInfo> {
|
||||
DecodedPacketInfo packetInfo = NetContext.getPacketService().read(tmpByteBuf);
|
||||
out.add(packetInfo);
|
||||
} catch (Exception e) {
|
||||
logger.error("[session:{}]解码exception异常", SessionUtils.sessionInfo(ctx), e);
|
||||
logger.error("decode exception {}", SessionUtils.sessionSimpleInfo(ctx), e);
|
||||
throw e;
|
||||
} catch (Throwable t) {
|
||||
logger.error("[session:{}]解码throwable错误", SessionUtils.sessionInfo(ctx), t);
|
||||
logger.error("decode throwable {}", SessionUtils.sessionSimpleInfo(ctx), t);
|
||||
throw t;
|
||||
} finally {
|
||||
ReferenceCountUtil.release(tmpByteBuf);
|
||||
@@ -83,10 +82,10 @@ public class TcpCodecHandler extends ByteToMessageCodec<EncodedPacketInfo> {
|
||||
try {
|
||||
NetContext.getPacketService().write(out, packetInfo.getPacket(), packetInfo.getAttachment());
|
||||
} catch (Exception e) {
|
||||
logger.error("[session:{}][{}]编码exception异常", SessionUtils.sessionInfo(ctx), packetInfo.getPacket().getClass().getSimpleName(), e);
|
||||
logger.error("[{}] encode exception {}", SessionUtils.sessionSimpleInfo(ctx), packetInfo.getPacket().getClass().getSimpleName(), e);
|
||||
throw e;
|
||||
} catch (Throwable t) {
|
||||
logger.error("[session:{}][{}]编码throwable错误", SessionUtils.sessionInfo(ctx), packetInfo.getPacket().getClass().getSimpleName(), t);
|
||||
logger.error("[{}] encode throwable {}", SessionUtils.sessionSimpleInfo(ctx), packetInfo.getPacket().getClass().getSimpleName(), t);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ import com.zfoo.net.packet.model.DecodedPacketInfo;
|
||||
import com.zfoo.net.packet.model.EncodedPacketInfo;
|
||||
import com.zfoo.net.packet.service.PacketService;
|
||||
import com.zfoo.net.router.attachment.UdpAttachment;
|
||||
import com.zfoo.net.util.SessionUtils;
|
||||
import com.zfoo.protocol.util.IOUtils;
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
@@ -54,8 +53,7 @@ public class UdpCodecHandler extends MessageToMessageCodec<DatagramPacket, Encod
|
||||
|
||||
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
|
||||
if (length < 0 || length > IOUtils.BYTES_PER_MB) {
|
||||
throw new IllegalArgumentException(StringUtils.format("[session:{}]的包头长度[length:{}]非法"
|
||||
, SessionUtils.sessionInfo(channelHandlerContext), length));
|
||||
throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
|
||||
}
|
||||
|
||||
// ByteBuf里的数据太小
|
||||
|
||||
@@ -17,7 +17,6 @@ import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.packet.model.DecodedPacketInfo;
|
||||
import com.zfoo.net.packet.model.EncodedPacketInfo;
|
||||
import com.zfoo.net.packet.service.PacketService;
|
||||
import com.zfoo.net.util.SessionUtils;
|
||||
import com.zfoo.protocol.util.IOUtils;
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
@@ -57,8 +56,7 @@ public class WebSocketCodecHandler extends MessageToMessageCodec<WebSocketFrame,
|
||||
|
||||
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
|
||||
if (length < 0 || length > IOUtils.BYTES_PER_MB) {
|
||||
throw new IllegalArgumentException(StringUtils.format("[session:{}]的包头长度[length:{}]非法"
|
||||
, SessionUtils.sessionInfo(channelHandlerContext), length));
|
||||
throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
|
||||
}
|
||||
|
||||
// ByteBuf里的数据太小
|
||||
|
||||
@@ -40,7 +40,7 @@ public class ClientIdleHandler extends ChannelDuplexHandler {
|
||||
if (evt instanceof IdleStateEvent) {
|
||||
IdleStateEvent event = (IdleStateEvent) evt;
|
||||
if (event.state() == IdleState.ALL_IDLE) {
|
||||
logger.warn("client sends heartbeat packet to {}", SessionUtils.sessionInfo(ctx));
|
||||
logger.warn("client sends heartbeat packet to {}", SessionUtils.sessionSimpleInfo(ctx));
|
||||
ctx.channel().writeAndFlush(heartbeatPacket);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ public class ServerIdleHandler extends ChannelDuplexHandler {
|
||||
if (evt instanceof IdleStateEvent) {
|
||||
IdleStateEvent event = (IdleStateEvent) evt;
|
||||
if (event.state() == IdleState.ALL_IDLE) {
|
||||
logger.warn("[channel:{}] is time out for close", SessionUtils.sessionInfo(ctx));
|
||||
logger.warn("channel is time out for close {}", SessionUtils.sessionSimpleInfo(ctx));
|
||||
}
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@@ -20,9 +20,6 @@ package com.zfoo.net.session.model;
|
||||
public enum AttributeType {
|
||||
|
||||
|
||||
CHANNEL_REMOTE_ADDRESS,
|
||||
|
||||
|
||||
/**
|
||||
* 一般是客户端session
|
||||
*/
|
||||
@@ -40,6 +37,4 @@ public enum AttributeType {
|
||||
*/
|
||||
GATEWAY_HOST_AND_PORT,
|
||||
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ public abstract class SessionUtils {
|
||||
|
||||
private static final String CHANNEL_INFO_TEMPLATE = "[ip:{}][sid:{}][uid:{}]";
|
||||
|
||||
private static final String CHANNEL_SIMPLE_INFO_TEMPLATE = "[sid:{}][uid:{}]";
|
||||
|
||||
|
||||
public static boolean isActive(Session session) {
|
||||
return session != null && session.getChannel().isActive();
|
||||
@@ -55,8 +57,30 @@ public abstract class SessionUtils {
|
||||
if (session == null) {
|
||||
return CHANNEL_INFO_TEMPLATE;
|
||||
}
|
||||
var remoteAddress = session.getAttribute(AttributeType.CHANNEL_REMOTE_ADDRESS);
|
||||
var remoteAddress = StringUtils.EMPTY;
|
||||
try {
|
||||
remoteAddress = StringUtils.substringAfterFirst(session.getChannel().remoteAddress().toString(), StringUtils.SLASH);
|
||||
} catch (Throwable t) {
|
||||
// do nothing
|
||||
// to avoid: io.netty.channel.unix.Errors$NativeIoException: readAddress(..) failed: Connection reset by peer
|
||||
// 有些情况当建立连接过后迅速关闭,这个时候取remoteAddress会有异常
|
||||
}
|
||||
return StringUtils.format(CHANNEL_INFO_TEMPLATE, remoteAddress, session.getSid(), session.getAttribute(AttributeType.UID));
|
||||
}
|
||||
|
||||
public static String sessionSimpleInfo(ChannelHandlerContext ctx) {
|
||||
var session = SessionUtils.getSession(ctx);
|
||||
if (session == null) {
|
||||
return StringUtils.format(CHANNEL_SIMPLE_INFO_TEMPLATE, ctx.channel());
|
||||
}
|
||||
return sessionSimpleInfo(session);
|
||||
}
|
||||
|
||||
public static String sessionSimpleInfo(Session session) {
|
||||
if (session == null) {
|
||||
return CHANNEL_SIMPLE_INFO_TEMPLATE;
|
||||
}
|
||||
return StringUtils.format(CHANNEL_SIMPLE_INFO_TEMPLATE, session.getSid(), session.getAttribute(AttributeType.UID));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user