Home / Class/ State Class — netty Architecture

State Class — netty Architecture

Architecture documentation for the State class in UniformStreamByteDistributor.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  52e74647_0bb8_efef_82ff_44ea07c3c527["State"]
  d72c463f_00af_2ec5_ed7b_8e37cf72c7b4["UniformStreamByteDistributor.java"]
  52e74647_0bb8_efef_82ff_44ea07c3c527 -->|defined in| d72c463f_00af_2ec5_ed7b_8e37cf72c7b4
  afac8524_30ec_b001_669b_614bd09f135c["State()"]
  52e74647_0bb8_efef_82ff_44ea07c3c527 -->|method| afac8524_30ec_b001_669b_614bd09f135c
  eb6bf10d_b6fd_8515_c69e_9cd09b8d3ba9["updateStreamableBytes()"]
  52e74647_0bb8_efef_82ff_44ea07c3c527 -->|method| eb6bf10d_b6fd_8515_c69e_9cd09b8d3ba9
  5f2d025b_d4ae_5222_7318_105d186fc0cd["write()"]
  52e74647_0bb8_efef_82ff_44ea07c3c527 -->|method| 5f2d025b_d4ae_5222_7318_105d186fc0cd
  7d4300b6_0588_0233_9ef1_d99a483aea7e["addToQueue()"]
  52e74647_0bb8_efef_82ff_44ea07c3c527 -->|method| 7d4300b6_0588_0233_9ef1_d99a483aea7e
  cf2d15f2_54e5_7731_0ea5_c72b2f0f4148["removeFromQueue()"]
  52e74647_0bb8_efef_82ff_44ea07c3c527 -->|method| cf2d15f2_54e5_7731_0ea5_c72b2f0f4148
  1e906bd4_0f9f_15e3_0931_d67395e16bd9["close()"]
  52e74647_0bb8_efef_82ff_44ea07c3c527 -->|method| 1e906bd4_0f9f_15e3_0931_d67395e16bd9

Relationship Graph

Source Code

codec-http2/src/main/java/io/netty/handler/codec/http2/UniformStreamByteDistributor.java lines 131–201

    private final class State {
        final Http2Stream stream;
        int streamableBytes;
        boolean windowNegative;
        boolean enqueued;
        boolean writing;

        State(Http2Stream stream) {
            this.stream = stream;
        }

        void updateStreamableBytes(int newStreamableBytes, boolean hasFrame, int windowSize) {
            assert hasFrame || newStreamableBytes == 0 :
                "hasFrame: " + hasFrame + " newStreamableBytes: " + newStreamableBytes;

            int delta = newStreamableBytes - streamableBytes;
            if (delta != 0) {
                streamableBytes = newStreamableBytes;
                totalStreamableBytes += delta;
            }
            // In addition to only enqueuing state when they have frames we enforce the following restrictions:
            // 1. If the window has gone negative. We never want to queue a state. However we also don't want to
            //    Immediately remove the item if it is already queued because removal from deque is O(n). So
            //    we allow it to stay queued and rely on the distribution loop to remove this state.
            // 2. If the window is zero we only want to queue if we are not writing. If we are writing that means
            //    we gave the state a chance to write zero length frames. We wait until updateStreamableBytes is
            //    called again before this state is allowed to write.
            windowNegative = windowSize < 0;
            if (hasFrame && (windowSize > 0 || windowSize == 0 && !writing)) {
                addToQueue();
            }
        }

        /**
         * Write any allocated bytes for the given stream and updates the streamable bytes,
         * assuming all of the bytes will be written.
         */
        void write(int numBytes, Writer writer) throws Http2Exception {
            writing = true;
            try {
                // Write the allocated bytes.
                writer.write(stream, numBytes);
            } catch (Throwable t) {
                throw connectionError(INTERNAL_ERROR, t, "byte distribution write error");
            } finally {
                writing = false;
            }
        }

        void addToQueue() {
            if (!enqueued) {
                enqueued = true;
                queue.addLast(this);
            }
        }

        void removeFromQueue() {
            if (enqueued) {
                enqueued = false;
                queue.remove(this);
            }
        }

        void close() {
            // Remove this state from the queue.
            removeFromQueue();

            // Clear the streamable bytes.
            updateStreamableBytes(0, false, 0);
        }
    }

Frequently Asked Questions

What is the State class?
State is a class in the netty codebase, defined in codec-http2/src/main/java/io/netty/handler/codec/http2/UniformStreamByteDistributor.java.
Where is State defined?
State is defined in codec-http2/src/main/java/io/netty/handler/codec/http2/UniformStreamByteDistributor.java at line 131.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free