CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/golang-golang-org-x--net

Supplementary Go networking libraries providing additional networking protocols and utilities

Overview
Eval results
Files

netutil.mddocs/

Network Utility Functions

Package netutil provides network utility functions, complementing the more common ones in the net package.

Import

import "golang.org/x/net/netutil"

Functions

// LimitListener returns a Listener that accepts at most n simultaneous connections
func LimitListener(l net.Listener, n int) net.Listener

LimitListener

LimitListener returns a Listener that accepts at most n simultaneous connections from the provided Listener. This is useful for limiting resource usage in servers.

Usage Examples

Limiting Concurrent Connections

import (
    "fmt"
    "golang.org/x/net/netutil"
    "net"
    "net/http"
)

func startLimitedServer() error {
    // Create listener
    listener, err := net.Listen("tcp", ":8080")
    if err != nil {
        return err
    }

    // Limit to 100 simultaneous connections
    limitedListener := netutil.LimitListener(listener, 100)

    // Serve HTTP on limited listener
    fmt.Println("Server listening on :8080 (max 100 connections)")
    return http.Serve(limitedListener, http.DefaultServeMux)
}

Custom Server with Connection Limits

func createLimitedHTTPServer(maxConnections int) *http.Server {
    listener, err := net.Listen("tcp", ":8080")
    if err != nil {
        panic(err)
    }

    limitedListener := netutil.LimitListener(listener, maxConnections)

    server := &http.Server{
        Handler: http.DefaultServeMux,
    }

    go server.Serve(limitedListener)

    return server
}

// Usage:
// server := createLimitedHTTPServer(50)
// Server will accept at most 50 simultaneous connections

Preventing Resource Exhaustion

import (
    "golang.org/x/net/netutil"
    "log"
    "net"
)

func runProtectedServer(handler func(net.Conn)) error {
    listener, err := net.Listen("tcp", ":9000")
    if err != nil {
        return err
    }
    defer listener.Close()

    // Protect against too many connections
    limitedListener := netutil.LimitListener(listener, 25)

    log.Println("Server ready, max 25 connections")

    for {
        conn, err := limitedListener.Accept()
        if err != nil {
            log.Printf("Accept error: %v", err)
            continue
        }

        go handler(conn)
    }
}

Install with Tessl CLI

npx tessl i tessl/golang-golang-org-x--net

docs

bpf.md

context-ctxhttp.md

context.md

dict.md

dns-dnsmessage.md

html-atom.md

html-charset.md

html.md

http-httpguts.md

http-httpproxy.md

http2-h2c.md

http2-hpack.md

http2.md

icmp.md

idna.md

index.md

ipv4.md

ipv6.md

nettest.md

netutil.md

proxy.md

publicsuffix.md

quic-qlog.md

quic.md

trace.md

webdav.md

websocket.md

xsrftoken.md

tile.json