okami.mom/ultraviolet

git clone https://git.nkp.pet/okami.mom/ultraviolet

Ayman Bagabasfeat: add new poll reader for cross-platform I/O polling (#71)04a6149

main
1.3 KiB38 linesraw
1package uv
2
3import (
4	"fmt"
5	"time"
6)
7
8// pollReader reads data from an [io.Reader] using different native poll APIs
9// depending on the operating system.
10//
11// On Linux, it uses the epoll API.
12// On Windows, it uses The Windows I/O and Console APIs.
13// On macOS and other BSD-based systems, it will try to use the kqueue API and
14// fall back to Unix select if kqueue is not available (e.g., on TTY).
15// On other Unix-like systems, it uses the select API.
16// On all other systems, it falls back to a simple read loop with a timeout.
17type pollReader interface {
18	// Read reads data from the underlying [io.Reader]. It blocks until data is
19	// available or an error occurs.
20	//
21	// Use [pollReader] to check for data availability before calling Read to
22	// avoid blocking.
23	Read(p []byte) (n int, err error)
24
25	// Poll notifies when data is available to read with the given timeout. Use
26	// a negative duration to wait indefinitely.
27	Poll(timeout time.Duration) (ready bool, err error)
28
29	// Cancel cancels any ongoing poll or read operations. It returns true if
30	// an operation was canceled, false otherwise.
31	Cancel() bool
32
33	// Close closes the reader and releases any resources associated with it.
34	Close() error
35}
36
37// ErrCanceled is returned when a poll or read operation is canceled.
38var ErrCanceled = fmt.Errorf("poll canceled")