Skip to content

Phase 2: Server/Conn API, graceful shutdown, logging, CI - #24

Merged
y001j merged 7 commits into
masterfrom
feat/phase2-api
Nov 22, 2026
Merged

y001j merged 7 commits into
masterfrom
feat/phase2-api

Conversation

@y001j

@y001j y001j commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Roadmap phase 2 (items 4, 5, 8, 9, 10, 11 and 19 in docs/ROADMAP.md).

New API

type echo struct{ uringnet.BuiltinHandler }

func (echo) OnTraffic(c *uringnet.Conn, data []byte) uringnet.Action {
	c.Write(data) // in place: data lies in the receive buffer
	return uringnet.None
}

srv, err := uringnet.Listen("tcp", ":8080", echo{}, uringnet.WithIdleTimeout(time.Minute))
go func() { <-sig; srv.Shutdown(ctx) }()
srv.Serve() // ErrServerClosed
  • Options replace the positional NewMany arguments and URingNet fields (WithRings, WithBuffers, WithBusyPoll, WithZeroCopy, WithMaxWriteBuffer, WithIdleTimeout, WithLogger, ...).
  • Conn: Write (in place when the bytes lie in the received buffer, copied otherwise), WriteNoCopy, Close (after queued data is sent), Context, addresses, and AsyncWrite/AsyncClose from any goroutine.
  • Actions reduced to None, Close, Shutdown. OnClose gets the reason: nil, io.EOF, ErrIdleTimeout, ErrWriteTimeout, ErrServerClosed or the socket errno.
  • Graceful shutdown: Shutdown(ctx) stops accepting, lets connections send what was written, closes them; forced close and ctx.Err() when ctx ends.
  • Logging with *slog.Logger (accept failures, zero-copy/buffer fallbacks, timeouts); silent by default.
  • Startup checks: ErrKernelTooOld below 6.0; ErrIOUringUnavailable with a seccomp / kernel.io_uring_disabled hint; ENOMEM hints at RLIMIT_MEMLOCK.

The EventHandler/UserData API keeps working and is marked Deprecated. Internally both APIs drive the same connection state machine through an internal handler interface.

Fixes found along the way

  • Peer half-close: on EOF the connection was closed at once, dropping queued replies (printf req | nc -N got nothing). Now queued data is sent first.
  • Port 0 with SO_REUSEPORT and several rings: each ring bound its own random port; they now share the first one's.
  • Incremental buffers: in-place send references are counted per segment, so one callback can send several slices of a buffer in place.

Other

  • Examples and the bench server use the new API; README (EN/中文) Get Started rewritten, both examples compile.
  • CI: Go 1.21 + stable matrix, go test -race ./..., vet, staticcheck, coverage upload (the old codecov workflow uploaded nothing). The runner kernel is 7.0; core coverage 77%.
  • byteslice: reflect.SliceHeader → unsafe.Slice; pool-reuse assertion skipped under -race (the race detector drops sync.Pool items at random).
  • go.mod: go 1.21 (log/slog).

Testing

Lima VM (Linux 6.17) and CI (Linux 7.0): all tests pass under -race; the 8 new API tests pass 10 runs in a row. HTTP example re-verified with curl/ab/ab -k/pipelining. Bench server on the new API vs master's old-API server: 0.91–1.06x across 6 scenarios (noise).

🤖 Generated with Claude Code

y001j and others added 7 commits November 22, 2026 20:16
Prepares for a second, Conn-based API next to EventHandler/UserData:
the connection state machine now calls an internal connHandler, and the
existing API is one implementation of it (legacyHandler). In-place
sends also take their incremental-buffer reference in writeFrom, once
per held segment, instead of once per OnTraffic call, so a callback may
send several slices of the same buffer in place. No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Listen(network, address, handler, opts...) returns a Server; Serve
blocks until Shutdown(ctx) or Close and returns ErrServerClosed.
Options replace the positional NewMany arguments and URingNet fields
(WithRings, WithBuffers, WithBusyPoll, WithZeroCopy, WithIdleTimeout,
WithMaxWriteBuffer, WithLogger, ...).

Handler callbacks get a *Conn: Write (in place when the data lies in
the received buffer, copied otherwise), WriteNoCopy, Close (after
queued data is sent), Context, addresses, and AsyncWrite/AsyncClose,
which are safe from any goroutine. Actions are reduced to None, Close
and Shutdown. OnClose receives the reason: nil, io.EOF,
ErrIdleTimeout, ErrWriteTimeout, ErrServerClosed or the socket errno.

Shutdown(ctx) stops accepting, lets each connection send what was
already written, then closes it; connections still open when ctx ends
are closed at once and ctx.Err() is returned.

Also:
- *slog.Logger for recovered failures (accept errors, zero-copy and
  buffer-registration fallbacks) and timeouts; silent by default.
- Startup checks: ErrKernelTooOld below Linux 6.0, and
  ErrIOUringUnavailable with a hint when seccomp or
  kernel.io_uring_disabled blocks io_uring; ENOMEM hints at
  RLIMIT_MEMLOCK.
- Port 0 with SO_REUSEPORT and several rings: all rings now bind the
  port the first one got (each used to get its own random port).
- go 1.21 (log/slog).

The EventHandler/UserData API keeps working unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the peer shut down its write side (EOF) the connection was closed
at once, dropping replies still queued: a client that sends a request
and then half-closes never got the answer (e.g. printf req | nc -N).
On EOF, stop receiving, send what was written, then close; if the peer
is fully gone the send fails and the connection closes anyway.
OnClose still receives io.EOF.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both examples now use Listen/Serve with options and shut down
gracefully on SIGINT/SIGTERM. Verified the HTTP example again with
curl HTTP/1.0, ab, ab -k and pipelined requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
New, NewMany, SetLoops, Ringloop, EventHandler, BuiltinEventEngine,
UserData, NetAddress and the Echo/Read/Write/EchoAndClose actions are
marked Deprecated in favour of Listen, Server, Handler and Conn. They
keep working for now.

Adds WithBusyPollThresholds, used by the bench server, which now runs
on the new API. Throughput against master's old-API server is
unchanged (0.91-1.06x over 6 scenarios, server on 1 core).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Test matrix on the go.mod minimum (1.21) and the latest release,
  with go vet, go test -race over all packages, example builds and a
  vet of the bench module; the runner's kernel is printed.
- staticcheck as a separate job.
- Coverage is now generated and uploaded (codecov-action v4); the
  separate codecov workflow, which uploaded nothing, is removed.
- byteslice: replace the deprecated reflect.SliceHeader with
  unsafe.Slice, and skip the pool-reuse assertion under the race
  detector, which makes sync.Pool drop items at random.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Get Started sections now show Listen/Serve/Shutdown, the options,
and Handler/Conn; incremental-buffer and zero-copy snippets use the
new API. Both README examples compile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@y001j
y001j marked this pull request as ready for review November 22, 2026 11:30
@y001j
y001j merged commit e1fbcf9 into master Nov 22, 2026
4 checks passed
@y001j
y001j deleted the feat/phase2-api branch November 22, 2026 11:32
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant