grpc-java/netty
Sergii Tkachenko eaf8b28930
netty: create adaptive cumulator (#9558)
Creates "Adaptive" cumulator: cumulate ByteBuf's 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 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. In this case we'll be paying a memory overhead for each buffer, as well as 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 minComposeSize) to ensure runtime O(n^2) amortized to O(n).

Note: this reintroduces https://github.com/grpc/grpc-java/pull/7532, addressing the subtle issue (ref b/155940949) with `CompositeByteBuf.component()` indexes getting out of sync, which results in the merge operation producing broken buffers.
2022-09-27 19:21:46 -07:00
..
shaded Swap Animalsniffer to Java 8 and Android 19 2022-08-10 12:41:57 -07:00
src netty: create adaptive cumulator (#9558) 2022-09-27 19:21:46 -07:00
BUILD.bazel netty: implement UdsNameResolver and UdsNettyChannelProvider (#9113) 2022-05-02 16:41:50 -07:00
build.gradle Swap Animalsniffer to Java 8 and Android 19 2022-08-10 12:41:57 -07:00