Documentation
¶
Index ¶
- Variables
- func Connect(ctx context.Context, a ContextIO, b ContextIO) error
- func ConnectAndClose(ctx context.Context, a io.ReadWriteCloser, b io.ReadWriteCloser) error
- type ContextIO
- func WrapClosableReadWriter(rwc io.ReadWriteCloser, name string) (ContextIO, error)
- func WrapClosableReader(rc io.ReadCloser, name string) (ContextIO, error)
- func WrapClosableWriter(wc io.WriteCloser, name string) (ContextIO, error)
- func WrapConn(conn net.Conn) (ContextIO, error)
- func WrapDeadliner(d Deadliner, name string) (ContextIO, error)
- func WrapFd(fd uintptr, name string) (ContextIO, error)
- func WrapFile(file *os.File) (ContextIO, error)
- type Deadliner
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCanceled is returned when an I/O operation was canceled. ErrCanceled = fmt.Errorf("I/O operation canceled") // ErrNotSupported is returned when wrapping an object that is not supported // by ctxio. ErrNotSupported = fmt.Errorf("not supported") )
Functions ¶
func Connect ¶
Connect copies data between a and b in both directions until an error occurs or ctx is canceled. It uses the ContextIO cancellation mechanism so that no data is consumed from either side when the context expires. If both copy directions fail, Connect returns the first error.
func ConnectAndClose ¶
func ConnectAndClose(ctx context.Context, a io.ReadWriteCloser, b io.ReadWriteCloser) error
ConnectAndClose is a convenience wrapper around Connect that accepts plain io.ReadWriteCloser values and closes both sides when it returns.
Types ¶
type ContextIO ¶
type ContextIO interface {
// ReadWriteCloser is the interface for interfacing with APIs that are not
// context aware. In this case, cancelation can be achieved explicitly using
// the Cancel* and Reset* methods. It is recommended not to mix IO using the
// Read()/Write() API and the explicit cancelation methods with the
// context-aware IO methods.
io.ReadWriteCloser
// ReadContext is like Read but it returns ErrCanceled as soon as the
// context expires. Calling it with a fresh context resets the ContextIO
// reader. It is recommended not to mix IO using the Read()/Write() API and
// the explicit cancelation methods with the context-aware IO methods.
ReadContext(ctx context.Context, buffer []byte) (int, error)
// WriteContext is like Write but it returns ErrCanceled as soon as the
// context expires. Calling it with a fresh context resets the ContextIO
// writer. It is recommended not to mix IO using the Read()/Write() API and
// the explicit cancelation methods with the context-aware IO methods.
WriteContext(ctx context.Context, buffer []byte) (int, error)
// Name returns the name of object wrapped by the ContextIO.
Name() string
// Cancel cancels any ongoing and future Read and Write calls. All
// subsequent calls to Read/Write will return ErrCanceled until Reset is
// called. ReadContext/WriteContext are not affected: they clear the
// canceled state automatically before starting a new operation.
// It is equivalent to calling both CancelReads and CancelWrites.
Cancel()
// CancelReads cancels any ongoing and future Read calls. All subsequent
// calls to Read will return ErrCanceled until ResetReader is called.
// ReadContext is not affected as it clears the canceled state automatically
// before starting a new operation when a fresh context is provided.
CancelReads()
// CancelWrites cancels any ongoing and future Write calls. All subsequent
// calls to Write will return ErrCanceled until ResetWriter is called.
// WriteContext is not affected as it clears the canceled state
// automatically before starting a new operation when a fresh context is
// provided.
CancelWrites()
// ResetReader clears the read cancellation state, allowing subsequent Reads
// to proceed. Must not be called while a Read is in progress.
ResetReader()
// ResetWriter clears the write cancellation state, allowing subsequent
// Writes to proceed. Must not be called while a Write is in progress.
ResetWriter()
// Reset clears both read and write cancellation state. Reset must not be
// called while a Read or Write is in progress.
Reset()
}
ContextIO provides context-aware, cancelable I/O operations for objects like files, sockets, pipes and more.
func WrapClosableReadWriter ¶
func WrapClosableReadWriter(rwc io.ReadWriteCloser, name string) (ContextIO, error)
WrapClosableReadWriter returns a ContextIO wrapping an io.ReadWriteCloser using close-based cancellation. Cancellation works by closing the underlying object, which means the object cannot be reused after cancellation. Reset clears the cancellation flag but does not reopen the object.
func WrapClosableReader ¶
func WrapClosableReader(rc io.ReadCloser, name string) (ContextIO, error)
WrapClosableReader returns a ContextIO wrapping an io.ReadCloser using close-based cancellation. Cancellation works by closing the underlying object, which means the object cannot be reused after cancellation. Reset clears the cancellation flag but does not reopen the object. Write and WriteContext always return ErrNotSupported.
func WrapClosableWriter ¶
func WrapClosableWriter(wc io.WriteCloser, name string) (ContextIO, error)
WrapClosableWriter returns a ContextIO wrapping an io.WriteCloser using close-based cancellation. Cancellation works by closing the underlying object, which means the object cannot be reused after cancellation. Reset clears the cancellation flag but does not reopen the object. Read and ReadContext always return ErrNotSupported.
func WrapConn ¶
WrapConn returns a ContextIO interface wrapping the given net.Conn with context-aware and explicitly cancelable I/O based on the epoll mechanism, falling back to deadline-based cancelation if epoll is not supported for the net.Conn.
func WrapDeadliner ¶
WrapDeadliner returns a ContextIO wrapping any value that implements the Deadliner interface. This is useful for types that are not an *os.File or net.Conn but still support deadline-based I/O (e.g. TLS connections, QUIC streams). Returns an error if the value does not actually support deadlines at runtime.
func WrapFd ¶
WrapFd returns a ContextIO interface wrapping the given file descriptor with context-aware and explicitly cancelable I/O based on the epoll mechanism, falling back to deadline-based cancelation if epoll is not supported for the file descriptor.
func WrapFile ¶
WrapFile returns a ContextIO interface wrapping the given *os.File with context-aware and explicitly cancelable I/O. The linux implementation is based on the epoll mechanism, falling back to deadline-based cancelation for files that support it but cannot be used with epoll (e.g. some device files).