perf[log]: simple log

This commit is contained in:
godotg committed 2022-10-31 22:02:55 +08:00
1 parent 5cfe1c9ddb
commit 913b51eef8
12 files changed
+53 -46

No files matched your search

@@ -15,7 +15,6 @@ package com.zfoo.net.handler;
import com.zfoo.net.NetContext; import com.zfoo.net.NetContext;
import com.zfoo.net.packet.model.DecodedPacketInfo; 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.session.model.Session;
import com.zfoo.net.util.SessionUtils; import com.zfoo.net.util.SessionUtils;
import com.zfoo.protocol.util.StringUtils; import com.zfoo.protocol.util.StringUtils;
@@ -46,15 +45,6 @@ public class BaseRouteHandler extends ChannelInboundHandlerAdapter {
channel.close(); channel.close();
throw new RuntimeException(StringUtils.format("无法设置[channel:{}]的session", channel)); 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; return session;
} }
@@ -71,7 +61,7 @@ public class BaseRouteHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
try { try {
logger.error("[session{}]未知异常", SessionUtils.sessionInfo(ctx), cause); logger.error("session exception caught {}", SessionUtils.sessionSimpleInfo(ctx), cause);
} finally { } finally {
ctx.close(); ctx.close();
} }
@@ -35,7 +35,7 @@ public class ClientRouteHandler extends BaseRouteHandler {
@Override @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx); super.channelActive(ctx);
logger.info("client channel [{}] is active", SessionUtils.sessionInfo(ctx)); logger.info("client channel is active {}", SessionUtils.sessionInfo(ctx));
} }
@Override @Override
@@ -57,12 +57,12 @@ public class ClientRouteHandler extends BaseRouteHandler {
NetContext.getConfigManager().getRegistry().checkConsumer(); NetContext.getConfigManager().getRegistry().checkConsumer();
} }
logger.warn("[channel:{}] is inactive", SessionUtils.sessionInfo(ctx)); logger.warn("client channel is inactive {}", SessionUtils.sessionSimpleInfo(ctx));
} }
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 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)) { if (packetFilter != null && packetFilter.apply(session, packet)) {
throw new IllegalArgumentException(StringUtils.format("[session:{}]发送了一个非法包[{}]" throw new IllegalArgumentException(StringUtils.format(" session:{}发送了一个非法包[{}]"
, SessionUtils.sessionInfo(ctx), JsonUtils.object2String(packet))); , SessionUtils.sessionSimpleInfo(ctx), JsonUtils.object2String(packet)));
} }
var signalAttachment = (SignalAttachment) decodedPacketInfo.getAttachment(); var signalAttachment = (SignalAttachment) decodedPacketInfo.getAttachment();
@@ -36,7 +36,7 @@ public class ServerRouteHandler extends BaseRouteHandler {
super.channelActive(ctx); super.channelActive(ctx);
var session = initChannel(ctx.channel()); var session = initChannel(ctx.channel());
NetContext.getSessionManager().addServerSession(session); NetContext.getSessionManager().addServerSession(session);
logger.info("server channel [{}] is active", SessionUtils.sessionInfo(ctx)); logger.info("server channel is active {}", SessionUtils.sessionInfo(ctx));
} }
@Override @Override
@@ -49,6 +49,6 @@ public class ServerRouteHandler extends BaseRouteHandler {
} }
NetContext.getSessionManager().removeServerSession(session); NetContext.getSessionManager().removeServerSession(session);
EventBus.asyncSubmit(ServerSessionInactiveEvent.valueOf(session)); EventBus.asyncSubmit(ServerSessionInactiveEvent.valueOf(session));
logger.warn("[channel:{}] is inactive", SessionUtils.sessionInfo(ctx)); logger.warn("server channel is inactive {}", SessionUtils.sessionSimpleInfo(ctx));
} }
} }
@@ -48,7 +48,7 @@ public class JProtobufTcpCodecHandler extends ByteToMessageCodec<EncodedPacketIn
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws IOException {
// 不够读一个int // 不够读一个int
if (in.readableBytes() <= PacketService.PACKET_HEAD_LENGTH) { if (in.readableBytes() <= PacketService.PACKET_HEAD_LENGTH) {
return; return;
@@ -58,8 +58,7 @@ public class JProtobufTcpCodecHandler extends ByteToMessageCodec<EncodedPacketIn
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽 // 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
if (length < 0 || length > IOUtils.BYTES_PER_MB) { if (length < 0 || length > IOUtils.BYTES_PER_MB) {
throw new IllegalArgumentException(StringUtils.format("[session:{}]的包头长度[length:{}]非法" throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
, SessionUtils.sessionInfo(ctx), length));
} }
// ByteBuf里的数据太小 // ByteBuf里的数据太小
@@ -74,22 +73,26 @@ public class JProtobufTcpCodecHandler extends ByteToMessageCodec<EncodedPacketIn
DecodedPacketInfo packetInfo = read(tmpByteBuf); DecodedPacketInfo packetInfo = read(tmpByteBuf);
out.add(packetInfo); out.add(packetInfo);
} catch (Exception e) { } catch (Exception e) {
logger.error("[session:{}]解码exception异常", SessionUtils.sessionInfo(ctx), e); logger.error("decode exception {}", SessionUtils.sessionSimpleInfo(ctx), e);
throw e;
} catch (Throwable t) { } catch (Throwable t) {
logger.error("[session:{}]解码throwable错误", SessionUtils.sessionInfo(ctx), t); logger.error("decode throwable {}", SessionUtils.sessionSimpleInfo(ctx), t);
throw t;
} finally { } finally {
ReferenceCountUtil.release(tmpByteBuf); ReferenceCountUtil.release(tmpByteBuf);
} }
} }
@Override @Override
protected void encode(ChannelHandlerContext ctx, EncodedPacketInfo packetInfo, ByteBuf out) { protected void encode(ChannelHandlerContext ctx, EncodedPacketInfo packetInfo, ByteBuf out) throws IOException {
try { try {
write(out, packetInfo.getPacket(), packetInfo.getAttachment()); write(out, packetInfo.getPacket(), packetInfo.getAttachment());
} catch (Exception e) { } 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) { } 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) { if (length < 0 || length > IOUtils.BYTES_PER_MB) {
throw new IllegalArgumentException(StringUtils.format("[session:{}]的包头长度[length:{}]非法" throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
, SessionUtils.sessionInfo(ctx), length));
} }
// ByteBuf里的数据太小 // ByteBuf里的数据太小
@@ -68,10 +67,10 @@ public class TcpCodecHandler extends ByteToMessageCodec<EncodedPacketInfo> {
DecodedPacketInfo packetInfo = NetContext.getPacketService().read(tmpByteBuf); DecodedPacketInfo packetInfo = NetContext.getPacketService().read(tmpByteBuf);
out.add(packetInfo); out.add(packetInfo);
} catch (Exception e) { } catch (Exception e) {
logger.error("[session:{}]解码exception异常", SessionUtils.sessionInfo(ctx), e); logger.error("decode exception {}", SessionUtils.sessionSimpleInfo(ctx), e);
throw e; throw e;
} catch (Throwable t) { } catch (Throwable t) {
logger.error("[session:{}]解码throwable错误", SessionUtils.sessionInfo(ctx), t); logger.error("decode throwable {}", SessionUtils.sessionSimpleInfo(ctx), t);
throw t; throw t;
} finally { } finally {
ReferenceCountUtil.release(tmpByteBuf); ReferenceCountUtil.release(tmpByteBuf);
@@ -83,10 +82,10 @@ public class TcpCodecHandler extends ByteToMessageCodec<EncodedPacketInfo> {
try { try {
NetContext.getPacketService().write(out, packetInfo.getPacket(), packetInfo.getAttachment()); NetContext.getPacketService().write(out, packetInfo.getPacket(), packetInfo.getAttachment());
} catch (Exception e) { } 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; throw e;
} catch (Throwable t) { } 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; 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.model.EncodedPacketInfo;
import com.zfoo.net.packet.service.PacketService; import com.zfoo.net.packet.service.PacketService;
import com.zfoo.net.router.attachment.UdpAttachment; import com.zfoo.net.router.attachment.UdpAttachment;
import com.zfoo.net.util.SessionUtils;
import com.zfoo.protocol.util.IOUtils; import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.JsonUtils; import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.StringUtils; 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) { if (length < 0 || length > IOUtils.BYTES_PER_MB) {
throw new IllegalArgumentException(StringUtils.format("[session:{}]的包头长度[length:{}]非法" throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
, SessionUtils.sessionInfo(channelHandlerContext), length));
} }
// ByteBuf里的数据太小 // ByteBuf里的数据太小
@@ -17,7 +17,6 @@ import com.zfoo.net.NetContext;
import com.zfoo.net.packet.model.DecodedPacketInfo; import com.zfoo.net.packet.model.DecodedPacketInfo;
import com.zfoo.net.packet.model.EncodedPacketInfo; import com.zfoo.net.packet.model.EncodedPacketInfo;
import com.zfoo.net.packet.service.PacketService; import com.zfoo.net.packet.service.PacketService;
import com.zfoo.net.util.SessionUtils;
import com.zfoo.protocol.util.IOUtils; import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.JsonUtils; import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.StringUtils; import com.zfoo.protocol.util.StringUtils;
@@ -57,8 +56,7 @@ public class WebSocketCodecHandler extends MessageToMessageCodec<WebSocketFrame,
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽 // 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
if (length < 0 || length > IOUtils.BYTES_PER_MB) { if (length < 0 || length > IOUtils.BYTES_PER_MB) {
throw new IllegalArgumentException(StringUtils.format("[session:{}]的包头长度[length:{}]非法" throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
, SessionUtils.sessionInfo(channelHandlerContext), length));
} }
// ByteBuf里的数据太小 // ByteBuf里的数据太小
@@ -40,7 +40,7 @@ public class ClientIdleHandler extends ChannelDuplexHandler {
if (evt instanceof IdleStateEvent) { if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt; IdleStateEvent event = (IdleStateEvent) evt;
if (event.state() == IdleState.ALL_IDLE) { 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); ctx.channel().writeAndFlush(heartbeatPacket);
} }
} }
@@ -36,7 +36,7 @@ public class ServerIdleHandler extends ChannelDuplexHandler {
if (evt instanceof IdleStateEvent) { if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt; IdleStateEvent event = (IdleStateEvent) evt;
if (event.state() == IdleState.ALL_IDLE) { 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(); ctx.close();
} }
@@ -20,9 +20,6 @@ package com.zfoo.net.session.model;
public enum AttributeType { public enum AttributeType {
CHANNEL_REMOTE_ADDRESS,
/** /**
* 一般是客户端session * 一般是客户端session
*/ */
@@ -40,6 +37,4 @@ public enum AttributeType {
*/ */
GATEWAY_HOST_AND_PORT, 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_INFO_TEMPLATE = "[ip:{}][sid:{}][uid:{}]";
private static final String CHANNEL_SIMPLE_INFO_TEMPLATE = "[sid:{}][uid:{}]";
public static boolean isActive(Session session) { public static boolean isActive(Session session) {
return session != null && session.getChannel().isActive(); return session != null && session.getChannel().isActive();
@@ -55,8 +57,30 @@ public abstract class SessionUtils {
if (session == null) { if (session == null) {
return CHANNEL_INFO_TEMPLATE; 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)); 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));
}
} }