//go:build !tinygo && linux package main import ( "log/slog" "net" "os" "strconv" "sync/atomic" "time" "github.com/soypat/lneto/http/httphi" ) const ( kB = 1 << 10 listenPort = 8080 connMemoryUse = 4 * kB numGoroutines = 4 readTimeout = 2 * time.Second ) func main() { if err := run(); err != nil { println("Error:", err.Error()) os.Exit(1) } println("DONE") } func run() error { ln, err := net.Listen("tcp", ":"+strconv.Itoa(listenPort)) if err != nil { return err } defer ln.Close() print("listening on http://localhost:", listenPort, "\n") var server Server server.Handle("GET /", server.homepage) var router httphi.Router cfg := httphi.DefaultRouterConfig(numGoroutines, connMemoryUse, server.mux.MaxPathValues()) err = router.Configure(&server.mux, cfg) if err != nil { return err } defer router.Shutdown() for { conn, err := ln.Accept() if err != nil { return err } // The connection owns the idle policy: a peer that opens a socket and // then stalls fails its read instead of holding a router goroutine. conn.SetReadDeadline(time.Now().Add(readTimeout)) err = router.Handle(conn) if err != nil { // Every goroutine is busy and the queue is full. Dropping the // connection is the backpressure: memory stays bounded. slog.Warn("dropped connection", slog.String("remote", conn.RemoteAddr().String()), slog.String("err", err.Error())) conn.Close() } } } const ( htmlHead = `