diff --git a/examples/http-linux/conn-linux.go b/examples/http-linux/conn-linux.go
index 9da6143..90a2008 100644
--- a/examples/http-linux/conn-linux.go
+++ b/examples/http-linux/conn-linux.go
@@ -5,6 +5,7 @@ package main
import (
"net/netip"
"syscall"
+ "time"
)
// Conn wraps an accepted TCP connection from a raw Linux socket file descriptor.
@@ -47,6 +48,25 @@ func (c *Conn) Close() error {
return syscall.Close(c.fd)
}
+// SetReadTimeout limits how long a single Read waits for data before failing
+// with [syscall.EAGAIN]. Zero blocks indefinitely. This is what stops a peer
+// that opens a connection and then stalls from holding a server worker: the
+// connection, not the HTTP handler, owns the idle policy.
+func (c *Conn) SetReadTimeout(timeout time.Duration) error {
+ return setSockTimeout(c.fd, syscall.SO_RCVTIMEO, timeout)
+}
+
+// SetWriteTimeout limits how long a single Write waits for the send buffer to
+// drain before failing with [syscall.EAGAIN]. Zero blocks indefinitely.
+func (c *Conn) SetWriteTimeout(timeout time.Duration) error {
+ return setSockTimeout(c.fd, syscall.SO_SNDTIMEO, timeout)
+}
+
+func setSockTimeout(fd, option int, timeout time.Duration) error {
+ tv := syscall.NsecToTimeval(int64(timeout))
+ return syscall.SetsockoptTimeval(fd, syscall.SOL_SOCKET, option, &tv)
+}
+
// RemoteAddr returns the peer address of the connection.
func (c *Conn) RemoteAddr() netip.AddrPort { return c.remote }
diff --git a/examples/http-linux/main-httplinux.go b/examples/http-linux/main-httplinux.go
index b243fea..07e6df6 100644
--- a/examples/http-linux/main-httplinux.go
+++ b/examples/http-linux/main-httplinux.go
@@ -3,15 +3,22 @@
package main
import (
+ "log/slog"
"os"
"strconv"
"sync/atomic"
"time"
- "github.com/soypat/lneto/http/httpraw"
+ "github.com/soypat/lneto/http/httphi"
)
-const listenPort = 8080
+const (
+ kB = 1 << 10
+ listenPort = 8080
+ bufferSizes = 2 * kB
+ numGoroutines = 4
+ readTimeout = 2 * time.Second
+)
func main() {
if err := run(); err != nil {
@@ -27,18 +34,44 @@ func run() error {
return err
}
defer ln.Close()
- println("listening on port", listenPort)
- conn := new(Conn)
+ print("listening on http://localhost:", listenPort, "\n")
+
+ var mux httphi.MuxSlice
+ mux.Handle("GET /", homepage)
+
+ var router httphi.Router
+ err = router.Configure(httphi.RouterConfig{
+ FixedNumGoroutines: numGoroutines,
+ RequestBufferSize: bufferSizes,
+ ResponseMinBufferSize: bufferSizes,
+ MaxAwaitingConns: 256,
+ Backoff: func(consecutiveBackoffs uint) (sleepOrFlag time.Duration) {
+ return min(time.Second, time.Millisecond*time.Duration(consecutiveBackoffs))
+ },
+ Mux: &mux,
+ Logger: slog.Default(),
+ })
+ if err != nil {
+ return err
+ }
+ defer router.TeardownGoroutines()
+
for {
+ conn := new(Conn)
err := ln.Accept(conn)
if err != nil {
return err
}
- visits.Add(1)
- if err := handle(conn); err != nil {
- println("handle:", conn.RemoteAddr().String(), err.Error())
+ // 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.SetReadTimeout(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()
}
- conn.Close()
}
}
@@ -50,58 +83,22 @@ const (
htmlTail = `!` +
`Sign my guestbook!` +
`