io

package
v2.5.0 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: 3 Imported by: 0

Documentation

Overview

Package io provides some handy network I/O functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadNB

func ReadNB(fd int, p []byte) (n int, err error)

ReadNB is read(2) for a descriptor that is in non-blocking mode.

unix.Read goes through syscall.Syscall, which brackets every call with runtime.entersyscall/exitsyscall so that the scheduler can take the P away from a goroutine that blocks in the kernel. For a non-blocking descriptor that protection buys nothing: the call returns within microseconds either with data or with EAGAIN, and the bracket is a measurable share of the per-request cost (two atomic status transitions plus the P hand-off checks, on every read). ReadNB issues the same system call through RawSyscall instead.

It MUST only be used with descriptors that have O_NONBLOCK set (every socket that gnet accepts or dials does); on a blocking descriptor it would block the OS thread together with its P and stall the garbage collector.

Like unix.Read it returns n == -1 together with the error when the call fails.

func ReadSocketNB

func ReadSocketNB(fd int, p []byte) (n int, err error)

ReadSocketNB uses recvfrom(2) for non-blocking sockets. The NULL source address and address-length arguments make this equivalent to recv(2) for the stream sockets used by the event loop. This is the default Linux socket-read path; gnet_legacy_read restores the previous read(2) implementation.

It must only be used with non-blocking socket descriptors. Eventfd and other non-socket descriptors must continue to use ReadNB.

func Readv

func Readv(fd int, iov [][]byte) (int, error)

Readv calls readv() on Linux.

func WriteNB

func WriteNB(fd int, p []byte) (n int, err error)

WriteNB is write(2) for a descriptor that is in non-blocking mode, see ReadNB for the reasoning and for the requirement that the descriptor must be non-blocking.

Like unix.Write it returns n == -1 together with the error when the call fails.

func WriteSocketNB

func WriteSocketNB(fd int, p []byte) (n int, err error)

WriteSocketNB uses sendto(2) with MSG_NOSIGNAL for non-blocking sockets. A NULL destination and zero address length make this equivalent to send(2) on the already-connected sockets used by the event loop. MSG_NOSIGNAL suppresses SIGPIPE for this syscall while preserving EPIPE as the returned error. This is the default Linux socket-write path; gnet_legacy_write restores the previous write(2) implementation.

It must only be used with socket descriptors. Eventfd and other non-socket descriptors must continue to use WriteNB.

func Writev

func Writev(fd int, iov [][]byte) (int, error)

Writev calls writev() on Linux.

Types

This section is empty.

Jump to

Keyboard shortcuts

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