|
@@ -8,6 +8,9 @@
|
|
|
|
|
|
package org.smartboot.http;
|
|
package org.smartboot.http;
|
|
|
|
|
|
|
|
+import com.jsoniter.output.JsonStream;
|
|
|
|
+import com.jsoniter.output.JsonStreamPool;
|
|
|
|
+import com.jsoniter.spi.JsonException;
|
|
import org.smartboot.http.server.HttpMessageProcessor;
|
|
import org.smartboot.http.server.HttpMessageProcessor;
|
|
import org.smartboot.http.server.decode.Http11Request;
|
|
import org.smartboot.http.server.decode.Http11Request;
|
|
import org.smartboot.http.server.decode.HttpRequestProtocol;
|
|
import org.smartboot.http.server.decode.HttpRequestProtocol;
|
|
@@ -40,10 +43,19 @@ public class Bootstrap {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void doHandle(HttpRequest request, HttpResponse response) throws IOException {
|
|
public void doHandle(HttpRequest request, HttpResponse response) throws IOException {
|
|
- byte[] b = JSON.toJson(new Message("Hello, World!"));
|
|
|
|
- response.setContentLength(b.length);
|
|
|
|
|
|
+
|
|
response.setContentType("application/json");
|
|
response.setContentType("application/json");
|
|
- response.getOutputStream().write(b);
|
|
|
|
|
|
+ JsonStream stream = JsonStreamPool.borrowJsonStream();
|
|
|
|
+ try {
|
|
|
|
+ stream.reset(null);
|
|
|
|
+ stream.writeVal(Message.class, new Message("Hello, World!"));
|
|
|
|
+ response.setContentLength(stream.buffer().tail());
|
|
|
|
+ response.getOutputStream().write(stream.buffer().data(), 0, stream.buffer().tail());
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new JsonException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ JsonStreamPool.returnJsonStream(stream);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
});
|
|
});
|
|
http(processor);
|
|
http(processor);
|
|
@@ -55,10 +67,10 @@ public class Bootstrap {
|
|
AioQuickServer<Http11Request> server = new AioQuickServer<>(8080, new HttpRequestProtocol(), processor);
|
|
AioQuickServer<Http11Request> server = new AioQuickServer<>(8080, new HttpRequestProtocol(), processor);
|
|
server.setReadBufferSize(1024 * 4);
|
|
server.setReadBufferSize(1024 * 4);
|
|
int cpuNum = Runtime.getRuntime().availableProcessors();
|
|
int cpuNum = Runtime.getRuntime().availableProcessors();
|
|
- int shareNum = Runtime.getRuntime().availableProcessors() * 3 / 4;
|
|
|
|
|
|
+ int shareNum = Runtime.getRuntime().availableProcessors() * 7 / 8;
|
|
server.setBossThreadNum(cpuNum);
|
|
server.setBossThreadNum(cpuNum);
|
|
server.setBossShareToWorkerThreadNum(shareNum);
|
|
server.setBossShareToWorkerThreadNum(shareNum);
|
|
- server.setWorkerThreadNum(cpuNum - shareNum);
|
|
|
|
|
|
+ server.setWorkerThreadNum((cpuNum - shareNum) * 2);
|
|
try {
|
|
try {
|
|
server.start();
|
|
server.start();
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|