Class MoleculerHandler

java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.SimpleChannelInboundHandler<Object>
services.moleculer.web.netty.MoleculerHandler
All Implemented Interfaces:
io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler

public class MoleculerHandler extends io.netty.channel.SimpleChannelInboundHandler<Object>
  • Nested Class Summary

    Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

    io.netty.channel.ChannelHandler.Sharable
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final services.moleculer.ServiceBroker
     
    protected final ApiGateway
     
    protected io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker
     
    protected String
     
    protected NettyWebRequest
     
    protected final NettyWebSocketRegistry
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    MoleculerHandler(ApiGateway gateway, services.moleculer.ServiceBroker broker, NettyWebSocketRegistry nettyWebSocketRegistry)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Sends an error to the in-flight request body stream (if any), releasing downstream resources.
    void
    channelInactive(io.netty.channel.ChannelHandlerContext ctx)
    Releases the half-open request stream when the connection drops in the middle of an upload, so a waiting action does not leak the stream.
    protected void
    channelRead0(io.netty.channel.ChannelHandlerContext ctx, Object request)
     
    void
    exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause)
    Aborts the request stream and closes the channel on a transport-level error (e.g. an HTTP decoder failure on a malformed request, or a reset by the peer).
    protected boolean
    Cheap, allocation-free check for an in-flight request body stream that is still open.
    void
    userEventTriggered(io.netty.channel.ChannelHandlerContext ctx, Object evt)
    Closes connections that go silent while an HTTP request is still being read.

    Methods inherited from class io.netty.channel.SimpleChannelInboundHandler

    acceptInboundMessage, channelRead

    Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter

    channelActive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged

    Methods inherited from class io.netty.channel.ChannelHandlerAdapter

    ensureNotSharable, handlerAdded, handlerRemoved, isSharable

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.netty.channel.ChannelHandler

    handlerAdded, handlerRemoved
  • Field Details

    • gateway

      protected final ApiGateway gateway
    • broker

      protected final services.moleculer.ServiceBroker broker
    • req

      protected volatile NettyWebRequest req
    • webSocketRegistry

      protected final NettyWebSocketRegistry webSocketRegistry
    • path

      protected volatile String path
    • handshaker

      protected volatile io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker handshaker
  • Constructor Details

  • Method Details

    • channelRead0

      protected void channelRead0(io.netty.channel.ChannelHandlerContext ctx, Object request) throws Exception
      Specified by:
      channelRead0 in class io.netty.channel.SimpleChannelInboundHandler<Object>
      Throws:
      Exception
    • userEventTriggered

      public void userEventTriggered(io.netty.channel.ChannelHandlerContext ctx, Object evt) throws Exception
      Closes connections that go silent while an HTTP request is still being read. The IdleStateHandler (added by the NettyServer only when a read-timeout is configured) fires a reader-idle event; we abort any half-received request body and close the channel. This is the Slowloris defence. Established WebSockets never reach this code, because the idle handler is removed from the pipeline on a successful handshake.
      Specified by:
      userEventTriggered in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      userEventTriggered in class io.netty.channel.ChannelInboundHandlerAdapter
      Throws:
      Exception
    • channelInactive

      public void channelInactive(io.netty.channel.ChannelHandlerContext ctx) throws Exception
      Releases the half-open request stream when the connection drops in the middle of an upload, so a waiting action does not leak the stream. Runs on every connection close, so it allocates the exception only when there is actually an in-flight body stream to abort (the abnormal case) - a normal, completed request takes the cheap, allocation-free path.
      Specified by:
      channelInactive in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelInactive in class io.netty.channel.ChannelInboundHandlerAdapter
      Throws:
      Exception
    • exceptionCaught

      public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) throws Exception
      Aborts the request stream and closes the channel on a transport-level error (e.g. an HTTP decoder failure on a malformed request, or a reset by the peer). Closing silently here also avoids log-flooding from malformed or malicious clients.
      Specified by:
      exceptionCaught in interface io.netty.channel.ChannelHandler
      Specified by:
      exceptionCaught in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      exceptionCaught in class io.netty.channel.ChannelInboundHandlerAdapter
      Throws:
      Exception
    • isRequestStreamOpen

      protected boolean isRequestStreamOpen()
      Cheap, allocation-free check for an in-flight request body stream that is still open. Used to skip building an exception on the common path (no body, or the request already completed).
      Returns:
      true if there is an open request body stream to abort
    • abortRequestStream

      protected void abortRequestStream(Throwable cause)
      Sends an error to the in-flight request body stream (if any), releasing downstream resources. Idempotent: PacketStream.sendError is a no-op once the stream is closed.
      Parameters:
      cause - the reason the request was aborted (never null)