|
| 1 | +package io.github.kimmking.gateway.inbound; |
| 2 | + |
| 3 | +import io.netty.buffer.Unpooled; |
| 4 | +import io.netty.channel.ChannelFutureListener; |
| 5 | +import io.netty.channel.ChannelHandlerContext; |
| 6 | +import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| 7 | +import io.netty.handler.codec.http.FullHttpRequest; |
| 8 | +import io.netty.handler.codec.http.FullHttpResponse; |
| 9 | +import io.netty.handler.codec.http.HttpUtil; |
| 10 | +import io.netty.util.internal.PlatformDependent; |
| 11 | +import lombok.SneakyThrows; |
| 12 | + |
| 13 | +import java.lang.reflect.Field; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.atomic.AtomicLong; |
| 17 | + |
| 18 | +import static io.netty.handler.codec.http.HttpResponseStatus.OK; |
| 19 | +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; |
| 20 | + |
| 21 | +/** |
| 22 | + * Description for this class. |
| 23 | + * |
| 24 | + * @Author : kimmking(kimmking@apache.org) |
| 25 | + * @create 2024/5/31 下午7:13 |
| 26 | + */ |
| 27 | +public class NettyInfoHandler { |
| 28 | + |
| 29 | + public final static NettyInfoHandler INSTANCE = new NettyInfoHandler(); |
| 30 | + |
| 31 | + public void handle(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx) { |
| 32 | + System.out.println("NettyInfoHandler.handle..."); |
| 33 | + Map<String, String> infos = new HashMap<>(); |
| 34 | + infos.put("netty.usedDirectMemory", ""+getNettyUsedDirectMemory()); |
| 35 | + infos.put("netty.directMemoryLimit", ""+getNettyDirectMemoryLimit()); |
| 36 | + StringBuilder sb = new StringBuilder(); |
| 37 | + sb.append("{"); |
| 38 | + infos.forEach((k, v) -> { |
| 39 | + sb.append("\"").append(k).append("\"") |
| 40 | + .append(":") |
| 41 | + .append("\"").append(v).append("\"").append(","); |
| 42 | + }); |
| 43 | + if(sb.length()>1) { |
| 44 | + sb.deleteCharAt(sb.length()-1); |
| 45 | + } |
| 46 | + sb.append("}"); |
| 47 | + |
| 48 | + byte[] body = ("{\"code\":200,\"msg\":\"success\",\"data\":" + sb +"}").getBytes(); |
| 49 | + FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(body)); |
| 50 | + |
| 51 | + response.headers().set("Content-Type", "application/json"); |
| 52 | + response.headers().setInt("Content-Length", body.length); |
| 53 | + response.headers().set("kk.gw.hanlder", "netty.info"); |
| 54 | + |
| 55 | + if (fullRequest != null) { |
| 56 | + if (!HttpUtil.isKeepAlive(fullRequest)) { |
| 57 | + ctx.write(response).addListener(ChannelFutureListener.CLOSE); |
| 58 | + } else { |
| 59 | + //response.headers().set(CONNECTION, KEEP_ALIVE); |
| 60 | + ctx.write(response); |
| 61 | + } |
| 62 | + } |
| 63 | + ctx.flush(); |
| 64 | + //ctx.close(); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + @SneakyThrows |
| 69 | + private static long getNettyUsedDirectMemory() { |
| 70 | + Field field = PlatformDependent.class.getDeclaredField("DIRECT_MEMORY_COUNTER"); |
| 71 | + field.setAccessible(true); |
| 72 | + AtomicLong o = (AtomicLong)field.get(null); |
| 73 | + return o.get(); |
| 74 | + } |
| 75 | + |
| 76 | + @SneakyThrows |
| 77 | + private static long getNettyDirectMemoryLimit() { |
| 78 | + Field field = PlatformDependent.class.getDeclaredField("DIRECT_MEMORY_LIMIT"); |
| 79 | + field.setAccessible(true); |
| 80 | + return (Long)field.get(null); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments