golang/net

repository·master·Indexed 25 days ago

https://github.com/golang/net

Supplementary networking packages for the Go programming language providing extended protocol support and utilities beyond the standard library. Includes the golang.org/x/net/http2 package for HTTP/2 transport and server implementations, the h2i interactive HTTP/2 console debugger, and WebSocket client and server implementations.

Tokens
2.9K
Snippets
4
Records
28
Agent score
84%

What's inside golang-net

  1. Overview of Go Networking packages

    master
    The golang/net repository contains supplementary Go networking packages that extend the standard library's networking capabilities. These packages provide specialized protocols and networking utilities for Go developers.
  2. Understand the HTTP/2 implementation status in golang.org/x/net/http2

    master

    The golang.org/x/net/http2 package contains two different implementations of the HTTP/2 transport and server, and its behavior depends on your Go version:

    1. The Original Implementation: This is the legacy implementation. It is used automatically if your Go version is less than 1.27.
    2. The Wrapping Implementation: This is a reimplementation of the x/net/http2 APIs that wraps the standard library's net/http. It is used automatically if your Go version is 1.27 or higher.

    Note: As of Go 1.27, the standard library package net/http/internal/http2 is the official source of truth for Go's HTTP/2 implementation. New features are developed there; x/net only receives critical bug and security fixes.

  3. Use the legacy HTTP/2 implementation via build tags

    master
    If you are using Go 1.27 or later but specifically need to use the original (legacy) HTTP/2 implementation instead of the new wrapping implementation, you can force its use by setting the http2legacy build tag during your build process.
  4. h2i interactive commands

    master

    Once connected, you can use the following interactive features:

    • Send raw frames: Send frames like PING or SETTINGS directly.
    • HTTP/1.n to HTTP/2 conversion: Type standard HTTP/1.n headers and requests (e.g., GET / HTTP/1.1) and h2i will automatically HPACK and frame-ify them for HTTP/2.
    • Tab completion: Use tab completion for commands and options.

    Example usage:

    h2i> PING h2iSayHI
    h2i> headers
    (as HTTP/1.1)> GET / HTTP/1.1
    (as HTTP/1.1)> Host: example.com
    (as HTTP/1.1)> 
  5. Use h2i to debug HTTP/2 connections

    master

    Run h2i followed by the target hostname to start an interactive session. h2i allows you to send raw HTTP/2 frames (like PING or SETTINGS), type HTTP/1.n requests that are automatically converted to HTTP/2 frames, and view pretty-printed received frames with HPACK decoding.

    $ h2i google.com
  6. Use Trailers in HTTP/2

    master
    HTTP/2 supports trailers. You can declare trailers by adding a header with the prefix Trailer:. For example, if you want to send a trailer named X-My-Trailer, you should set the header Trailer:X-My-Trailer in the response headers. The implementation also supports a 'hack' where any header key starting with Trailer: is automatically promoted to a trailer after the handler finishes.
  7. Handle HTTP/2 server graceful shutdown

    master

    The server can initiate a graceful shutdown by sending a GOAWAY frame with the error code ErrCodeNo. This informs the client that the server is shutting down and no new streams should be created, while allowing existing streams to complete.

    Note that a non-graceful shutdown (sending GOAWAY with an error code) will cause the connection to close after a timeout (defaulting to 1 * time.Second) to allow the client to receive the GOAWAY frame and avoid immediate TCP RSTs.

  8. Configure HTTP/2 Server settings

    master

    The http2.Server struct allows you to customize the behavior of the HTTP/2 implementation. Key configuration areas include:

    • Timeouts: IdleTimeout and ReadTimeout can be inherited from the base http.Server or set explicitly.
    • Flow Control: MaxUploadBufferPerStream and MaxUploadBufferPerConnection control the window sizes for flow control.
    • Concurrency: MaxConcurrentStreams sets the limit for concurrent streams advertised to the client.
    • Security: PermitProhibitedCipherSuites (boolean) allows or disallows certain cipher suites.
    • Error Tracking: CountError is a function hook for tracking HTTP/2 error types.
    • Ping/Idle Management: PingTimeout controls how long the server waits for a PING response before closing the connection.
  9. Initialize a WebSocket configuration with NewConfig

    master
    Use NewConfig to create a new *Config object for a WebSocket client connection. You must provide the server URL and the origin URL. The returned configuration defaults to ProtocolVersionHybi13.