Reactor Netty Documentation

repository·main·Indexed 25 days ago

https://github.com/reactor/reactor-netty

Reactor Netty provides non-blocking and backpressure-ready TCP, HTTP, UDP, and QUIC clients and servers based on the Netty framework. It includes modules for HTTP server and client implementation, QUIC support via reactor-netty-quic, and tracing decorators for HttpClient and HttpServer using Brave (deprecated as of 1.1.0 in favor of Micrometer Tracing).

Tokens
17.2K
Snippets
26
Records
121
Agent score
83%

What's inside Reactor Netty

  1. Understand TcpClient connection pooling behavior

    main

    By default, TcpClient.create() uses a shared ConnectionProvider that implements a "fixed" connection pool per remote host (hostname + port).

    Default Limits:

    • maxConnections: 500 active channels.
    • pendingAcquireMaxCount: 1000 pending channel acquisition attempts.

    Important Behavior: Unlike HttpClient, connections used by TcpClient are never returned to the pool; they are closed after use. When a connection is closed, its slot in the pool is freed, allowing a new connection to be opened. This ensures protocol compatibility is managed by the user/framework.

  2. Quickstart: Use Highlight.js on a web page

    main

    To use Highlight.js on a web page, include the library and a CSS theme, then call hljs.initHighlightingOnLoad(). The library will automatically find and highlight code inside <pre><code> tags by attempting to auto-detect the language.

    To explicitly specify a language when auto-detection fails, use the class attribute on the <code> tag with the language name (e.g., class="html") or use the language- or lang- prefixes.

    To disable highlighting for a specific block, use the nohighlight class.

    <link rel="stylesheet" href="/path/to/styles/default.css">
    <script src="/path/to/highlight.pack.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
    
    <!-- Explicit language specification -->
    <pre><code class="html">...</code></pre>
    
    <!-- Disable highlighting -->
    <pre><code class="nohighlight">...</code></pre>
  3. Quickstart Highlight.js on a web page

    main

    To use highlight.js with automatic language detection on a web page, link to the library and a style sheet, then call hljs.initHighlightingOnLoad(). This method automatically finds and highlights code inside <pre><code> tags.

    If automatic detection fails, you can manually specify the language using the class attribute on the <code> tag with the format language-{lang} or lang-{lang} (e.g., class="html").

    To prevent a specific block from being highlighted, use the nohighlight class.

    <link rel="stylesheet" href="/path/to/styles/default.css">
    <script src="/path/to/highlight.pack.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
    
    <!-- Manual language specification -->
    <pre><code class="html">...</code></pre>
    
    <!-- Disable highlighting -->
    <pre><code class="nohighlight">...</code></pre>
  4. Run highlighting in a Web Worker

    main

    To prevent large code blocks from freezing the browser UI, you can offload the highlighting process to a Web Worker.

    1. In the main script, capture the code content and post it to the worker.
    2. In the worker script, import the highlight.js library and use self.hljs.highlightAuto(data) to process the code, then post the result back.
    // Main script
    addEventListener('load', function() {
      var code = document.querySelector('#code');
      var worker = new Worker('worker.js');
      worker.onmessage = function(event) { code.innerHTML = event.data; }
      worker.postMessage(code.textContent);
    })
    // worker.js
    onmessage = function(event) {
      importScripts('<path>/highlight.pack.js');
      var result = self.hljs.highlightAuto(event.data);
      postMessage(result.value);
    }
  5. Monitor TCP Server metrics with Micrometer

    main

    The TCP server integrates with Micrometer. Metrics are prefixed with reactor.netty.tcp.server.

    Available metrics:

    • reactor.netty.tcp.server.connections.total (Gauge): Total opened connections.
    • reactor.netty.tcp.server.data.received (DistributionSummary): Bytes received.
    • reactor.netty.tcp.server.data.sent (DistributionSummary): Bytes sent.
    • reactor.netty.tcp.server.errors (Counter): Number of errors.
    • reactor.netty.tcp.server.tls.handshake.time (Timer): TLS handshake duration.

    To enable built-in integration:

    TcpServer.create()
        .metrics(true)
        .bind()
        .get()
        .onDispose()
        .block();
  6. Dispose Connection Pools

    main

    To prevent resource leaks, you must dispose of connection pools when they are no longer needed.

    • If using default ConnectionProvider: Use HttpResources#disposeLoopsAndConnections() or HttpResources#disposeLoopsAndConnectionsLater().
    • If using custom ConnectionProvider: Use ConnectionProvider#dispose(), ConnectionProvider#disposeLater(), or ConnectionProvider#disposeWhen().

    Warning: Disposing a resource makes it unavailable for all clients currently using it.

  7. Write data to a TCP connection

    main
    To send data, attach an I/O handler that has access to NettyOutbound. Alternatively, use Connection#outbound() for more control. Note that when using Connection#outbound(), you must explicitly call Connection#dispose() to close the connection, whereas an I/O handler closes the connection when the provided Publisher finishes.
  8. Eagerly initialize HttpServer resources

    main

    By default, HttpServer resources (event loop groups, native transport, and security libraries like OpenSsl) are initialized on demand during the bind operation. To avoid latency during the first request, you can use the .warmup() method to preload these resources.

    HttpServer.create()
        .warmup()
        .handle((request, response) -> response.sendString(Mono.just("Hello World!")))
        .bindNow();