|
@@ -1,28 +1,20 @@
|
|
|
package hello;
|
|
|
|
|
|
-import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
|
|
|
-import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
|
|
|
-import static io.netty.handler.codec.http.HttpHeaderNames.DATE;
|
|
|
-import static io.netty.handler.codec.http.HttpHeaderNames.SERVER;
|
|
|
-import static io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_JSON;
|
|
|
-import static io.netty.handler.codec.http.HttpHeaderValues.TEXT_PLAIN;
|
|
|
+import static hello.HttpResponses.makeJsonResponse;
|
|
|
+import static hello.HttpResponses.makePlaintextResponse;
|
|
|
+import static hello.JsonUtils.acquireJsonStreamFromEventLoop;
|
|
|
+import static hello.JsonUtils.releaseJsonStreamFromEventLoop;
|
|
|
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
|
|
|
-import static io.netty.handler.codec.http.HttpResponseStatus.OK;
|
|
|
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
import java.text.DateFormat;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import com.jsoniter.output.JsonStream;
|
|
|
-import com.jsoniter.output.JsonStreamPool;
|
|
|
-import com.jsoniter.spi.JsonException;
|
|
|
|
|
|
-import io.netty.buffer.ByteBuf;
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
import io.netty.channel.ChannelFutureListener;
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
@@ -33,51 +25,21 @@ import io.netty.handler.codec.http.FullHttpResponse;
|
|
|
import io.netty.handler.codec.http.HttpRequest;
|
|
|
import io.netty.handler.codec.http.LastHttpContent;
|
|
|
import io.netty.util.AsciiString;
|
|
|
-import io.netty.util.CharsetUtil;
|
|
|
import io.netty.util.ReferenceCountUtil;
|
|
|
import io.netty.util.concurrent.FastThreadLocal;
|
|
|
|
|
|
public class HelloServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
|
|
|
- private static final FastThreadLocal<DateFormat> FORMAT = new FastThreadLocal<DateFormat>() {
|
|
|
- @Override
|
|
|
- protected DateFormat initialValue() {
|
|
|
- return new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- private static Message newMsg() {
|
|
|
- return new Message("Hello, World!");
|
|
|
- }
|
|
|
-
|
|
|
- private static byte[] serializeMsg(Message obj) {
|
|
|
- JsonStream stream = JsonStreamPool.borrowJsonStream();
|
|
|
- try {
|
|
|
- stream.reset(null);
|
|
|
- stream.writeVal(Message.class, obj);
|
|
|
- return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
|
|
|
- } catch (IOException e) {
|
|
|
- throw new JsonException(e);
|
|
|
- } finally {
|
|
|
- JsonStreamPool.returnJsonStream(stream);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static int jsonLen() {
|
|
|
- return serializeMsg(newMsg()).length;
|
|
|
- }
|
|
|
+ private static final FastThreadLocal<DateFormat> FORMAT = new FastThreadLocal<>() {
|
|
|
+ @Override
|
|
|
+ protected DateFormat initialValue() {
|
|
|
+ return new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
- private static final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(CharsetUtil.UTF_8);
|
|
|
- private static final int STATIC_PLAINTEXT_LEN = STATIC_PLAINTEXT.length;
|
|
|
+ protected volatile AsciiString date = new AsciiString(FORMAT.get().format(new Date()));
|
|
|
|
|
|
- private static final CharSequence PLAINTEXT_CLHEADER_VALUE = AsciiString.cached(String.valueOf(STATIC_PLAINTEXT_LEN));
|
|
|
- private static final int JSON_LEN = jsonLen();
|
|
|
- private static final CharSequence JSON_CLHEADER_VALUE = AsciiString.cached(String.valueOf(JSON_LEN));
|
|
|
- private static final CharSequence SERVER_NAME = AsciiString.cached("Netty");
|
|
|
-
|
|
|
- private volatile CharSequence date = new AsciiString(FORMAT.get().format(new Date()));
|
|
|
-
|
|
|
- HelloServerHandler(ScheduledExecutorService service) {
|
|
|
+ public HelloServerHandler(ScheduledExecutorService service) {
|
|
|
service.scheduleWithFixedDelay(new Runnable() {
|
|
|
private final DateFormat format = FORMAT.get();
|
|
|
|
|
@@ -118,42 +80,39 @@ public class HelloServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
String uri = request.uri();
|
|
|
switch (uri) {
|
|
|
case "/plaintext":
|
|
|
- writePlainResponse(ctx, Unpooled.wrappedBuffer(STATIC_PLAINTEXT));
|
|
|
+ writePlainResponse(ctx, date);
|
|
|
return;
|
|
|
case "/json":
|
|
|
- byte[] json = serializeMsg(newMsg());
|
|
|
- writeJsonResponse(ctx, Unpooled.wrappedBuffer(json));
|
|
|
+ // even for the virtual thread case we expect virtual threads to be executed inlined!
|
|
|
+ var stream = acquireJsonStreamFromEventLoop();
|
|
|
+ try {
|
|
|
+ writeJsonResponse(ctx, stream, date);
|
|
|
+ } finally {
|
|
|
+ releaseJsonStreamFromEventLoop(stream);
|
|
|
+ }
|
|
|
return;
|
|
|
}
|
|
|
+ // we drain in-flight responses before closing the connection
|
|
|
+ channelReadComplete(ctx);
|
|
|
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND, Unpooled.EMPTY_BUFFER, false);
|
|
|
- ctx.write(response).addListener(ChannelFutureListener.CLOSE);
|
|
|
- }
|
|
|
-
|
|
|
- private void writePlainResponse(ChannelHandlerContext ctx, ByteBuf buf) {
|
|
|
- ctx.write(makeResponse(buf, TEXT_PLAIN, PLAINTEXT_CLHEADER_VALUE), ctx.voidPromise());
|
|
|
+ ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
|
|
}
|
|
|
|
|
|
- private void writeJsonResponse(ChannelHandlerContext ctx, ByteBuf buf) {
|
|
|
- ctx.write(makeResponse(buf, APPLICATION_JSON, JSON_CLHEADER_VALUE), ctx.voidPromise());
|
|
|
+ protected void writePlainResponse(ChannelHandlerContext ctx, AsciiString date) {
|
|
|
+ ctx.write(makePlaintextResponse(date), ctx.voidPromise());
|
|
|
}
|
|
|
|
|
|
- private FullHttpResponse makeResponse(ByteBuf buf, CharSequence contentType, CharSequence contentLength) {
|
|
|
- final FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, buf, false);
|
|
|
- response.headers()
|
|
|
- .set(CONTENT_TYPE, contentType)
|
|
|
- .set(SERVER, SERVER_NAME)
|
|
|
- .set(DATE, date)
|
|
|
- .set(CONTENT_LENGTH, contentLength);
|
|
|
- return response;
|
|
|
+ protected void writeJsonResponse(ChannelHandlerContext ctx, JsonStream stream, AsciiString date) {
|
|
|
+ ctx.write(makeJsonResponse(stream, date), ctx.voidPromise());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
|
|
ctx.close();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
|
+ public void channelReadComplete(ChannelHandlerContext ctx) {
|
|
|
ctx.flush();
|
|
|
}
|
|
|
}
|