Class AdaptiveCumulator

java.lang.Object
io.netty.handler.codec.AdaptiveCumulator
All Implemented Interfaces:
ByteToMessageDecoder.Cumulator

public final class AdaptiveCumulator extends Object implements ByteToMessageDecoder.Cumulator
"Adaptive" cumulator: cumulate ByteBufs by dynamically switching between merge and compose strategies.
  • Constructor Details

    • AdaptiveCumulator

      public AdaptiveCumulator(int composeMinSize)
      Parameters:
      composeMinSize - Determines the minimal size of the buffer that should be composed (added as a new component of the CompositeByteBuf). If the total size of the last component (tail) and the incoming buffer is below this value, the incoming buffer is appended to the tail, and the new component is not added.
  • Method Details

    • cumulate

      public ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in)
      "Adaptive" cumulator: cumulate ByteBufs by dynamically switching between merge and compose strategies.

      This cumulator applies a heuristic to make a decision whether to track a reference to the buffer with bytes received from the network stack in an array ("zero-copy"), or to merge into the last component (the tail) by performing a memory copy.

      It is necessary as a protection from a potential attack on the ByteToMessageDecoder.COMPOSITE_CUMULATOR. Consider a pathological case when an attacker sends TCP packages containing a single byte of data, and forcing the cumulator to track each one in a separate buffer. The cost is memory overhead for each buffer, and extra compute to read the cumulation.

      Implemented heuristic establishes a minimal threshold for the total size of the tail and incoming buffer, below which they are merged. The sum of the tail and the incoming buffer is used to avoid a case where attacker alternates the size of data packets to trick the cumulator into always selecting compose strategy.

      Merging strategy attempts to minimize unnecessary memory writes. When possible, it expands the tail capacity and only copies the incoming buffer into available memory. Otherwise, when both tail and the buffer must be copied, the tail is reallocated (or fully replaced) with a new buffer of exponentially increasing capacity (bounded to composeMinSize) to ensure runtime O(n^2) is amortized to O(n).

      Specified by:
      cumulate in interface ByteToMessageDecoder.Cumulator