blockio

package
v1.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package blockio implements symmetric bounded blocking Reader and Writer endpoints backed by non-blocking connection turns.

Index

Constants

View Source
const DefaultWindow = 64 << 10

DefaultWindow is the default per-direction blocking buffer limit (64 KiB).

Variables

View Source
var ErrBlockingOnTurn = errors.New("uio: blocking I/O cannot run on a connection turn")

ErrBlockingOnTurn reports an attempt to invoke a blocking operation from a connection turn or event-loop goroutine.

View Source
var ErrOverflow = errors.New("outbound buffer overflow")

ErrOverflow matches the underlying transport's outbound overflow sentinel.

Functions

func EnterTurn

func EnterTurn()

EnterTurn marks the calling goroutine as executing event-loop or connection-turn work. Nested EnterTurn/LeaveTurn pairs on the same goroutine are reference-counted.

func LeaveTurn

func LeaveTurn()

LeaveTurn unmarks one EnterTurn call on the calling goroutine.

func OnTurn

func OnTurn() bool

OnTurn reports whether the calling goroutine is registered as a turn or event-loop goroutine.

Types

type Access

type Access[T Getter] struct{}

Access embeds a zero-sized capability marker on supported connections.

type Getter

type Getter interface {
	GetBlockState(source any) (*State, bool)
}

Getter resolves the State of a concrete connection implementation.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader is a stream connection's pull-mode blocking inbound endpoint.

func (*Reader) Buffered

func (r *Reader) Buffered() int

Buffered reports unread bytes currently held in the blocking pipe.

func (*Reader) Pump

func (r *Reader) Pump()

Pump runs on the connection's turn in place of Handler.OnRead, moving inbound bytes directly into a parked reader's slice or into the bounded pipe.

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

Read reads up to len(p) bytes from the connection, blocking until data arrives, the peer ends the stream, the connection closes, or the read deadline expires.

func (*Reader) SetReadDeadline

func (r *Reader) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for blocked Read and WriteTo calls without closing the underlying connection on expiry.

func (*Reader) WriteTo

func (r *Reader) WriteTo(w io.Writer) (int64, error)

WriteTo writes inbound stream data to w until EOF or error. When w is a *Writer (or unwraps to one via InternalWriter), pooled blocks transfer with zero intermediate copying.

type Source

type Source interface {
	PeekChunk() []byte
	Discard(n int) (int, error)
	InboundBuffered() int
	ReadEnded() bool
	WriteEnded() bool
	ClosingOrClosed() bool

	Write(p []byte) (int, error)
	Writev(vec [][]byte) (int, error)
	WriteOwned(buf *bytebuf.Buffer) (int, error)
	Flush() error
	OutboundBuffered() int

	MaxInbound() int
	MaxOutbound() int
	EnablePullRead() (onConnTurn bool, err error)
	Wake() error
	NewReadPause() *readcontrol.Pause
	WatchWrite(fn func(int)) bool
	IsOverflow(err error) bool
}

Source abstracts the non-blocking stream connection operations needed by Reader and Writer without importing root package uio.

type State

type State struct {
	// contains filtered or unexported fields
}

State lazily holds a stream connection's Reader and Writer instances.

func NewState

func NewState(src Source) *State

NewState creates a new blocking I/O attachment state for src.

func StateOf

func StateOf(source any) (*State, bool)

StateOf resolves source's blocking I/O State, or (nil, false) if unsupported (such as datagram connections).

func (*State) ActiveReader

func (s *State) ActiveReader() *Reader

ActiveReader returns the attached Reader if pull mode is active, or nil.

func (*State) HasWriter added in v1.9.1

func (s *State) HasWriter() bool

HasWriter reports whether a blocking Writer has been attached to s.

func (*State) OnClose

func (s *State) OnClose(localClose bool, err error)

OnClose notifies any attached Reader and Writer that the underlying connection closed. localClose is true when closed locally via Conn.Close().

func (*State) Reader

func (s *State) Reader(window int) (*Reader, error)

Reader returns the connection's single blocking Reader, creating it and switching inbound delivery to pull mode on the first call.

func (*State) Writer

func (s *State) Writer(window int) *Writer

Writer returns the connection's single blocking Writer, creating it and subscribing to write-progress notifications on the first call.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer is a stream connection's blocking outbound endpoint.

func (*Writer) Buffered

func (w *Writer) Buffered() int

Buffered reports accepted outbound bytes not yet flushed to the wire.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush asks the underlying transport to flush queued outbound data and waits until the outbound buffer drains, the connection closes, or the write deadline expires.

func (*Writer) FlushContext

func (w *Writer) FlushContext(ctx context.Context) error

FlushContext asks the underlying transport to flush queued outbound data and waits until the outbound buffer drains, the connection closes, the write deadline expires, or ctx ends.

func (*Writer) NotifySpace

func (w *Writer) NotifySpace()

NotifySpace wakes any writer waiting for outbound queue or flow-control room.

func (*Writer) ReadFrom

func (w *Writer) ReadFrom(r io.Reader) (int64, error)

ReadFrom copies from r into the connection using pooled zero-copy WriteOwned buffers. When r implements io.WriterTo (such as *Reader), it delegates to WriteTo so pooled pipe blocks transfer directly without an intermediate copy.

func (*Writer) SetWriteDeadline

func (w *Writer) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for blocked Write and ReadFrom calls without closing the underlying connection on expiry.

func (*Writer) TryWriteOwned added in v1.9.1

func (w *Writer) TryWriteOwned(buf *bytebuf.Buffer) (int, error)

TryWriteOwned attempts a fast non-blocking write when no writer is currently contended or parked on w and no deadline has expired. If the outbound window is full or contended, it returns ErrOverflow without releasing buf so the caller can fall back to WriteOwnedContext.

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Write writes all of p to the connection, suspending when the outbound window is full. Concurrent calls on the same Writer are serialized atomically.

func (*Writer) WriteContext

func (w *Writer) WriteContext(ctx context.Context, p []byte) (int, error)

WriteContext writes all of p to the connection, suspending until space is available, the connection closes, the write deadline expires, or ctx ends.

func (*Writer) WriteOwned

func (w *Writer) WriteOwned(buf *bytebuf.Buffer) (int, error)

WriteOwned transfers ownership of buf to the connection, suspending while the outbound window has no room, and always releases buf on failure.

func (*Writer) WriteOwnedContext

func (w *Writer) WriteOwnedContext(ctx context.Context, buf *bytebuf.Buffer) (int, error)

WriteOwnedContext transfers ownership of buf to the connection, suspending while the outbound window has no room until space drains, the connection closes, the write deadline expires, or ctx ends. Always consumes or releases buf.

func (*Writer) Writev

func (w *Writer) Writev(vec [][]byte) (int, error)

Writev writes all segments of vec atomically among blocking writers, suspending when the outbound window is full.

func (*Writer) WritevContext

func (w *Writer) WritevContext(ctx context.Context, vec [][]byte) (int, error)

WritevContext writes all segments of vec atomically among blocking writers, suspending until space is available, the connection closes, the write deadline expires, or ctx ends.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL