You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by GitBox <gi...@apache.org> on 2019/02/28 23:44:25 UTC

[GitHub] nicolaferraro commented on a change in pull request #22: Add runtime health endpoint

nicolaferraro commented on a change in pull request #22: Add runtime health endpoint
URL: https://github.com/apache/camel-k-runtime/pull/22#discussion_r261432125
 
 

 ##########
 File path: camel-k-runtime-health/src/main/java/org/apache/camel/k/health/HealthEndpoint.java
 ##########
 @@ -0,0 +1,130 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.k.health;
+
+import java.util.Objects;
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.SimpleChannelInboundHandler;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+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.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;
+import io.netty.util.AsciiString;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.support.ServiceSupport;
+
+public class HealthEndpoint extends ServiceSupport {
+    private static final AsciiString CONTENT_TYPE = AsciiString.cached("Content-Type");
+    private static final AsciiString CONTENT_LENGTH = AsciiString.cached("Content-Length");
+    private static final byte[] OK = {'O', 'K'};
+    private static final byte[] KO = {'K', 'O'};
+
+    private final CamelContext context;
+    private final String bindHost;
+    private final int bindPort;
+    private final String path;
+
+    private EventLoopGroup bossGroup;
+    private EventLoopGroup workerGroup;
+    private Channel channel;
+
+    public HealthEndpoint(CamelContext context, String bindHost, int bindPort, String path) {
+        this.context = context;
+        this.bindHost = bindHost;
+        this.bindPort = bindPort;
+        this.path = path;
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        bossGroup = new NioEventLoopGroup(1);
+        workerGroup = new NioEventLoopGroup();
+
+        ServerBootstrap b = new ServerBootstrap();
+        b.group(bossGroup, workerGroup)
+            .channel(NioServerSocketChannel.class)
+            .handler(new LoggingHandler(LogLevel.DEBUG))
+            .childHandler(new ChannelInitializer<SocketChannel>() {
+                @Override
+                protected void initChannel(SocketChannel ch) throws Exception {
+                    ch.pipeline()
+                        .addLast(new HttpServerCodec())
+                        .addLast(new HttpServerExpectContinueHandler())
+                        .addLast(new Handler());
+                }
+            });
+
+        channel = b.bind(bindHost, bindPort).channel();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if (bossGroup != null) {
+            bossGroup.shutdownGracefully();
+        }
+        if (workerGroup != null) {
+            workerGroup.shutdownGracefully();
+        }
+    }
+
+    private class Handler extends SimpleChannelInboundHandler<HttpRequest> {
+        @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) {
 
 Review comment:
   Is this true when routes are down for master-component/routepolicy?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services