Documentation
¶
Index ¶
- Variables
- func NewPipe(opts ...Option) (reader io.Reader, writer io.Writer)
- func NewReadWriter(r io.Reader, w io.Writer, opts ...Option) io.ReadWriter
- func NewReader(r io.Reader, opts ...Option) io.Reader
- func NewWriter(w io.Writer, opts ...Option) io.Writer
- type Forwarder
- type Option
- func WithBlock() Option
- func WithByteOrder(order binary.ByteOrder) Option
- func WithNonblock() Option
- func WithProtocol(proto Protocol) Option
- func WithReadByteOrder(order binary.ByteOrder) Option
- func WithReadLimit(limit int) Option
- func WithReadLocal() Option
- func WithReadProtocol(proto Protocol) Option
- func WithReadSCTP() Option
- func WithReadTCP() Option
- func WithReadUDP() Option
- func WithReadUnix() Option
- func WithReadUnixPacket() Option
- func WithReadWebSocket() Option
- func WithRetryDelay(d time.Duration) Option
- func WithWriteByteOrder(order binary.ByteOrder) Option
- func WithWriteLocal() Option
- func WithWriteProtocol(proto Protocol) Option
- func WithWriteSCTP() Option
- func WithWriteTCP() Option
- func WithWriteUDP() Option
- func WithWriteUnix() Option
- func WithWriteUnixPacket() Option
- func WithWriteWebSocket() Option
- type Options
- type Protocol
- type ReadWriter
- type Reader
- type Writer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidArgument reports an invalid configuration or nil reader/writer. ErrInvalidArgument = errors.New("framer: invalid argument") // ErrTooLong reports that a frame length exceeds limits or the supported wire format. ErrTooLong = errors.New("framer: message too long") )
var ( // ErrWouldBlock means “no further progress without waiting”. // // It is an expected, non-failure control-flow signal for non-blocking I/O. // Any returned byte count (n) still represents real progress. // // Caller action: stop the current attempt and retry later (after readiness/event), // or configure RetryDelay to emulate cooperative blocking on top of a non-blocking transport. ErrWouldBlock = iox.ErrWouldBlock // ErrMore means “this completion is usable and more completions will follow”. // // It is not io.EOF and not “try later”. The operation remains active and additional // data/results are expected from the same ongoing operation. // // Caller action: process the returned bytes/result, then call again to obtain the next chunk. ErrMore = iox.ErrMore )
These are provided as package-level aliases so callers can reference the semantic control-flow errors without importing iox directly.
Functions ¶
func NewReadWriter ¶
NewReadWriter returns an io.ReadWriter that reads and writes framed messages.
Types ¶
type Forwarder ¶
type Forwarder struct {
// contains filtered or unexported fields
}
Forwarder relays framed messages from a source to a destination while preserving message boundaries.
Semantics (BinaryStream):
- One call to ForwardOnce processes at most one logical message.
- Two-phase state machine per message: 1) Read a whole framed message payload from src into an internal buffer (non-blocking; may return early with partial progress and ErrWouldBlock or ErrMore). 2) Write that same payload as exactly one framed message to dst (non-blocking; may return early with partial progress and ErrWouldBlock or ErrMore).
- Returns (n, nil) when a whole message payload has been forwarded to dst.
- Returns (n>0, ErrWouldBlock|ErrMore) when progress happened in the current phase (read or write) but the forwarding of this message is incomplete.
- Message boundaries are preserved: the destination sees exactly the same payload bytes as the source, encoded as one framed message on stream transports.
Semantics (SeqPacket/Datagram):
- Treats one packet as one message unit per call. Reads one packet from src and writes one packet to dst.
- Returns values and non-blocking semantics as above.
Limits and buffer sizing:
- The internal payload buffer is allocated during construction based on read-side limit (WithReadLimit). If ReadLimit is zero, a conservative default (64KiB) is used. There are no heap allocations in the steady-state forwarding path.
- If the current message exceeds the internal buffer capacity, ForwardOnce returns io.ErrShortBuffer. Callers can construct a new Forwarder with a larger ReadLimit to accommodate larger messages.
- If the current message exceeds the configured ReadLimit, ForwardOnce returns ErrTooLong.
Retry rule:
- On ErrWouldBlock or ErrMore, the caller must retry ForwardOnce on the SAME Forwarder instance to complete the in-flight message. Do not reuse a different instance because the in-flight state (read/write progress) is maintained internally.
func NewForwarder ¶
NewForwarder constructs a Forwarder that relays messages from src to dst. Options apply per direction (read/write) following the same rules as Reader/Writer.
func (*Forwarder) ForwardOnce ¶
ForwardOnce forwards at most one message. See Forwarder docs for semantics.
Return value n reflects progress in the current phase:
- During the read phase, n is the number of payload bytes read into the internal buffer in this call.
- During the write phase, n is the number of payload bytes written to dst in this call.
type Option ¶
type Option func(*Options)
func WithBlock ¶
func WithBlock() Option
WithBlock enables cooperative blocking (yield-and-retry) on iox.ErrWouldBlock.
func WithByteOrder ¶
func WithNonblock ¶
func WithNonblock() Option
WithNonblock forces non-blocking behavior (return iox.ErrWouldBlock immediately).
func WithProtocol ¶
func WithReadByteOrder ¶
func WithReadLimit ¶
func WithReadLocal ¶
func WithReadLocal() Option
WithReadLocal configures the reader side for local (stream) transports: BinaryStream, native byte order.
func WithReadProtocol ¶
func WithReadSCTP ¶
func WithReadSCTP() Option
WithReadSCTP configures the reader side for SCTP: SeqPacket (boundaries preserved), BigEndian.
func WithReadTCP ¶
func WithReadTCP() Option
WithReadTCP configures the reader side for TCP: BinaryStream with BigEndian length prefix.
func WithReadUDP ¶
func WithReadUDP() Option
WithReadUDP configures the reader side for UDP: Datagram (pass-through), BigEndian default.
func WithReadUnix ¶
func WithReadUnix() Option
WithReadUnix configures the reader side for Unix stream sockets: BinaryStream, BigEndian.
func WithReadUnixPacket ¶
func WithReadUnixPacket() Option
WithReadUnixPacket configures the reader side for Unix datagram sockets: Datagram (pass-through), BigEndian.
func WithReadWebSocket ¶
func WithReadWebSocket() Option
WithReadWebSocket configures the reader side for WebSocket: SeqPacket (boundaries preserved), BigEndian.
func WithRetryDelay ¶
WithRetryDelay sets the retry/wait policy used when the underlying transport returns iox.ErrWouldBlock.
func WithWriteByteOrder ¶
func WithWriteLocal ¶
func WithWriteLocal() Option
WithWriteLocal configures the writer side for local (stream) transports: BinaryStream, native byte order.
func WithWriteProtocol ¶
func WithWriteSCTP ¶
func WithWriteSCTP() Option
WithWriteSCTP configures the writer side for SCTP: SeqPacket (boundaries preserved), BigEndian.
func WithWriteTCP ¶
func WithWriteTCP() Option
WithWriteTCP configures the writer side for TCP: BinaryStream with BigEndian length prefix.
func WithWriteUDP ¶
func WithWriteUDP() Option
WithWriteUDP configures the writer side for UDP: Datagram (pass-through), BigEndian default.
func WithWriteUnix ¶
func WithWriteUnix() Option
WithWriteUnix configures the writer side for Unix stream sockets: BinaryStream, BigEndian.
func WithWriteUnixPacket ¶
func WithWriteUnixPacket() Option
WithWriteUnixPacket configures the writer side for Unix datagram sockets: Datagram (pass-through), BigEndian.
func WithWriteWebSocket ¶
func WithWriteWebSocket() Option
WithWriteWebSocket configures the writer side for WebSocket: SeqPacket (boundaries preserved), BigEndian.
type Options ¶
type Options struct {
ReadByteOrder binary.ByteOrder
WriteByteOrder binary.ByteOrder
ReadProto Protocol
WriteProto Protocol
// ReadLimit caps the maximum allowed payload size (bytes). Zero means no limit.
ReadLimit int
// RetryDelay controls how the framer handles iox.ErrWouldBlock from the underlying transport:
// - negative: nonblock, return ErrWouldBlock immediately
// - zero: yield (runtime.Gosched) and retry
// - positive: sleep for the duration and retry
RetryDelay time.Duration
}
Options configures framing behavior.
type Protocol ¶
type Protocol uint8
Protocol describes the expected message-boundary behavior of the underlying transport.
The framer logic adapts its algorithm based on this setting:
- BinaryStream: boundaries are not preserved (e.g., TCP). Framer adds a length prefix.
- SeqPacket / Datagram: boundaries are preserved. Framer is pass-through.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads framed messages.
func (*Reader) WriteTo ¶
WriteTo implements io.WriterTo.
Semantics:
- Stream (BinaryStream): transfers one framed message payload at a time from the underlying reader into dst. The payload bytes are written as-is; this method does not attempt to preserve or reconstruct framer wire format on the destination unless dst is itself a framer.Writer. It uses an internal reusable scratch buffer sized by the Reader's ReadLimit; when ReadLimit is zero, a conservative default cap is used (64KiB) and messages exceeding this cap result in ErrTooLong.
- Packet (SeqPacket/Datagram): pass-through, reads bytes and writes them to dst.
Non-blocking semantics: if the underlying reader or writer returns iox.ErrWouldBlock or iox.ErrMore, WriteTo returns immediately with the progress count (bytes written) and the same semantic error. Short writes on dst are handled per io.Writer contract.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer writes framed messages.
func (*Writer) ReadFrom ¶
ReadFrom implements io.ReaderFrom.
Semantics:
- Chunk-to-message: each chunk read from src (a successful src.Read call) is encoded as a single framed message and written via w.Write. This is efficient but does not preserve upstream application message boundaries. For protocols that already preserve boundaries (SeqPacket/Datagram), this is effectively pass-through.
Non-blocking semantics: if src.Read or the underlying writer returns iox.ErrWouldBlock or iox.ErrMore, ReadFrom returns immediately with the progress count and the same error. No heap allocations in the steady-state path.