Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some improvements for some more speed ;) #12

Merged
merged 1 commit into from
Mar 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion netty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This is the netty portion of a [benchmarking test suite](../) comparing a variet
* [Netty 4.0.0.Beta2](http://netty.io/)

## References
* shttps://github.com/netty/netty/tree/master/example/src/main/java/io/netty/example/http/snoop
* https://github.com/netty/netty/tree/master/example/src/main/java/io/netty/example/http/snoop

## Test URLs

Expand Down
2 changes: 1 addition & 1 deletion netty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
<version>4.0.0.Beta2</version>
<version>4.0.0.CR1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
34 changes: 23 additions & 11 deletions netty/src/main/java/hello/HelloServerHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package hello;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.MessageBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
Expand All @@ -17,14 +17,11 @@
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.codec.http.ServerCookieEncoder;
import io.netty.util.CharsetUtil;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

import java.io.*;
Expand All @@ -43,13 +40,22 @@ public class HelloServerHandler extends ChannelInboundMessageHandlerAdapter<Obje
private final StringBuilder buf = new StringBuilder();
private static final ObjectMapper mapper = new ObjectMapper();

private boolean flush;

@Override
protected boolean beginMessageReceived(ChannelHandlerContext ctx) throws Exception {
flush = false;
return super.beginMessageReceived(ctx);
}

@Override
public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
MessageBuf<Object> out = ctx.nextOutboundMessageBuffer();
if (msg instanceof HttpRequest) {
HttpRequest request = this.request = (HttpRequest) msg;

if (is100ContinueExpected(request)) {
send100Continue(ctx);
send100Continue(out);
}

Map<String, String> data = new HashMap<String, String>();
Expand All @@ -71,7 +77,7 @@ public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Except
if (msg instanceof HttpContent) {
if (msg instanceof LastHttpContent) {
LastHttpContent trailer = (LastHttpContent) msg;
writeResponse(ctx, trailer);
writeResponse(ctx, out, trailer);
}
}
}
Expand All @@ -87,7 +93,7 @@ private static void appendDecoderResult(StringBuilder buf, HttpObject o) {
buf.append("\r\n");
}

private void writeResponse(ChannelHandlerContext ctx, HttpObject currentObj) {
private void writeResponse(ChannelHandlerContext ctx, MessageBuf<Object> out, HttpObject currentObj) {
// Decide whether to close the connection or not.
boolean keepAlive = isKeepAlive(request);
// Build the response object.
Expand Down Expand Up @@ -122,22 +128,28 @@ private void writeResponse(ChannelHandlerContext ctx, HttpObject currentObj) {
}

// Write the response.
ctx.nextOutboundMessageBuffer().add(response);
out.add(response);

// Close the non-keep-alive connection after the write operation is done.
if (!keepAlive) {
flush = false;
ctx.flush().addListener(ChannelFutureListener.CLOSE);
} else {
flush = true;
}
}

private static void send100Continue(ChannelHandlerContext ctx) {
private void send100Continue(MessageBuf<Object> out) {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, CONTINUE);
ctx.nextOutboundMessageBuffer().add(response);
out.add(response);
flush = true;
}

@Override
public void endMessageReceived(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
if (flush) {
ctx.flush();
}
}

@Override
Expand Down