Browse Source

fix (#7162)

Co-authored-by: longzhili <[email protected]>
longzl 3 years ago
parent
commit
9aa83b23f5

+ 8 - 0
frameworks/Java/isocket-nio/.gitignore

@@ -0,0 +1,8 @@
+.settings
+.project
+.classpath
+
+/logs
+/target
+.idea
+*.iml

+ 1 - 1
frameworks/Java/isocket-nio/isocket-nio.dockerfile

@@ -10,4 +10,4 @@ COPY --from=maven /isocket/target/isocket-nio-benchmark-1.0-jar-with-dependencie
 
 
 EXPOSE 8080
 EXPOSE 8080
 
 
-CMD ["java", "-server", "-XX:+UseNUMA", "-XX:+UseParallelGC", "-XX:+AggressiveOpts", "-cp", "app.jar", "cn.ibaijia.tfb.HttpBootstrap"]
+CMD ["java", "-server", "-XX:+UseNUMA", "-XX:+UseParallelGC", "-cp", "app.jar", "cn.ibaijia.tfb.HttpBootstrap"]

+ 5 - 1
frameworks/Java/isocket-nio/pom.xml

@@ -44,12 +44,16 @@
             <artifactId>slf4j-api</artifactId>
             <artifactId>slf4j-api</artifactId>
             <version>${slf4j.version}</version>
             <version>${slf4j.version}</version>
         </dependency>
         </dependency>
-
         <dependency>
         <dependency>
             <groupId>org.apache.logging.log4j</groupId>
             <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-api</artifactId>
             <artifactId>log4j-api</artifactId>
             <version>${log4j2.version}</version>
             <version>${log4j2.version}</version>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-slf4j-impl</artifactId>
+            <version>${log4j2.version}</version>
+        </dependency>
         <dependency>
         <dependency>
             <groupId>org.apache.logging.log4j</groupId>
             <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-core</artifactId>
             <artifactId>log4j-core</artifactId>

+ 0 - 12
frameworks/Java/isocket-nio/src/main/java/cn/ibaijia/tfb/Consts.java

@@ -1,12 +0,0 @@
-package cn.ibaijia.tfb;
-
-public class Consts {
-
-    public static final byte[] TEXT_TYPE = "text/plain".getBytes();
-    public static final byte[] JSON_TYPE = "application/json".getBytes();
-
-    public static final String URL_TEXT_PLAIN = "/plaintext";
-    public static final String URL_JSON = "/json";
-    public static final String URL_STATE = "/state";
-
-}

+ 0 - 33
frameworks/Java/isocket-nio/src/main/java/cn/ibaijia/tfb/DateUtil.java

@@ -1,33 +0,0 @@
-package cn.ibaijia.tfb;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Locale;
-
-public class DateUtil {
-
-    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);//Fri, 09 Jul 2021 09:10:42 UTC
-
-    private static byte[] date = ("\r\nDate:" + dateFormat.format(new Date())).getBytes();
-
-    public static byte[] getDate() {
-        return date;
-    }
-
-    public static void start() {
-        new Thread(new Runnable() {
-            @Override
-            public void run() {
-                while (true) {
-                    date = ("\r\nDate:" + dateFormat.format(new Date())).getBytes();
-                    try {
-                        Thread.sleep(1000);
-                    } catch (InterruptedException e) {
-
-                    }
-                }
-            }
-        }).start();
-    }
-
-}

+ 5 - 9
frameworks/Java/isocket-nio/src/main/java/cn/ibaijia/tfb/HttpBootstrap.java

@@ -2,9 +2,10 @@ package cn.ibaijia.tfb;
 
 
 import cn.ibaijia.isocket.Server;
 import cn.ibaijia.isocket.Server;
 import cn.ibaijia.isocket.listener.SessionProcessErrorListener;
 import cn.ibaijia.isocket.listener.SessionProcessErrorListener;
+import cn.ibaijia.isocket.protocol.SimpleHttpProtocol;
+import cn.ibaijia.isocket.protocol.http.DateUtil;
 import cn.ibaijia.isocket.session.Session;
 import cn.ibaijia.isocket.session.Session;
 import cn.ibaijia.tfb.processor.PlanTextProcessor;
 import cn.ibaijia.tfb.processor.PlanTextProcessor;
-import cn.ibaijia.tfb.protocol.SimpleHttpProtocol;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 
 
@@ -19,17 +20,12 @@ public class HttpBootstrap {
         Server server = new Server("0.0.0.0", 8080);
         Server server = new Server("0.0.0.0", 8080);
         server.addProtocol(new SimpleHttpProtocol());
         server.addProtocol(new SimpleHttpProtocol());
         server.setProcessor(new PlanTextProcessor());
         server.setProcessor(new PlanTextProcessor());
-        server.setSessionProcessErrorListener(new SessionProcessErrorListener() {
-            @Override
-            public void run(Session session, Object o, Throwable throwable) {
-                logger.error("session on process error.", throwable);
-            }
-        });
+        server.setSessionProcessErrorListener((session, o, throwable) -> logger.error("session on process error.", throwable));
         server.setUseDirectBuffer(true);
         server.setUseDirectBuffer(true);
         server.setUsePool(true);
         server.setUsePool(true);
-        server.setPoolPageSize(32 * 1024);
+        server.setPoolSize(64 * 1024);
         server.setBuffSize(1 * 1024);
         server.setBuffSize(1 * 1024);
-        server.setBacklog(16 * 1024);
+        server.setBacklog(4 * 1024);
         server.start();
         server.start();
     }
     }
 
 

+ 0 - 12
frameworks/Java/isocket-nio/src/main/java/cn/ibaijia/tfb/http/Header.java

@@ -1,12 +0,0 @@
-package cn.ibaijia.tfb.http;
-
-public class Header {
-
-    public String name;
-    public String value;
-
-    public Header(String name, String value) {
-        this.name = name;
-        this.value = value;
-    }
-}

+ 0 - 17
frameworks/Java/isocket-nio/src/main/java/cn/ibaijia/tfb/http/HttpEntity.java

@@ -1,17 +0,0 @@
-package cn.ibaijia.tfb.http;
-
-public abstract class HttpEntity {
-
-    public String charset = "UTF-8";
-
-    public abstract byte[] getHeader(byte[] name);
-
-    public abstract byte[] getHeader(String name);
-
-    public abstract void setHeader(byte[] name, byte[] value);
-
-    public abstract void setContentType(String contentType);
-
-    public abstract void setContentType(byte[] contentType);
-
-}

+ 0 - 99
frameworks/Java/isocket-nio/src/main/java/cn/ibaijia/tfb/http/HttpRequestEntity.java

@@ -1,99 +0,0 @@
-package cn.ibaijia.tfb.http;
-
-import cn.ibaijia.tfb.Consts;
-
-import java.nio.ByteBuffer;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @author longzl
- */
-public class HttpRequestEntity extends HttpEntity {
-
-
-    public ByteBuffer bodyBuffer = null;
-    public boolean chunked = false;
-    public int contentLength = -1;
-    public int crNum = 0;
-    public int lfNum = 0;
-    public byte[] tmp;
-
-
-    public String method;
-    public String url;
-    public String protocol;
-
-    /**
-     * 请求体
-     */
-    public String body;
-    private int count = 0;
-    /**
-     * 第一次 请求header时解析 第一行不要
-     */
-    private Map<byte[], byte[]> headers = new HashMap<>(8);
-    private byte[] contentType = Consts.TEXT_TYPE;
-
-    @Override
-    public byte[] getHeader(byte[] name) {
-        for (Map.Entry<byte[], byte[]> entry : headers.entrySet()) {
-            if (Arrays.equals(entry.getKey(), name)) {
-                return entry.getValue();
-            }
-        }
-        return null;
-    }
-
-    @Override
-    public byte[] getHeader(String name) {
-        return getHeader(name.getBytes());
-    }
-
-    @Override
-    public void setHeader(byte[] name, byte[] value) {
-        count ++;
-        this.headers.put(name, value);
-    }
-
-    public void printAllHeaders() {
-        for (Map.Entry<byte[], byte[]> entry : headers.entrySet()) {
-            System.out.println(count);
-            System.out.println(new String(entry.getKey()) + ":" + new String(entry.getValue()));
-        }
-    }
-
-    @Override
-    public void setContentType(String contentType) {
-        this.contentType = contentType.getBytes();
-    }
-
-    @Override
-    public void setContentType(byte[] contentType) {
-        this.contentType = contentType;
-    }
-
-    public void processBody() {
-        bodyBuffer.flip();
-        byte[] bytes = new byte[bodyBuffer.remaining()];
-        bodyBuffer.get(bytes);
-        body = new String(bytes);
-    }
-
-    public boolean complete() {
-        if (contentLength < 1) {
-            return true;
-        } else {
-            return body != null;
-        }
-    }
-
-    public boolean headerComplete() {
-        return this.crNum == 2 && this.lfNum == 2;
-    }
-
-    public boolean isReadHeadLine() {
-        return this.protocol == null;
-    }
-}

+ 0 - 104
frameworks/Java/isocket-nio/src/main/java/cn/ibaijia/tfb/http/HttpResponseEntity.java

@@ -1,104 +0,0 @@
-package cn.ibaijia.tfb.http;
-
-import cn.ibaijia.tfb.Consts;
-import cn.ibaijia.tfb.DateUtil;
-
-import java.nio.ByteBuffer;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Objects;
-
-/**
- * @author longzl
- */
-public class HttpResponseEntity extends HttpEntity {
-    private static final byte[] PROTOCOL = "HTTP/1.1 ".getBytes();
-    private static final byte[] STATUS_200 = "200 OK".getBytes();
-    private static final byte[] CRLF = "\r\n".getBytes();
-    private static final byte[] COLON = ":".getBytes();
-    private static final byte[] SERVER_NAME = "\r\nServer:tfb\r\n".getBytes();
-    private static final byte[] CONTENT_LENGTH_HEAD = "\r\nContent-Length:".getBytes();
-    private static final byte[] CONTENT_TYPE_HEAD = "\r\nContent-Type:".getBytes();
-
-    private byte[] contentType = Consts.TEXT_TYPE;
-    /**
-     * 响应体
-     */
-    public String body;
-
-    /**
-     * 请求头 或者 响应头
-     */
-    public Map<byte[], byte[]> headers = new HashMap<>();
-
-    @Override
-    public byte[] getHeader(byte[] name) {
-        for (Map.Entry<byte[], byte[]> entry : headers.entrySet()) {
-            if (Arrays.equals(entry.getKey(), name)) {
-                return entry.getValue();
-            }
-        }
-        return null;
-    }
-
-    @Override
-    public byte[] getHeader(String name) {
-        return getHeader(name.getBytes());
-    }
-
-    @Override
-    public void setHeader(byte[] name, byte[] value) {
-        headers.put(name, value);
-    }
-
-    @Override
-    public void setContentType(String contentType) {
-        this.contentType = contentType.getBytes();
-    }
-
-    @Override
-    public void setContentType(byte[] contentType) {
-        this.contentType = contentType;
-    }
-
-    public ByteBuffer toBuffer(ByteBuffer byteBuffer) {
-        byteBuffer.put(PROTOCOL);
-        byteBuffer.put(STATUS_200);
-        byteBuffer.put(DateUtil.getDate());
-        byteBuffer.put(CONTENT_LENGTH_HEAD);
-        byteBuffer.put(String.valueOf(body.length()).getBytes());
-        byteBuffer.put(CONTENT_TYPE_HEAD);
-        byteBuffer.put(contentType);
-        byteBuffer.put(SERVER_NAME);
-        for (Map.Entry<byte[], byte[]> header : headers.entrySet()) {
-            byteBuffer.put(header.getKey());
-            byteBuffer.put(COLON);
-            byteBuffer.put(header.getValue());
-            byteBuffer.put(CRLF);
-        }
-        byteBuffer.put(CRLF);
-        byteBuffer.put(body.getBytes());
-        byteBuffer.flip();
-        return byteBuffer;
-    }
-
-    @Override
-    public boolean equals(Object o) {
-        if (this == o) {
-            return true;
-        }
-        if (o == null || getClass() != o.getClass()) {
-            return false;
-        }
-        HttpResponseEntity that = (HttpResponseEntity) o;
-        return Objects.equals(body, that.body) &&
-                Objects.equals(headers, that.headers);
-    }
-
-    @Override
-    public int hashCode() {
-
-        return Objects.hash(body, headers);
-    }
-}

+ 3 - 0
frameworks/Java/isocket-nio/src/main/java/cn/ibaijia/tfb/processor/Message.java

@@ -1,5 +1,8 @@
 package cn.ibaijia.tfb.processor;
 package cn.ibaijia.tfb.processor;
 
 
+/**
+ * @author longzl
+ */
 public class Message {
 public class Message {
 
 
     public String message;
     public String message;

+ 4 - 11
frameworks/Java/isocket-nio/src/main/java/cn/ibaijia/tfb/processor/PlanTextProcessor.java

@@ -1,11 +1,11 @@
 package cn.ibaijia.tfb.processor;
 package cn.ibaijia.tfb.processor;
 
 
 import cn.ibaijia.isocket.processor.Processor;
 import cn.ibaijia.isocket.processor.Processor;
+import cn.ibaijia.isocket.protocol.http.Consts;
+import cn.ibaijia.isocket.protocol.http.HttpEntity;
+import cn.ibaijia.isocket.protocol.http.HttpRequestEntity;
+import cn.ibaijia.isocket.protocol.http.HttpResponseEntity;
 import cn.ibaijia.isocket.session.Session;
 import cn.ibaijia.isocket.session.Session;
-import cn.ibaijia.tfb.Consts;
-import cn.ibaijia.tfb.http.HttpEntity;
-import cn.ibaijia.tfb.http.HttpRequestEntity;
-import cn.ibaijia.tfb.http.HttpResponseEntity;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSON;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
@@ -20,7 +20,6 @@ public class PlanTextProcessor implements Processor<HttpEntity> {
     public boolean process(final Session session, final HttpEntity httpEntity) {
     public boolean process(final Session session, final HttpEntity httpEntity) {
         HttpRequestEntity httpRequestEntity = (HttpRequestEntity) httpEntity;
         HttpRequestEntity httpRequestEntity = (HttpRequestEntity) httpEntity;
         String url = httpRequestEntity.url;
         String url = httpRequestEntity.url;
-        logger.trace("url:{}", url);
         if (Consts.URL_TEXT_PLAIN.equals(url)) {
         if (Consts.URL_TEXT_PLAIN.equals(url)) {
             HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
             HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
             httpResponseEntity.setContentType(Consts.TEXT_TYPE);
             httpResponseEntity.setContentType(Consts.TEXT_TYPE);
@@ -31,12 +30,6 @@ public class PlanTextProcessor implements Processor<HttpEntity> {
             httpResponseEntity.setContentType(Consts.JSON_TYPE);
             httpResponseEntity.setContentType(Consts.JSON_TYPE);
             httpResponseEntity.body = JSON.toJSONString(new Message("Hello, World!"));
             httpResponseEntity.body = JSON.toJSONString(new Message("Hello, World!"));
             session.write(httpResponseEntity);
             session.write(httpResponseEntity);
-        } else if (Consts.URL_STATE.equals(url)) {
-            HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
-            httpResponseEntity.setContentType(Consts.JSON_TYPE);
-            State state = new State();
-            httpResponseEntity.body = JSON.toJSONString(state);
-            session.write(httpResponseEntity);
         } else {
         } else {
             HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
             HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
             httpResponseEntity.setContentType(Consts.TEXT_TYPE);
             httpResponseEntity.setContentType(Consts.TEXT_TYPE);

+ 0 - 8
frameworks/Java/isocket-nio/src/main/java/cn/ibaijia/tfb/processor/State.java

@@ -1,8 +0,0 @@
-package cn.ibaijia.tfb.processor;
-
-public class State {
-
-//    public int sessionCount;
-//    public BufferState bufferState;
-
-}

+ 0 - 163
frameworks/Java/isocket-nio/src/main/java/cn/ibaijia/tfb/protocol/SimpleHttpProtocol.java

@@ -1,163 +0,0 @@
-package cn.ibaijia.tfb.protocol;
-
-import cn.ibaijia.isocket.protocol.Protocol;
-import cn.ibaijia.isocket.session.Session;
-import cn.ibaijia.tfb.http.HttpEntity;
-import cn.ibaijia.tfb.http.HttpRequestEntity;
-import cn.ibaijia.tfb.http.HttpResponseEntity;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.nio.ByteBuffer;
-import java.util.Arrays;
-
-/**
- * @author longzl
- */
-public class SimpleHttpProtocol implements Protocol<ByteBuffer, HttpEntity> {
-
-    private static final Logger logger = LoggerFactory.getLogger(SimpleHttpProtocol.class);
-    /**
-     * CR13 \r
-     * LF10 \n
-     * SPACE0 \SP
-     * COLON :
-     */
-    private static final byte CR13 = (byte) 13;
-    private static final byte LF10 = (byte) 10;
-    private static final byte SPACE0 = (byte) 32;
-    private static final byte COLON = (byte) 58;
-
-    private static final String httpEntityKey = "httpEntity";
-
-    /**
-     * 解析HTTP请求
-     *
-     * @param byteBuffer
-     * @param session
-     * @return
-     */
-    @Override
-    public HttpEntity decode(ByteBuffer byteBuffer, Session session) {
-        HttpRequestEntity httpEntity = (HttpRequestEntity) session.getAttribute(httpEntityKey);
-        if (httpEntity == null) {
-            httpEntity = new HttpRequestEntity();
-            session.setAttribute(httpEntityKey, httpEntity);
-        }
-        //解析header
-        if (!httpEntity.headerComplete() && byteBuffer.hasRemaining()) {
-            readHeader(byteBuffer, httpEntity);
-        }
-
-        if (httpEntity.headerComplete()) {
-            if (httpEntity.complete()) {
-                session.setAttribute(httpEntityKey, null);
-                return httpEntity;
-            }
-            // 解析request body
-            if (httpEntity.bodyBuffer != null && byteBuffer.hasRemaining()) {
-                readBody(byteBuffer, httpEntity);
-            }
-        }
-        return null;
-    }
-
-    private void readHeader(ByteBuffer byteBuffer, HttpRequestEntity httpEntity) {
-        try {
-            ByteBuffer buf = byteBuffer.duplicate();
-            int startPos = 0;
-            int endPos = 0;
-            while (byteBuffer.hasRemaining()) {
-                byte b = byteBuffer.get();
-                endPos++;
-                if (b == CR13) {
-                    httpEntity.crNum++;
-                } else if (b == LF10) {
-                    httpEntity.lfNum++;
-                } else {
-                    httpEntity.crNum = 0;
-                    httpEntity.lfNum = 0;
-                }
-
-                if (httpEntity.headerComplete()) {
-                    return;
-                }
-
-                if (httpEntity.isReadHeadLine()) {
-                    if (b == SPACE0) {
-                        int len = endPos - startPos - 1;
-                        byte[] bytes = new byte[len];
-                        buf.get(bytes, 0, len);
-                        startPos = endPos;
-                        buf.position(startPos);
-                        if (httpEntity.method == null) {
-                            httpEntity.method = new String(bytes);
-                        } else if (httpEntity.url == null) {
-                            httpEntity.url = new String(bytes);
-                        }
-                    } else if (httpEntity.crNum == 1 && httpEntity.lfNum == 1) {
-                        int len = endPos - startPos - 2;
-                        byte[] bytes = new byte[len];
-                        buf.get(bytes, 0, len);
-                        startPos = endPos;
-                        buf.position(startPos);
-                        httpEntity.protocol = new String(bytes);
-                    }
-                } else {
-                    if (b == COLON && httpEntity.tmp == null) {
-                        int len = endPos - startPos - 1;
-                        byte[] bytes = new byte[len];
-                        buf.get(bytes, 0, len);
-                        startPos = endPos;
-                        buf.position(startPos);
-                        httpEntity.tmp = bytes;
-                    } else if (httpEntity.crNum == 1 && httpEntity.lfNum == 1) {
-                        int len = endPos - startPos - 2;
-                        byte[] bytes = new byte[len];
-                        buf.get(bytes, 0, len);
-                        startPos = endPos;
-                        buf.position(startPos);
-                        httpEntity.setHeader(httpEntity.tmp, bytes);
-                        httpEntity.tmp = null;
-//                        if (Arrays.equals(CONTENT_LENGTH, httpEntity.tmp)) {
-//                            httpEntity.contentLength = (value == null ? 0 : Integer.valueOf(value));
-//                            httpEntity.bodyBuffer = ByteBuffer.allocate(httpEntity.contentLength);//TODO can pooling
-//                        }
-//                        if (Arrays.equals(CHUNKED, httpEntity.tmp)) {
-//                            httpEntity.chunked = true;
-//                            throw new RuntimeException("not support chunked");
-//                        }
-                    }
-                }
-            }
-        } catch (Exception e) {
-            logger.error("readHeader error.", e);
-        }
-    }
-
-    private void readBody(ByteBuffer byteBuffer, HttpRequestEntity httpEntity) {
-        try {
-            if (httpEntity.bodyBuffer.hasRemaining()) {
-                if (byteBuffer.remaining() <= httpEntity.bodyBuffer.remaining()) {
-                    httpEntity.bodyBuffer.put(byteBuffer);
-                } else {
-                    byte[] bytes = new byte[httpEntity.bodyBuffer.remaining()];
-                    byteBuffer.get(bytes);
-                    httpEntity.bodyBuffer.put(bytes);
-                }
-            }
-            if (!httpEntity.bodyBuffer.hasRemaining()) {
-                httpEntity.processBody();
-            }
-        } catch (Exception e) {
-            logger.error("readHeader error.", e);
-        }
-    }
-
-    @Override
-    public ByteBuffer encode(HttpEntity httpEntity, Session session) {
-        ByteBuffer byteBuffer = session.getHandler().getPooledByteBuff().get();
-        HttpResponseEntity httpResponseEntity = (HttpResponseEntity) httpEntity;
-        return httpResponseEntity.toBuffer(byteBuffer);
-    }
-}

+ 2 - 4
frameworks/Java/isocket-nio/src/main/resources/log4j2.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <?xml version="1.0" encoding="UTF-8"?>
-<Configuration status="info">
+<Configuration status="OFF">
     <Properties>
     <Properties>
         <Property name="logName">isocket-nio-tfb</Property>
         <Property name="logName">isocket-nio-tfb</Property>
     </Properties>
     </Properties>
@@ -25,9 +25,7 @@
         </RollingFile>
         </RollingFile>
     </Appenders>
     </Appenders>
     <Loggers>
     <Loggers>
-        <logger name="cn.ibaijia.tfb" level="info"/>
-        <logger name="cn.ibaijia.isocket" level="info"/>
-        <Root level="info">
+        <Root level="OFF">
             <AppenderRef ref="console"/>
             <AppenderRef ref="console"/>
             <AppenderRef ref="file"/>
             <AppenderRef ref="file"/>
         </Root>
         </Root>