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

chore(netty): improve health service #24

Merged
merged 1 commit into from
Mar 1, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpServerExpectContinueHandler;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
Expand Down Expand Up @@ -80,7 +80,6 @@ protected void doStart() throws Exception {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast(new HttpServerCodec())
.addLast(new HttpServerExpectContinueHandler())
.addLast(new Handler());
}
});
Expand All @@ -98,33 +97,37 @@ protected void doStop() throws Exception {
}
}

private class Handler extends SimpleChannelInboundHandler<HttpRequest> {
private class Handler extends SimpleChannelInboundHandler<HttpObject> {
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
HttpResponseStatus status;
ByteBuf content;

if (!Objects.equals(path, msg.uri())) {
status = HttpResponseStatus.NOT_FOUND;
content = Unpooled.wrappedBuffer(KO);
} else if (context.getStatus() == ServiceStatus.Started) {
status = HttpResponseStatus.OK;
content = Unpooled.wrappedBuffer(OK);
} else {
status = HttpResponseStatus.SERVICE_UNAVAILABLE;
content = Unpooled.wrappedBuffer(KO);
}
protected void channelRead0(ChannelHandlerContext ctx, HttpObject object) throws Exception {
if (object instanceof HttpRequest) {
final HttpRequest msg = (HttpRequest)object;

HttpResponseStatus status;
ByteBuf content;

FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);
response.headers().set(CONTENT_TYPE, "text/plain");
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
if (!Objects.equals(path, msg.uri())) {
status = HttpResponseStatus.NOT_FOUND;
content = Unpooled.wrappedBuffer(KO);
} else if (context.getStatus() == ServiceStatus.Started) {
status = HttpResponseStatus.OK;
content = Unpooled.wrappedBuffer(OK);
} else {
status = HttpResponseStatus.SERVICE_UNAVAILABLE;
content = Unpooled.wrappedBuffer(KO);
}

ctx.write(response).addListener(ChannelFutureListener.CLOSE);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);
response.headers().set(CONTENT_TYPE, "text/plain");
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());

ctx.write(response).addListener(ChannelFutureListener.CLOSE);
}
}
}
}
5 changes: 3 additions & 2 deletions camel-k-runtime-health/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
<Configuration status="INFO">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS}|%-5level|%t|%c{1} - %msg%n"/>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS}|%-5level|%t|%c - %msg%n"/>
</Console>
<Null name="NONE"/>
</Appenders>

<Loggers>
<Logger name="io.netty" level="DEBUG"/>
<Logger name="io.netty" level="INFO"/>
<Logger name="io.netty.handler.logging" level="DEBUG"/>
<Root level="INFO">
<!--<AppenderRef ref="STDOUT"/>-->
<AppenderRef ref="NONE"/>
Expand Down