Documentation
¶
Overview ¶
Package transport provides context-aware TCP dial and listen primitives for USB/IP sessions. It wraps `net.Dialer` / `net.ListenConfig`, enables `TCP_NODELAY` on established connections, and surfaces errors with structured context so upstream layers can log or classify them.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NetTransport ¶
type NetTransport struct {
// contains filtered or unexported fields
}
NetTransport is the pure-Go implementation of the app.Transport surface declared in architecture-layering OpenSpec. It wraps net.Dialer / net.ListenConfig so that every dial and listen observes a caller-supplied context, and flips TCP_NODELAY on dialed connections to minimise USB/IP handshake latency.
Zero-value NetTransport{} is NOT valid — use New to obtain one with the default dialer and a no-op logger installed. NetTransport is safe for concurrent use; all mutating configuration is applied at construction time via Option values.
func New ¶
func New(opts ...Option) *NetTransport
New constructs a NetTransport ready to dial or listen. A fresh net.Dialer is allocated per instance so option-driven future knobs (timeout, keepalive) don't leak between callers.
func (*NetTransport) Dial ¶
func (t *NetTransport) Dial( ctx context.Context, r domain.RemoteEndpoint, opts netopts.TransportOptions, ) (net.Conn, error)
Dial connects to r over TCP using ctx for cancellation. Port 0 in r is normalised to domain.DefaultPort per domain-model and transport-networking OpenSpec documents — the net.Dialer would otherwise try to connect to port 0 which is a kernel-reserved sentinel. TCP_NODELAY is set on the returned connection so the USB/IP handshake's small frames are not Nagle-delayed.
Non-zero opts fields are honored on the dialed conn:
- DialConnectTimeout caps the connect phase via a per-call net.Dialer copy (the embedded dialer is left untouched so concurrent Dials do not race on the timeout field).
- SendBufferBytes / ReceiveBufferBytes call SetWriteBuffer / SetReadBuffer; Linux doubles the requested value internally.
- TCPKeepAlive{Idle,Interval,Probes} call SetKeepAliveConfig (Go ≥ 1.23) and enable SO_KEEPALIVE.
- ReadDeadline / WriteDeadline call SetReadDeadline / SetWriteDeadline; the deadlines are absolute timestamps measured from the moment of the Dial call.
Buffer / keepalive / deadline failures are logged at warn unless the conn is already dead (see isSockoptFatal); a perf knob falling over must not turn a usable conn into an availability outage.
func (*NetTransport) Listen ¶
func (t *NetTransport) Listen( ctx context.Context, addr string, opts netopts.TransportOptions, ) (net.Listener, error)
Listen binds addr and returns a ctx-bound net.Listener. The listener is closed automatically when ctx is cancelled, so graceful-shutdown call sites in cli-interface OpenSpec can drive a daemon teardown by cancelling one root context without having to track the listener separately. The returned Listener's own Close is idempotent and waits for the watcher goroutine to exit, so callers cannot leak it.
When opts carries non-zero tuning fields, accepted server-side connections are tuned by tuneTCPConn before they are returned from Accept. A failure to apply a tuning knob does NOT propagate as an Accept error: the conn is returned to the caller with a logged warning, mirroring Dial's "perf knob failure must not become an availability outage" policy. Fatal errnos (conn already dead) close the conn and surface as Accept errors.
type Option ¶
type Option func(*NetTransport)
Option configures a NetTransport at construction time.
func WithLogger ¶
WithLogger installs l as the NetTransport's logger. Passing nil selects a discarding handler so call sites never have to nil-check. This mirrors the Codec option pattern in internal/adapter/wire so both adapters share a single logging convention (operations-observability OpenSpec).