Documentation
¶
Overview ¶
Package io provides some handy network I/O functions.
Index ¶
- func ReadNB(fd int, p []byte) (n int, err error)
- func ReadSocketNB(fd int, p []byte) (n int, err error)
- func Readv(fd int, iov [][]byte) (int, error)
- func WriteNB(fd int, p []byte) (n int, err error)
- func WriteSocketNB(fd int, p []byte) (n int, err error)
- func Writev(fd int, iov [][]byte) (int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReadNB ¶
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 ¶
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 WriteNB ¶
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 ¶
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.
Types ¶
This section is empty.