README
¶
RIO
基于IOURING
的AIO
网络库,非CGO
方式,且遵循标准库使用设计模式。
支持协议:TCP
、UDP
、UNIX
、UNIXGRAM
(IP
为代理标准库)。
RIO
是遵循标准库使用方式的,是可以非常方便的投入使用,所以它不是个玩具。
Linux 内核版本需要>= 5.19
,推荐版本为>= 6.6
。
特性
- 基于
IOURING
的实现 - 基于
net.Listener
net.Conn
和net.PacketConn
的实现 - 使用批处理方式来减少系统调用的开销
- 支持
TLS
- 支持
FIXED BUFFER
和FIXED FILE
- 支持
SEND_ZC
和SENDMSG_ZC
- 支持动态调整
WAIT_CQE_TIME_CURVE
控制不同场景的性能
性能



详细信息
使用 tcpkali
进行压力测试,基准测试代码地址 。
环境:
端 | 平台 | IP | OS | 规格 |
---|---|---|---|---|
客户端 | WSL2 | 192.168.100.1 | Ubuntu22.04 (6.6.36.6-microsoft-standard-WSL2) | 4C 16G |
服务端 | Hyper-V | 192.168.100.120 | Ubuntu24.10(6.11.0-8-generic) | 4C 8G |
C50 T10s
50链接10秒。
tcpkali --workers 1 -c 50 -T 10s -m "PING" 192.168.100.120:9000
结果:
种类 | 速率 (pps) | 说明 | 性能 |
---|---|---|---|
RIO | 23263.3 | 稳定在23000 | 100 % |
EVIO | 18855.5 | 稳定在18000 | 81 % |
GNET | 18284.7 | 稳定在18000 | 79 % |
NET | 14638.6 | 稳定在14000 | 63 % |
C50 R5k
50链接重复5000次。
tcpkali --workers 1 -c 50 -r 5k -m "PING" 192.168.100.120:9000
结果:
种类 | 速率 (pps) | 说明 | 性能 |
---|---|---|---|
RIO | 44031.1 | 稳定在44000 | 100 % |
EVIO | 27997.6 | 稳定在28000 | 64 % |
GNET | 28817.2 | 稳定在28000 | 65 % |
NET | 27874.5 | 稳定在28000 | 63 % |
使用
go get -u github.com/brickingsoft/rio
基本使用,将 github.com/brickingsoft/rio
替换 net
。
// 将 net.Listen() 替换成 rio.Listen()
ln, lnErr := rio.Listen("tcp", ":9000")
// 将 net.Dial() 替换成 rio.Dial()
conn, dialErr := rio.Dial("tcp", "127.0.0.1:9000")
进阶使用
TLS
使用内置security
方式。
// server("github.com/brickingsoft/rio/security")
ln, _ = security.Listen("tcp", ":9000", config)
// client("github.com/brickingsoft/rio/security")
conn, _ = security.Dial("tcp", "127.0.0.1:9000", config)
使用包裹方式。
// server(use crypto/tls wrap)
ln, _ := rio.Listen("tcp", ":9000")
ln, _ := tls.NewListener(ln, config)
// client(use crypto/tls wrap)
rawConn, dialErr := rio.Dial("tcp", "127.0.0.1:9000")
conn := tls.Client(rawConn, config)
if err := conn.HandshakeContext(ctx); err != nil {
rawConn.Close()
return nil, err
}
类型转换
// 转换成 TCP 链接
tcpConn, ok := conn.(*rio.TCPConn)
// 转换成 UDP 链接
udpConn, ok := conn.(*rio.UDPConn)
// 转换成 UNIX 链接
unixConn, ok := conn.(*rio.UnixConn)
Config
rio.ListenConfig
与 net.ListenConfig
是类似的,通过配置来监听。
config := rio.ListenConfig{
Control: nil, // 设置控制器
KeepAlive: 0, // 设置 KeepAlive 时长
KeepAliveConfig: net.KeepAliveConfig{}, // 设置 KeepAlive 详细配置
MultipathTCP: false, // 是否多路TCP模式
FastOpen: false, // 是否快速打开(tcp自动开启)
QuickAck: false, // 是否快速应答(tcp自动开启)
ReusePort: false, // 是否重用端口(同时开启cBPF)
SendZC: false, // 是否使用 Zero-Copy 方式发送(某些场景会遥测不到但其实是发送了,如 TCPKALI)
MultishotAccept: false, // 是否单投多发模式来接受链接
AutoFixedFdInstall: false, // 是否启用接受到的链接进行自动安装描述符(超出后会退回到非注册模式)
Vortex: nil, // 自定义 iouring
}
ln, lnErr := config.Listen(context.Background(), "tcp", ":9000")
rio.Dialer
与 net.Dialer
是类似的,通过配置来拨号。
dialer := rio.Dialer{
Timeout: 0, // 超时
Deadline: time.Time{}, // 死期
KeepAlive: 0, // 设置 KeepAlive 时长
KeepAliveConfig: net.KeepAliveConfig{}, // 设置 KeepAlive 详细配置
LocalAddr: nil, // 本地地址
MultipathTCP: false, // 是否多路TCP模式
FastOpen: false, // 是否快速打开(tcp自动开启)
QuickAck: false, // 是否快速应答(tcp自动开启)
SendZC: false, // 是否使用 Zero-Copy 方式发送(某些场景会遥测不到但其实是发送了,如 TCPKALI)
AutoFixedFdInstall: false, // 是否启用接受到的链接进行自动安装描述符(超出后会退回到非注册模式)
Control: nil, // 设置控制器
ControlContext: nil, // 设置带上下文的控制器
Vortex: nil, // 自定义 iouring
}
conn, dialErr := dialer.DialContext(context.Background(), "tcp", "127.0.0.1:9000")
Fixed Buffer
使用注册的固定字节缓存进行读写,其效果是相比非注册模式会减少字节缓存在映射时上下文切换。
如需使用,请务必设置固定字节缓存在先,可以通过名为RIO_IOURING_REG_FIXED_BUFFERS
的环境变量或rio.Presets(aio.WithRegisterFixedBuffer(..))
方式来设置。
// convert
fixed, ok := rio.ConvertToFixedReaderWriter(conn)
// acquire buf
buf := fixed.AcquireRegisteredBuffer()
defer fixed.ReleaseRegisteredBuffer(buf)
// check buf
if buf == nil {
// not registered or no buf remain
// use normal read
}
// read
rn, rErr := buf.ReadFixed(buf)
// write
wn, wErr := fixed.WriteFixed(buf)
Fixed Fd
使用固定描述符进行 IO 操作,其效果是相比非注册模式会减少内核操作 FD 是的上下文切换。
如需使用,请务必设置固定描述符数组大小,可以通过名为RIO_IOURING_REG_FIXED_FILES
的环境变量或rio.Presets(aio.WithRegisterFixedFiles(..))
方式来设置。
自动模式见 Config,自动模式下当不够用时会不安装但不会返回错误。
手动安装如下:
// convert
fixed, ok := rio.ConvertToFixedFd(conn)
// install
err := fixed.InstallFixedFd()
参数设置
通过设置环境变量进行调控,具体详见 IOURING。
名称 | 值 | 说明 |
---|---|---|
RIO_IOURING_ENTRIES | 数字 | 环大小,默认为最大值 16384。 |
RIO_IOURING_SETUP_FLAGS | 文本 | 标识,如IORING_SETUP_SQPOLL, IORING_SETUP_SQ_AFF 等。 |
RIO_IOURING_SETUP_FLAGS_SCHEMA | 文本 | 标识方案,DEFAULT 或 PERFORMANCE 。 |
RIO_IOURING_SQ_THREAD_CPU | 数字 | 设置 SQ 环锁亲和的 CPU。 |
RIO_IOURING_SQ_THREAD_IDLE | 数字 | 在含有IORING_SETUP_SQPOLL 标识时,设置空闲时长,单位为毫秒,默认是 10 秒。 |
RIO_IOURING_REG_FIXED_BUFFERS | 文本 | 设置注册固定字节缓存,格式为 单个大小, 个数 , 如4096, 1024 。 |
RIO_IOURING_REG_FIXED_FILES | 数字 | 设置注册固定描述符,当大于软上限时,会使用软上线值。 默认是 4096。 |
RIO_PREP_SQE_BATCH_SIZE | 数字 | 准备 SQE 的缓冲大小,默认为 1024 的大小。 |
RIO_PREP_SQE_BATCH_TIME_WINDOW | 数字 | 准备 SQE 批处理时长,默认 500 纳秒。 |
RIO_PREP_SQE_BATCH_IDLE_TIME | 数字 | 准备 SQE 空闲时长,默认 15 秒。 |
RIO_PREP_SQE_BATCH_AFF_CPU | 数字 | 设置准备 SQE 线程所亲和的 CPU。 |
RIO_WAIT_CQE_BATCH_SIZE | 数字 | 获取 CQE 的批大小,默认为 1024 的大小。 |
RIO_WAIT_CQE_BATCH_AFF_CPU | 布尔 | 设置获取 CQE 线程所亲和的 CPU。 |
RIO_WAIT_CQE_BATCH_TIME_CURVE | 文本 | 设置等待 CQE 策略曲线,如 1:15s, 8:2us, 16:1ms 。 |
注意事项:
RIO_IOURING_SETUP_FLAGS
与系统内核版本有关联,请务必确认版本,但程序会自动过滤掉与版本不符的标识,但不解决标识冲突。IORING_SETUP_SQPOLL
取决于运行环境,非常吃配置,请自行选择配置进行调试。IORING_SETUP_SQ_AFF
激活时,且是容器环境,此时需要注意 CPU 的相关设置。IORING_SETUP_SINGLE_ISSUER
与AutoFixedFdInstall
冲突。
RIO_IOURING_SETUP_FLAGS_SCHEMA
优先级低于RIO_IOURING_SETUP_FLAGS
。DEFAULT
为IORING_SETUP_COOP_TASKRUN
PERFORMANCE
为IORING_SETUP_SQPOLL
和IORING_SETUP_SQ_AFF
,所以非常吃配置,但是会减少系统调用。
RIO_IOURING_REG_FIXED_BUFFERS
为rio.FixedReaderWriter
的前置必要条件,如果使用固定读写,必须设置该变量来注册。RIO_IOURING_REG_FIXED_FILES
为rio.FixedReaderWriter
、rio.FixedFd
和AutoFixedFdInstall
的前置必要条件,如果使用固定文件,必须设置该变量来注册。RIO_PREP_SQE_BATCH_IDLE_TIME
是用来处理忙等待问题,所以值会大一点。RIO_PREP_SQE_BATCH_TIME_WINDOW
是批处理窗口,过小和过大都会影响性能。RIO_WAIT_CQE_BATCH_TIME_CURVE
的第一个节点的时长建议大一些,太小会引发忙等待,后续节点会具体影响性能。
通过编程方式:
// 使用 Presets 函数进行预设参数
rio.Presets(aio.WithFlagsSchema(aio.DefaultFlagsSchema))
Documentation
¶
Index ¶
- Constants
- Variables
- func Dial(network string, address string) (net.Conn, error)
- func DialContext(ctx context.Context, network string, address string) (net.Conn, error)
- func DialContextTimeout(ctx context.Context, network string, address string, timeout time.Duration) (net.Conn, error)
- func DialTimeout(network string, address string, timeout time.Duration) (net.Conn, error)
- func Listen(network string, addr string) (ln net.Listener, err error)
- func ListenPacket(network string, addr string) (c net.PacketConn, err error)
- func Presets(options ...aio.Option)
- func UseProcessPriority(level process.PriorityLevel)
- type Dialer
- func (d *Dialer) Dial(network string, address string) (c net.Conn, err error)
- func (d *Dialer) DialContext(ctx context.Context, network, address string) (c net.Conn, err error)
- func (d *Dialer) DialIP(_ context.Context, network string, laddr, raddr *net.IPAddr) (*IPConn, error)
- func (d *Dialer) DialTCP(ctx context.Context, network string, laddr, raddr *net.TCPAddr) (*TCPConn, error)
- func (d *Dialer) DialUDP(ctx context.Context, network string, laddr, raddr *net.UDPAddr) (*UDPConn, error)
- func (d *Dialer) DialUnix(ctx context.Context, network string, laddr, raddr *net.UnixAddr) (*UnixConn, error)
- func (d *Dialer) SetAutoFixedFdInstall(auto bool)
- func (d *Dialer) SetFastOpen(use bool)
- func (d *Dialer) SetMultipathTCP(use bool)
- func (d *Dialer) SetQuickAck(use bool)
- func (d *Dialer) SetSendZC(use bool)
- func (d *Dialer) SetVortex(v *aio.Vortex)
- type FixedFd
- type FixedReaderWriter
- type IPConn
- type ListenConfig
- func (lc *ListenConfig) Listen(ctx context.Context, network string, address string) (ln net.Listener, err error)
- func (lc *ListenConfig) ListenIP(_ context.Context, network string, addr *net.IPAddr) (*IPConn, error)
- func (lc *ListenConfig) ListenMulticastUDP(ctx context.Context, network string, ifi *net.Interface, addr *net.UDPAddr) (*UDPConn, error)
- func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (c net.PacketConn, err error)
- func (lc *ListenConfig) ListenTCP(ctx context.Context, network string, addr *net.TCPAddr) (*TCPListener, error)
- func (lc *ListenConfig) ListenUDP(ctx context.Context, network string, addr *net.UDPAddr) (*UDPConn, error)
- func (lc *ListenConfig) ListenUnix(ctx context.Context, network string, addr *net.UnixAddr) (*UnixListener, error)
- func (lc *ListenConfig) ListenUnixgram(ctx context.Context, network string, addr *net.UnixAddr) (*UnixConn, error)
- func (lc *ListenConfig) SetAutoFixedFdInstall(auto bool)
- func (lc *ListenConfig) SetFastOpen(use bool)
- func (lc *ListenConfig) SetMultipathTCP(use bool)
- func (lc *ListenConfig) SetMultishotAccept(multi bool)
- func (lc *ListenConfig) SetQuickAck(use bool)
- func (lc *ListenConfig) SetReusePort(use bool)
- func (lc *ListenConfig) SetSendZC(use bool)
- func (lc *ListenConfig) SetVortex(v *aio.Vortex)
- type TCPConn
- func (c *TCPConn) AcquireRegisteredBuffer() *aio.FixedBuffer
- func (c *TCPConn) Close() error
- func (c *TCPConn) CloseRead() error
- func (c *TCPConn) CloseWrite() error
- func (c *TCPConn) Context() context.Context
- func (c *TCPConn) File() (f *os.File, err error)
- func (c *TCPConn) FixedFdInstalled() bool
- func (c *TCPConn) InstallFixedFd() (err error)
- func (c *TCPConn) LocalAddr() net.Addr
- func (c *TCPConn) MultipathTCP() (bool, error)
- func (c *TCPConn) Read(b []byte) (n int, err error)
- func (c *TCPConn) ReadBuffer() (int, error)
- func (c *TCPConn) ReadFixed(buf *aio.FixedBuffer) (n int, err error)
- func (c *TCPConn) ReadFrom(r io.Reader) (int64, error)
- func (c *TCPConn) ReleaseRegisteredBuffer(buf *aio.FixedBuffer)
- func (c *TCPConn) RemoteAddr() net.Addr
- func (c *TCPConn) SetDeadline(t time.Time) error
- func (c *TCPConn) SetKeepAlive(keepalive bool) error
- func (c *TCPConn) SetKeepAliveConfig(config net.KeepAliveConfig) error
- func (c *TCPConn) SetKeepAlivePeriod(period time.Duration) error
- func (c *TCPConn) SetLinger(sec int) error
- func (c *TCPConn) SetNoDelay(noDelay bool) error
- func (c *TCPConn) SetReadBuffer(bytes int) error
- func (c *TCPConn) SetReadDeadline(t time.Time) error
- func (c *TCPConn) SetReadFromFilePolicy(policy int32)
- func (c *TCPConn) SetWriteBuffer(bytes int) error
- func (c *TCPConn) SetWriteDeadline(t time.Time) error
- func (c *TCPConn) SyscallConn() (syscall.RawConn, error)
- func (c *TCPConn) UseSendZC(use bool) bool
- func (c *TCPConn) Write(b []byte) (n int, err error)
- func (c *TCPConn) WriteBuffer() (int, error)
- func (c *TCPConn) WriteFixed(buf *aio.FixedBuffer) (n int, err error)
- func (c *TCPConn) WriteTo(w io.Writer) (int64, error)
- type TCPListener
- func (ln *TCPListener) Accept() (net.Conn, error)
- func (ln *TCPListener) AcceptTCP() (tc *TCPConn, err error)
- func (ln *TCPListener) Addr() net.Addr
- func (ln *TCPListener) Close() error
- func (ln *TCPListener) File() (f *os.File, err error)
- func (ln *TCPListener) FixedFdInstalled() bool
- func (ln *TCPListener) InstallFixedFd() error
- func (ln *TCPListener) SetDeadline(t time.Time) error
- func (ln *TCPListener) SyscallConn() (syscall.RawConn, error)
- type UDPConn
- func (c *UDPConn) AcquireRegisteredBuffer() *aio.FixedBuffer
- func (c *UDPConn) Close() error
- func (c *UDPConn) Context() context.Context
- func (c *UDPConn) File() (f *os.File, err error)
- func (c *UDPConn) FixedFdInstalled() bool
- func (c *UDPConn) InstallFixedFd() (err error)
- func (c *UDPConn) LocalAddr() net.Addr
- func (c *UDPConn) Read(b []byte) (n int, err error)
- func (c *UDPConn) ReadBuffer() (int, error)
- func (c *UDPConn) ReadFixed(buf *aio.FixedBuffer) (n int, err error)
- func (c *UDPConn) ReadFrom(b []byte) (n int, addr net.Addr, err error)
- func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
- func (c *UDPConn) ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error)
- func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error)
- func (c *UDPConn) ReadMsgUDPAddrPort(b, oob []byte) (n, oobn, flags int, addr netip.AddrPort, err error)
- func (c *UDPConn) ReleaseRegisteredBuffer(buf *aio.FixedBuffer)
- func (c *UDPConn) RemoteAddr() net.Addr
- func (c *UDPConn) SetDeadline(t time.Time) error
- func (c *UDPConn) SetReadBuffer(bytes int) error
- func (c *UDPConn) SetReadDeadline(t time.Time) error
- func (c *UDPConn) SetWriteBuffer(bytes int) error
- func (c *UDPConn) SetWriteDeadline(t time.Time) error
- func (c *UDPConn) SyscallConn() (syscall.RawConn, error)
- func (c *UDPConn) UseSendMSGZC(use bool) bool
- func (c *UDPConn) UseSendZC(use bool) bool
- func (c *UDPConn) Write(b []byte) (n int, err error)
- func (c *UDPConn) WriteBuffer() (int, error)
- func (c *UDPConn) WriteFixed(buf *aio.FixedBuffer) (n int, err error)
- func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)
- func (c *UDPConn) WriteMsgUDPAddrPort(b, oob []byte, addr netip.AddrPort) (n, oobn int, err error)
- func (c *UDPConn) WriteTo(b []byte, addr net.Addr) (n int, err error)
- func (c *UDPConn) WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)
- func (c *UDPConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (n int, err error)
- type UnixConn
- func (c *UnixConn) AcquireRegisteredBuffer() *aio.FixedBuffer
- func (c *UnixConn) Close() error
- func (c *UnixConn) Context() context.Context
- func (c *UnixConn) File() (f *os.File, err error)
- func (c *UnixConn) FixedFdInstalled() bool
- func (c *UnixConn) InstallFixedFd() (err error)
- func (c *UnixConn) LocalAddr() net.Addr
- func (c *UnixConn) Read(b []byte) (n int, err error)
- func (c *UnixConn) ReadBuffer() (int, error)
- func (c *UnixConn) ReadFixed(buf *aio.FixedBuffer) (n int, err error)
- func (c *UnixConn) ReadFrom(b []byte) (int, net.Addr, error)
- func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *net.UnixAddr, err error)
- func (c *UnixConn) ReadMsgUnix(b []byte, oob []byte) (n, oobn, flags int, addr *net.UnixAddr, err error)
- func (c *UnixConn) ReleaseRegisteredBuffer(buf *aio.FixedBuffer)
- func (c *UnixConn) RemoteAddr() net.Addr
- func (c *UnixConn) SetDeadline(t time.Time) error
- func (c *UnixConn) SetReadBuffer(bytes int) error
- func (c *UnixConn) SetReadDeadline(t time.Time) error
- func (c *UnixConn) SetWriteBuffer(bytes int) error
- func (c *UnixConn) SetWriteDeadline(t time.Time) error
- func (c *UnixConn) SyscallConn() (syscall.RawConn, error)
- func (c *UnixConn) UseSendMSGZC(use bool) bool
- func (c *UnixConn) UseSendZC(use bool) bool
- func (c *UnixConn) Write(b []byte) (n int, err error)
- func (c *UnixConn) WriteBuffer() (int, error)
- func (c *UnixConn) WriteFixed(buf *aio.FixedBuffer) (n int, err error)
- func (c *UnixConn) WriteMsgUnix(b []byte, oob []byte, addr *net.UnixAddr) (n int, oobn int, err error)
- func (c *UnixConn) WriteTo(b []byte, addr net.Addr) (int, error)
- func (c *UnixConn) WriteToUnix(b []byte, addr *net.UnixAddr) (int, error)
- type UnixListener
- func (ln *UnixListener) Accept() (net.Conn, error)
- func (ln *UnixListener) AcceptUnix() (c *UnixConn, err error)
- func (ln *UnixListener) Addr() net.Addr
- func (ln *UnixListener) Close() error
- func (ln *UnixListener) File() (f *os.File, err error)
- func (ln *UnixListener) FixedFdInstalled() bool
- func (ln *UnixListener) InstallFixedFd() error
- func (ln *UnixListener) SetDeadline(t time.Time) error
- func (ln *UnixListener) SetUnlinkOnClose(unlink bool)
- func (ln *UnixListener) SyscallConn() (syscall.RawConn, error)
Constants ¶
const ( ReadFromFileUseMMapPolicy = int32(iota) ReadFromFileUseMixPolicy )
Variables ¶
var ( DefaultDialer = Dialer{ Timeout: 15 * time.Second, Deadline: time.Time{}, LocalAddr: nil, KeepAlive: 0, KeepAliveConfig: net.KeepAliveConfig{Enable: true}, MultipathTCP: false, FastOpen: false, QuickAck: false, SendZC: false, AutoFixedFdInstall: false, Control: nil, ControlContext: nil, } )
Functions ¶
func Dial ¶
Dial connects to the address on the named network.
Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and "unixpacket".
For TCP and UDP networks, the address has the form "host:port". The host must be a literal IP address, or a host name that can be resolved to IP addresses. The port must be a literal port number or a service name. If the host is a literal IPv6 address it must be enclosed in square brackets, as in "[2001:db8::1]:80" or "[fe80::1%zone]:80". The zone specifies the scope of the literal IPv6 address as defined in RFC 4007. The functions [JoinHostPort] and [SplitHostPort] manipulate a pair of host and port in this form. When using TCP, and the host resolves to multiple IP addresses, Dial will try each IP address in order until one succeeds.
Examples:
Dial("tcp", "golang.org:http") Dial("tcp", "192.0.2.1:http") Dial("tcp", "198.51.100.1:80") Dial("udp", "[2001:db8::1]:domain") Dial("udp", "[fe80::1%lo0]:53") Dial("tcp", ":80")
For IP networks, the network must be "ip", "ip4" or "ip6" followed by a colon and a literal protocol number or a protocol name, and the address has the form "host". The host must be a literal IP address or a literal IPv6 address with zone. It depends on each operating system how the operating system behaves with a non-well known protocol number such as "0" or "255".
Examples:
Dial("ip4:1", "192.0.2.1") Dial("ip6:ipv6-icmp", "2001:db8::1") Dial("ip6:58", "fe80::1%lo0")
For TCP, UDP and IP networks, if the host is empty or a literal unspecified IP address, as in ":80", "0.0.0.0:80" or "[::]:80" for TCP and UDP, "", "0.0.0.0" or "::" for IP, the local system is assumed.
For Unix networks, the address must be a file system path.
func DialContext ¶
DialContext same as Dial but takes a context.
func DialContextTimeout ¶ added in v1.5.6
func DialContextTimeout(ctx context.Context, network string, address string, timeout time.Duration) (net.Conn, error)
DialContextTimeout acts like DialTimeout but takes a context.
func DialTimeout ¶
DialTimeout acts like Dial but takes a timeout.
The timeout includes name resolution, if required. When using TCP, and the host in the address parameter resolves to multiple IP addresses, the timeout is spread over each consecutive dial, such that each is given an appropriate fraction of the time to connect.
See func Dial for a description of the network and address parameters.
func Listen ¶
Listen announces on the local network address.
The network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket".
For TCP networks, if the host in the address parameter is empty or a literal unspecified IP address, Listen listens on all available unicast and anycast IP addresses of the local system. To only use IPv4, use network "tcp4". The address can use a host name, but this is not recommended, because it will create a listener for at most one of the host's IP addresses. If the port in the address parameter is empty or "0", as in "127.0.0.1:" or "[::1]:0", a port number is automatically chosen. The [Addr] method of [Listener] can be used to discover the chosen port.
See func Dial for a description of the network and address parameters.
Listen uses context.Background internally; to specify the context, use ListenConfig.Listen.
func ListenPacket ¶
func ListenPacket(network string, addr string) (c net.PacketConn, err error)
ListenPacket announces on the local network address.
The network must be "udp", "udp4", "udp6", "unixgram", or an IP transport. The IP transports are "ip", "ip4", or "ip6" followed by a colon and a literal protocol number or a protocol name, as in "ip:1" or "ip:icmp".
For UDP and IP networks, if the host in the address parameter is empty or a literal unspecified IP address, ListenPacket listens on all available IP addresses of the local system except multicast IP addresses. To only use IPv4, use network "udp4" or "ip4:proto". The address can use a host name, but this is not recommended, because it will create a listener for at most one of the host's IP addresses. If the port in the address parameter is empty or "0", as in "127.0.0.1:" or "[::1]:0", a port number is automatically chosen. The LocalAddr method of [PacketConn] can be used to discover the chosen port.
See func Dial for a description of the network and address parameters.
ListenPacket uses context.Background internally; to specify the context, use ListenConfig.ListenPacket.
func Presets ¶ added in v1.5.6
Presets preset aio options, must be called before Pin, Dial and Listen.
func UseProcessPriority ¶
func UseProcessPriority(level process.PriorityLevel)
UseProcessPriority set process priority
Types ¶
type Dialer ¶
type Dialer struct { net.Dialer // Timeout is the maximum amount of time a dial will wait for // a connect to complete. If Deadline is also set, it may fail // earlier. // // The default is no timeout. // // When using TCP and dialing a host name with multiple IP // addresses, the timeout may be divided between them. // // With or without a timeout, the operating system may impose // its own earlier timeout. For instance, TCP timeouts are // often around 3 minutes. Timeout time.Duration // Deadline is the absolute point in time after which dials // will fail. If Timeout is set, it may fail earlier. // Zero means no deadline, or dependent on the operating system // as with the Timeout option. Deadline time.Time // KeepAlive specifies the interval between keep-alive // probes for an active network connection. // // KeepAlive is ignored if KeepAliveConfig.Enable is true. // // If zero, keep-alive probes are sent with a default value // (currently 15 seconds), if supported by the protocol and operating // system. Network protocols or operating systems that do // not support keep-alive ignore this field. // If negative, keep-alive probes are disabled. KeepAlive time.Duration // KeepAliveConfig specifies the keep-alive probe configuration // for an active network connection, when supported by the // protocol and operating system. // // If KeepAliveConfig.Enable is true, keep-alive probes are enabled. // If KeepAliveConfig.Enable is false and KeepAlive is negative, // keep-alive probes are disabled. KeepAliveConfig net.KeepAliveConfig // LocalAddr is the local address to use when dialing an // address. The address must be of a compatible type for the // network being dialed. // If nil, a local address is automatically chosen. LocalAddr net.Addr // FallbackDelay specifies the length of time to wait before // spawning a RFC 6555 Fast Fallback connection. That is, this // is the amount of time to wait for IPv6 to succeed before // assuming that IPv6 is misconfigured and falling back to // IPv4. // // If zero, a default delay of 300ms is used. // A negative value disables Fast Fallback support. FallbackDelay time.Duration // If MultipathTCP is set to a value allowing Multipath TCP (MPTCP) to be // used, any call to Dial with "tcp(4|6)" as network will use MPTCP if // supported by the operating system. MultipathTCP bool // FastOpen is set TCP_FASTOPEN FastOpen bool // QuickAck is set TCP_QUICKACK QuickAck bool // SendZC is set IOURING.OP_SENDZC SendZC bool // AutoFixedFdInstall is set install conn fd into iouring after accepted. AutoFixedFdInstall bool // If Control is not nil, it is called after creating the network // connection but before actually dialing. // // Network and address parameters passed to Control function are not // necessarily the ones passed to Dial. Calling Dial with TCP networks // will cause the Control function to be called with "tcp4" or "tcp6", // UDP networks become "udp4" or "udp6", IP networks become "ip4" or "ip6", // and other known networks are passed as-is. // // Control is ignored if ControlContext is not nil. Control func(network, address string, c syscall.RawConn) error // If ControlContext is not nil, it is called after creating the network // connection but before actually dialing. // // Network and address parameters passed to ControlContext function are not // necessarily the ones passed to Dial. Calling Dial with TCP networks // will cause the ControlContext function to be called with "tcp4" or "tcp6", // UDP networks become "udp4" or "udp6", IP networks become "ip4" or "ip6", // and other known networks are passed as-is. // // If ControlContext is not nil, Control is ignored. ControlContext func(ctx context.Context, network, address string, c syscall.RawConn) error // Vortex customize [aio.Vortex] Vortex *aio.Vortex }
func (*Dialer) Dial ¶
Dial connects to the address on the named network.
See func Dial for a description of the network and address parameters.
Dial uses context.Background internally; to specify the context, use Dialer.DialContext.
func (*Dialer) DialContext ¶
DialContext connects to the address on the named network using the provided context.
The provided Context must be non-nil. If the context expires before the connection is complete, an error is returned. Once successfully connected, any expiration of the context will not affect the connection.
When using TCP, and the host in the address parameter resolves to multiple network addresses, any dial timeout (from d.Timeout or ctx) is spread over each consecutive dial, such that each is given an appropriate fraction of the time to connect. For example, if a host has 4 IP addresses and the timeout is 1 minute, the connect to each single address will be given 15 seconds to complete before trying the next one.
See func Dial for a description of the network and address parameters.
func (*Dialer) DialIP ¶
func (d *Dialer) DialIP(_ context.Context, network string, laddr, raddr *net.IPAddr) (*IPConn, error)
DialIP acts like Dial for IP networks.
The network must be an IP network name; see func Dial for details.
If laddr is nil, a local address is automatically chosen. If the IP field of raddr is nil or an unspecified IP address, the local system is assumed.
func (*Dialer) DialTCP ¶
func (d *Dialer) DialTCP(ctx context.Context, network string, laddr, raddr *net.TCPAddr) (*TCPConn, error)
DialTCP acts like Dial for TCP networks.
The network must be a TCP network name; see func Dial for details.
If laddr is nil, a local address is automatically chosen. If the IP field of raddr is nil or an unspecified IP address, the local system is assumed.
func (*Dialer) DialUDP ¶
func (d *Dialer) DialUDP(ctx context.Context, network string, laddr, raddr *net.UDPAddr) (*UDPConn, error)
DialUDP acts like Dial for UDP networks.
The network must be a UDP network name; see func Dial for details.
If laddr is nil, a local address is automatically chosen. If the IP field of raddr is nil or an unspecified IP address, the local system is assumed.
func (*Dialer) DialUnix ¶
func (d *Dialer) DialUnix(ctx context.Context, network string, laddr, raddr *net.UnixAddr) (*UnixConn, error)
DialUnix acts like Dial for Unix networks.
The network must be a Unix network name; see func Dial for details.
If laddr is non-nil, it is used as the local address for the connection.
func (*Dialer) SetAutoFixedFdInstall ¶ added in v1.5.6
SetAutoFixedFdInstall set auto install fixed fd.
auto install fixed fd when connected. available after [RIO_IOURING_REG_FIXED_FILES] set.
func (*Dialer) SetMultipathTCP ¶
SetMultipathTCP set multi-path tcp.
type FixedFd ¶ added in v1.5.6
type FixedFd interface { // InstallFixedFd // install fixed fd into iouring. InstallFixedFd() (err error) // FixedFdInstalled // check installed. FixedFdInstalled() bool }
FixedFd it is used to install fd into iouring.
func ConvertToFixedFd ¶ added in v1.5.6
ConvertToFixedFd convert net.Conn, rio.TCPListener or rio.UnixListener to FixedFd.
type FixedReaderWriter ¶ added in v1.3.0
type FixedReaderWriter interface { // AcquireRegisteredBuffer // acquire a buffer. // Note! must release buffer after used. AcquireRegisteredBuffer() *aio.FixedBuffer // ReleaseRegisteredBuffer // release a buffer. ReleaseRegisteredBuffer(buf *aio.FixedBuffer) // ReadFixed // read via registered fixed buffer. ReadFixed(buf *aio.FixedBuffer) (n int, err error) // WriteFixed // write via registered fixed buffer. WriteFixed(buf *aio.FixedBuffer) (n int, err error) }
FixedReaderWriter use fixed buffer to read and write, which is registered in iouring.
func ConvertToFixedReaderWriter ¶ added in v1.5.6
func ConvertToFixedReaderWriter(c net.Conn) (fixed FixedReaderWriter, ok bool)
ConvertToFixedReaderWriter convert net.Conn to FixedReaderWriter.
type IPConn ¶
IPConn is the implementation of the net.Conn and net.PacketConn interfaces for IP network connections.
func DialIP ¶
DialIP acts like Dial for IP networks.
The network must be an IP network name; see func Dial for details.
If laddr is nil, a local address is automatically chosen. If the IP field of raddr is nil or an unspecified IP address, the local system is assumed.
func ListenIP ¶
ListenIP acts like ListenPacket for IP networks.
The network must be an IP network name; see func Dial for details.
If the IP field of laddr is nil or an unspecified IP address, ListenIP listens on all available IP addresses of the local system except multicast IP addresses.
type ListenConfig ¶
type ListenConfig struct { // If Control is not nil, it is called after creating the network // connection but before binding it to the operating system. // // Network and address parameters passed to Control function are not // necessarily the ones passed to Listen. Calling Listen with TCP networks // will cause the Control function to be called with "tcp4" or "tcp6", // UDP networks become "udp4" or "udp6", IP networks become "ip4" or "ip6", // and other known networks are passed as-is. Control func(network, address string, c syscall.RawConn) error // KeepAlive specifies the keep-alive period for network // connections accepted by this listener. // // KeepAlive is ignored if KeepAliveConfig.Enable is true. // // If zero, keep-alive are enabled if supported by the protocol // and operating system. Network protocols or operating systems // that do not support keep-alive ignore this field. // If negative, keep-alive are disabled. KeepAlive time.Duration // KeepAliveConfig specifies the keep-alive probe configuration // for an active network connection, when supported by the // protocol and operating system. // // If KeepAliveConfig.Enable is true, keep-alive probes are enabled. // If KeepAliveConfig.Enable is false and KeepAlive is negative, // keep-alive probes are disabled. KeepAliveConfig net.KeepAliveConfig // MultipathTCP is set to a value allowing Multipath TCP (MPTCP) to be // used, any call to Listen with "tcp(4|6)" as network will use MPTCP if // supported by the operating system. MultipathTCP bool // FastOpen is set TCP_FASTOPEN FastOpen bool // QuickAck is set TCP_QUICKACK QuickAck bool // ReusePort is set SO_REUSEPORT ReusePort bool // SendZC is set IOURING.OP_SENDZC SendZC bool // MultishotAccept is set use IOURING.OP_MULTISHOT_ACCEPT MultishotAccept bool // AutoFixedFdInstall is set install conn fd into iouring after accepted. AutoFixedFdInstall bool // Vortex customize [aio.Vortex] Vortex *aio.Vortex }
ListenConfig contains options for listening to an address.
func (*ListenConfig) Listen ¶
func (lc *ListenConfig) Listen(ctx context.Context, network string, address string) (ln net.Listener, err error)
Listen announces on the local network address.
See func Listen for a description of the network and address parameters.
The ctx argument is used while resolving the address on which to listen; it does not affect the returned Listener.
func (*ListenConfig) ListenIP ¶
func (lc *ListenConfig) ListenIP(_ context.Context, network string, addr *net.IPAddr) (*IPConn, error)
ListenIP acts like ListenPacket for IP networks.
The network must be an IP network name; see func Dial for details.
If the IP field of laddr is nil or an unspecified IP address, ListenIP listens on all available IP addresses of the local system except multicast IP addresses.
func (*ListenConfig) ListenMulticastUDP ¶
func (lc *ListenConfig) ListenMulticastUDP(ctx context.Context, network string, ifi *net.Interface, addr *net.UDPAddr) (*UDPConn, error)
ListenMulticastUDP acts like ListenPacket for UDP networks but takes a group address on a specific network interface.
The network must be a UDP network name; see func Dial for details.
ListenMulticastUDP listens on all available IP addresses of the local system including the group, multicast IP address. If ifi is nil, ListenMulticastUDP uses the system-assigned multicast interface, although this is not recommended because the assignment depends on platforms and sometimes it might require routing configuration. If the Port field of gaddr is 0, a port number is automatically chosen.
ListenMulticastUDP is just for convenience of simple, small applications. There are golang.org/x/net/ipv4 and golang.org/x/net/ipv6 packages for general purpose uses.
Note that ListenMulticastUDP will set the IP_MULTICAST_LOOP socket option to 0 under IPPROTO_IP, to disable loopback of multicast packets.
func (*ListenConfig) ListenPacket ¶
func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (c net.PacketConn, err error)
ListenPacket announces on the local network address.
See func ListenPacket for a description of the network and address parameters.
The ctx argument is used while resolving the address on which to listen; it does not affect the returned Listener.
func (*ListenConfig) ListenTCP ¶
func (lc *ListenConfig) ListenTCP(ctx context.Context, network string, addr *net.TCPAddr) (*TCPListener, error)
ListenTCP acts like Listen for TCP networks.
The network must be a TCP network name; see func Dial for details.
If the IP field of laddr is nil or an unspecified IP address, ListenTCP listens on all available unicast and anycast IP addresses of the local system. If the Port field of laddr is 0, a port number is automatically chosen.
func (*ListenConfig) ListenUDP ¶
func (lc *ListenConfig) ListenUDP(ctx context.Context, network string, addr *net.UDPAddr) (*UDPConn, error)
ListenUDP acts like ListenPacket for UDP networks.
The network must be a UDP network name; see func Dial for details.
If the IP field of laddr is nil or an unspecified IP address, ListenUDP listens on all available IP addresses of the local system except multicast IP addresses. If the Port field of laddr is 0, a port number is automatically chosen.
func (*ListenConfig) ListenUnix ¶
func (lc *ListenConfig) ListenUnix(ctx context.Context, network string, addr *net.UnixAddr) (*UnixListener, error)
ListenUnix acts like Listen for Unix networks.
The network must be "unix" or "unixpacket".
func (*ListenConfig) ListenUnixgram ¶
func (lc *ListenConfig) ListenUnixgram(ctx context.Context, network string, addr *net.UnixAddr) (*UnixConn, error)
ListenUnixgram acts like ListenPacket for Unix networks.
The network must be "unixgram".
func (*ListenConfig) SetAutoFixedFdInstall ¶ added in v1.5.6
func (lc *ListenConfig) SetAutoFixedFdInstall(auto bool)
SetAutoFixedFdInstall set auto install fixed fd.
auto install fixed fd when connected. available after [RIO_IOURING_REG_FIXED_FILES] set.
func (*ListenConfig) SetFastOpen ¶
func (lc *ListenConfig) SetFastOpen(use bool)
SetFastOpen set fast open.
func (*ListenConfig) SetMultipathTCP ¶
func (lc *ListenConfig) SetMultipathTCP(use bool)
SetMultipathTCP set multi-path tcp.
func (*ListenConfig) SetMultishotAccept ¶ added in v1.5.6
func (lc *ListenConfig) SetMultishotAccept(multi bool)
SetMultishotAccept set multishot accept mode.
func (*ListenConfig) SetQuickAck ¶
func (lc *ListenConfig) SetQuickAck(use bool)
SetQuickAck set quick ack.
func (*ListenConfig) SetReusePort ¶
func (lc *ListenConfig) SetReusePort(use bool)
SetReusePort set reuse port.
func (*ListenConfig) SetSendZC ¶
func (lc *ListenConfig) SetSendZC(use bool)
SetSendZC set send zero-copy.
available after 6.0
func (*ListenConfig) SetVortex ¶ added in v1.5.7
func (lc *ListenConfig) SetVortex(v *aio.Vortex)
SetVortex set customize aio.Vortex.
type TCPConn ¶
type TCPConn struct {
// contains filtered or unexported fields
}
TCPConn is an implementation of the net.Conn interface for TCP network connections.
func DialTCP ¶
DialTCP acts like Dial for TCP networks.
The network must be a TCP network name; see func Dial for details.
If laddr is nil, a local address is automatically chosen. If the IP field of raddr is nil or an unspecified IP address, the local system is assumed.
func (*TCPConn) AcquireRegisteredBuffer ¶ added in v1.3.0
func (c *TCPConn) AcquireRegisteredBuffer() *aio.FixedBuffer
AcquireRegisteredBuffer implements the FixedReaderWriter AcquireRegisteredBuffer method.
func (*TCPConn) CloseRead ¶
CloseRead shuts down the reading side of the TCP connection. Most callers should just use Close.
func (*TCPConn) CloseWrite ¶
CloseWrite shuts down the writing side of the TCP connection. Most callers should just use Close.
func (*TCPConn) File ¶
File returns a copy of the underlying os.File. It is the caller's responsibility to close f when finished. Closing c does not affect f, and closing f does not affect c.
The returned os.File's file descriptor is different from the connection's. Attempting to change properties of the original using this duplicate may or may not have the desired effect.
func (*TCPConn) FixedFdInstalled ¶ added in v1.5.6
func (c *TCPConn) FixedFdInstalled() bool
FixedFdInstalled implements the FixedFd FixedFdInstalled method.
func (*TCPConn) InstallFixedFd ¶ added in v1.5.6
func (c *TCPConn) InstallFixedFd() (err error)
InstallFixedFd implements the FixedFd InstallFixedFd method.
func (*TCPConn) MultipathTCP ¶
MultipathTCP reports whether the ongoing connection is using MPTCP.
If Multipath TCP is not supported by the host, by the other peer or intentionally / accidentally filtered out by a device in between, a fallback to TCP will be done. This method does its best to check if MPTCP is still being used or not.
On Linux, more conditions are verified on kernels >= v5.16, improving the results.
func (*TCPConn) ReadFixed ¶ added in v1.3.0
func (c *TCPConn) ReadFixed(buf *aio.FixedBuffer) (n int, err error)
ReadFixed implements the FixedReaderWriter ReadFixed method.
func (*TCPConn) ReadFrom ¶
ReadFrom implements the io.ReaderFrom ReadFrom method.
func (*TCPConn) ReleaseRegisteredBuffer ¶ added in v1.3.0
func (c *TCPConn) ReleaseRegisteredBuffer(buf *aio.FixedBuffer)
ReleaseRegisteredBuffer implements the FixedReaderWriter ReleaseRegisteredBuffer method.
func (*TCPConn) RemoteAddr ¶
RemoteAddr implements the net.Conn RemoteAddr method.
func (*TCPConn) SetDeadline ¶
SetDeadline implements the net.Conn SetDeadline method.
func (*TCPConn) SetKeepAlive ¶
SetKeepAlive sets whether the operating system should send keep-alive messages on the connection.
func (*TCPConn) SetKeepAliveConfig ¶
func (c *TCPConn) SetKeepAliveConfig(config net.KeepAliveConfig) error
SetKeepAliveConfig configures keep-alive messages sent by the operating system.
func (*TCPConn) SetKeepAlivePeriod ¶
SetKeepAlivePeriod sets the duration the connection needs to remain idle before TCP starts sending keepalive probes.
Note that calling this method on Windows prior to Windows 10 version 1709 will reset the KeepAliveInterval to the default system value, which is normally 1 second.
func (*TCPConn) SetLinger ¶
SetLinger sets the behavior of Close on a connection which still has data waiting to be sent or to be acknowledged.
If sec < 0 (the default), the operating system finishes sending the data in the background.
If sec == 0, the operating system discards any unsent or unacknowledged data.
If sec > 0, the data is sent in the background as with sec < 0. On some operating systems including Linux, this may cause Close to block until all data has been sent or discarded. On some operating systems after sec seconds have elapsed any remaining unsent data may be discarded.
func (*TCPConn) SetNoDelay ¶
SetNoDelay controls whether the operating system should delay packet transmission in hopes of sending fewer packets (Nagle's algorithm). The default is true (no delay), meaning that data is sent as soon as possible after a Write.
func (*TCPConn) SetReadBuffer ¶
SetReadBuffer set SO_RCVBUF.
func (*TCPConn) SetReadDeadline ¶
SetReadDeadline implements the net.Conn SetReadDeadline method.
func (*TCPConn) SetReadFromFilePolicy ¶ added in v1.5.6
func (*TCPConn) SetWriteBuffer ¶
SetWriteBuffer set SO_SNDBUF.
func (*TCPConn) SetWriteDeadline ¶
SetWriteDeadline implements the net.Conn SetWriteDeadline method.
func (*TCPConn) SyscallConn ¶
SyscallConn returns a raw network connection. This implements the syscall.Conn interface.
func (*TCPConn) WriteBuffer ¶
WriteBuffer get SO_SNDBUF.
func (*TCPConn) WriteFixed ¶ added in v1.3.0
func (c *TCPConn) WriteFixed(buf *aio.FixedBuffer) (n int, err error)
WriteFixed implements the FixedReaderWriter WriteFixed method.
type TCPListener ¶
type TCPListener struct {
// contains filtered or unexported fields
}
TCPListener is a TCP network listener. Clients should typically use variables of type net.Listener instead of assuming TCP.
func ListenTCP ¶
func ListenTCP(network string, addr *net.TCPAddr) (*TCPListener, error)
ListenTCP acts like Listen for TCP networks.
The network must be a TCP network name; see func Dial for details.
If the IP field of laddr is nil or an unspecified IP address, ListenTCP listens on all available unicast and anycast IP addresses of the local system. If the Port field of laddr is 0, a port number is automatically chosen.
func (*TCPListener) Accept ¶
func (ln *TCPListener) Accept() (net.Conn, error)
Accept implements the Accept method in the net.Listener interface; it waits for the next call and returns a generic net.Conn.
func (*TCPListener) AcceptTCP ¶
func (ln *TCPListener) AcceptTCP() (tc *TCPConn, err error)
AcceptTCP accepts the next incoming call and returns the new connection.
func (*TCPListener) Addr ¶
func (ln *TCPListener) Addr() net.Addr
Addr returns the listener's network address, a [*TCPAddr]. The Addr returned is shared by all invocations of Addr, so do not modify it.
func (*TCPListener) Close ¶
func (ln *TCPListener) Close() error
Close stops listening on the TCP address. Already Accepted connections are not closed.
func (*TCPListener) File ¶
func (ln *TCPListener) File() (f *os.File, err error)
File returns a copy of the underlying os.File. It is the caller's responsibility to close f when finished. Closing l does not affect f, and closing f does not affect l.
The returned os.File's file descriptor is different from the connection's. Attempting to change properties of the original using this duplicate may or may not have the desired effect.
func (*TCPListener) FixedFdInstalled ¶ added in v1.5.6
func (ln *TCPListener) FixedFdInstalled() bool
FixedFdInstalled implements the FixedFdInstalled method in the FixedFd interface.
func (*TCPListener) InstallFixedFd ¶ added in v1.5.6
func (ln *TCPListener) InstallFixedFd() error
InstallFixedFd implements the InstallFixedFd method in the FixedFd interface.
func (*TCPListener) SetDeadline ¶
func (ln *TCPListener) SetDeadline(t time.Time) error
SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline. Only valid when not multishot accept mode.
func (*TCPListener) SyscallConn ¶
func (ln *TCPListener) SyscallConn() (syscall.RawConn, error)
SyscallConn returns a raw network connection. This implements the syscall.Conn interface.
The returned RawConn only supports calling Control. Read and Write return an error.
type UDPConn ¶
type UDPConn struct {
// contains filtered or unexported fields
}
UDPConn is the implementation of the net.Conn and net.PacketConn interfaces for UDP network connections.
func DialUDP ¶
DialUDP acts like Dial for UDP networks.
The network must be a UDP network name; see func Dial for details.
If laddr is nil, a local address is automatically chosen. If the IP field of raddr is nil or an unspecified IP address, the local system is assumed.
func ListenMulticastUDP ¶
ListenMulticastUDP acts like ListenPacket for UDP networks but takes a group address on a specific network interface.
The network must be a UDP network name; see func Dial for details.
ListenMulticastUDP listens on all available IP addresses of the local system including the group, multicast IP address. If ifi is nil, ListenMulticastUDP uses the system-assigned multicast interface, although this is not recommended because the assignment depends on platforms and sometimes it might require routing configuration. If the Port field of gaddr is 0, a port number is automatically chosen.
ListenMulticastUDP is just for convenience of simple, small applications. There are golang.org/x/net/ipv4 and golang.org/x/net/ipv6 packages for general purpose uses.
Note that ListenMulticastUDP will set the IP_MULTICAST_LOOP socket option to 0 under IPPROTO_IP, to disable loopback of multicast packets.
func ListenUDP ¶
ListenUDP acts like ListenPacket for UDP networks.
The network must be a UDP network name; see func Dial for details.
If the IP field of laddr is nil or an unspecified IP address, ListenUDP listens on all available IP addresses of the local system except multicast IP addresses. If the Port field of laddr is 0, a port number is automatically chosen.
func (*UDPConn) AcquireRegisteredBuffer ¶ added in v1.3.0
func (c *UDPConn) AcquireRegisteredBuffer() *aio.FixedBuffer
AcquireRegisteredBuffer implements the FixedReaderWriter AcquireRegisteredBuffer method.
func (*UDPConn) File ¶
File returns a copy of the underlying os.File. It is the caller's responsibility to close f when finished. Closing c does not affect f, and closing f does not affect c.
The returned os.File's file descriptor is different from the connection's. Attempting to change properties of the original using this duplicate may or may not have the desired effect.
func (*UDPConn) FixedFdInstalled ¶ added in v1.5.6
func (c *UDPConn) FixedFdInstalled() bool
FixedFdInstalled implements the FixedFd FixedFdInstalled method.
func (*UDPConn) InstallFixedFd ¶ added in v1.5.6
func (c *UDPConn) InstallFixedFd() (err error)
InstallFixedFd implements the FixedFd InstallFixedFd method.
func (*UDPConn) ReadFixed ¶ added in v1.3.0
func (c *UDPConn) ReadFixed(buf *aio.FixedBuffer) (n int, err error)
ReadFixed implements the FixedReaderWriter ReadFixed method.
func (*UDPConn) ReadFrom ¶
ReadFrom implements the net.PacketConn ReadFrom method.
func (*UDPConn) ReadFromUDP ¶
ReadFromUDP acts like UDPConn.ReadFrom but returns a net.UDPAddr.
func (*UDPConn) ReadFromUDPAddrPort ¶
ReadFromUDPAddrPort acts like ReadFrom but returns a netip.AddrPort.
If c is bound to an unspecified address, the returned netip.AddrPort's address might be an IPv4-mapped IPv6 address. Use netip.Addr.Unmap to get the address without the IPv6 prefix.
func (*UDPConn) ReadMsgUDP ¶
ReadMsgUDP reads a message from c, copying the payload into b and the associated out-of-band data into oob. It returns the number of bytes copied into b, the number of bytes copied into oob, the flags that were set on the message and the source address of the message.
The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be used to manipulate IP-level socket options in oob.
func (*UDPConn) ReadMsgUDPAddrPort ¶
func (c *UDPConn) ReadMsgUDPAddrPort(b, oob []byte) (n, oobn, flags int, addr netip.AddrPort, err error)
ReadMsgUDPAddrPort is like UDPConn.ReadMsgUDP but returns an netip.AddrPort instead of a net.UDPAddr.
func (*UDPConn) ReleaseRegisteredBuffer ¶ added in v1.3.0
func (c *UDPConn) ReleaseRegisteredBuffer(buf *aio.FixedBuffer)
ReleaseRegisteredBuffer implements the FixedReaderWriter ReleaseRegisteredBuffer method.
func (*UDPConn) RemoteAddr ¶
RemoteAddr implements the net.Conn RemoteAddr method.
func (*UDPConn) SetDeadline ¶
SetDeadline implements the net.Conn SetDeadline method.
func (*UDPConn) SetReadBuffer ¶
SetReadBuffer set SO_RCVBUF.
func (*UDPConn) SetReadDeadline ¶
SetReadDeadline implements the net.Conn SetReadDeadline method.
func (*UDPConn) SetWriteBuffer ¶
SetWriteBuffer set SO_SNDBUF.
func (*UDPConn) SetWriteDeadline ¶
SetWriteDeadline implements the net.Conn SetWriteDeadline method.
func (*UDPConn) SyscallConn ¶
SyscallConn returns a raw network connection. This implements the syscall.Conn interface.
func (*UDPConn) UseSendMSGZC ¶ added in v1.5.6
UseSendMSGZC try to enable sendmsg_zc.
func (*UDPConn) WriteBuffer ¶
WriteBuffer get SO_SNDBUF.
func (*UDPConn) WriteFixed ¶ added in v1.3.0
func (c *UDPConn) WriteFixed(buf *aio.FixedBuffer) (n int, err error)
WriteFixed implements the FixedReaderWriter WriteFixed method.
func (*UDPConn) WriteMsgUDP ¶
WriteMsgUDP writes a message to addr via c if c isn't connected, or to c's remote address if c is connected (in which case addr must be nil). The payload is copied from b and the associated out-of-band data is copied from oob. It returns the number of payload and out-of-band bytes written.
The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be used to manipulate IP-level socket options in oob.
func (*UDPConn) WriteMsgUDPAddrPort ¶
WriteMsgUDPAddrPort is like UDPConn.WriteMsgUDP but takes a netip.AddrPort instead of a net.UDPAddr.
func (*UDPConn) WriteTo ¶
WriteTo implements the net.PacketConn WriteTo method.
func (*UDPConn) WriteToUDP ¶
WriteToUDP acts like UDPConn.WriteTo but takes a [UDPAddr].
func (*UDPConn) WriteToUDPAddrPort ¶
WriteToUDPAddrPort acts like UDPConn.WriteTo but takes a netip.AddrPort.
type UnixConn ¶
type UnixConn struct {
// contains filtered or unexported fields
}
UnixConn is an implementation of the net.Conn interface for connections to Unix domain sockets.
func DialUnix ¶
DialUnix acts like Dial for Unix networks.
The network must be a Unix network name; see func Dial for details.
If laddr is non-nil, it is used as the local address for the connection.
func ListenUnixgram ¶
ListenUnixgram acts like ListenPacket for Unix networks.
The network must be "unixgram".
func (*UnixConn) AcquireRegisteredBuffer ¶ added in v1.3.0
func (c *UnixConn) AcquireRegisteredBuffer() *aio.FixedBuffer
AcquireRegisteredBuffer implements the FixedReaderWriter AcquireRegisteredBuffer method.
func (*UnixConn) Close ¶
func (c *UnixConn) Close() error
Close implements the net.Conn Close method.
func (*UnixConn) File ¶
File returns a copy of the underlying os.File. It is the caller's responsibility to close f when finished. Closing c does not affect f, and closing f does not affect c.
The returned os.File's file descriptor is different from the connection's. Attempting to change properties of the original using this duplicate may or may not have the desired effect.
func (*UnixConn) FixedFdInstalled ¶ added in v1.5.6
func (c *UnixConn) FixedFdInstalled() bool
FixedFdInstalled implements the FixedFd FixedFdInstalled method.
func (*UnixConn) InstallFixedFd ¶ added in v1.5.6
func (c *UnixConn) InstallFixedFd() (err error)
InstallFixedFd implements the FixedFd InstallFixedFd method.
func (*UnixConn) ReadBuffer ¶
ReadBuffer get SO_RCVBUF.
func (*UnixConn) ReadFixed ¶ added in v1.3.0
func (c *UnixConn) ReadFixed(buf *aio.FixedBuffer) (n int, err error)
ReadFixed implements the FixedReaderWriter ReadFixed method.
func (*UnixConn) ReadFrom ¶
ReadFrom implements the net.PacketConn ReadFrom method.
func (*UnixConn) ReadFromUnix ¶
ReadFromUnix acts like UnixConn.ReadFrom but returns a net.UnixAddr.
func (*UnixConn) ReadMsgUnix ¶
func (c *UnixConn) ReadMsgUnix(b []byte, oob []byte) (n, oobn, flags int, addr *net.UnixAddr, err error)
ReadMsgUnix reads a message from c, copying the payload into b and the associated out-of-band data into oob. It returns the number of bytes copied into b, the number of bytes copied into oob, the flags that were set on the message and the source address of the message.
Note that if len(b) == 0 and len(oob) > 0, this function will still read (and discard) 1 byte from the connection.
func (*UnixConn) ReleaseRegisteredBuffer ¶ added in v1.3.0
func (c *UnixConn) ReleaseRegisteredBuffer(buf *aio.FixedBuffer)
ReleaseRegisteredBuffer implements the FixedReaderWriter ReleaseRegisteredBuffer method.
func (*UnixConn) RemoteAddr ¶
RemoteAddr implements the net.Conn RemoteAddr method.
func (*UnixConn) SetDeadline ¶
SetDeadline implements the net.Conn SetDeadline method.
func (*UnixConn) SetReadBuffer ¶
SetReadBuffer set SO_RCVBUF.
func (*UnixConn) SetReadDeadline ¶
SetReadDeadline implements the net.Conn SetReadDeadline method.
func (*UnixConn) SetWriteBuffer ¶
SetWriteBuffer set SO_SNDBUF.
func (*UnixConn) SetWriteDeadline ¶
SetWriteDeadline implements the net.Conn SetWriteDeadline method.
func (*UnixConn) SyscallConn ¶
SyscallConn returns a raw network connection. This implements the syscall.Conn interface.
func (*UnixConn) UseSendMSGZC ¶ added in v1.5.6
UseSendMSGZC try to enable sendmsg_zc.
func (*UnixConn) WriteBuffer ¶
WriteBuffer get SO_SNDBUF.
func (*UnixConn) WriteFixed ¶ added in v1.3.0
func (c *UnixConn) WriteFixed(buf *aio.FixedBuffer) (n int, err error)
WriteFixed implements the FixedReaderWriter WriteFixed method.
func (*UnixConn) WriteMsgUnix ¶
func (c *UnixConn) WriteMsgUnix(b []byte, oob []byte, addr *net.UnixAddr) (n int, oobn int, err error)
WriteMsgUnix writes a message to addr via c, copying the payload from b and the associated out-of-band data from oob. It returns the number of payload and out-of-band bytes written.
Note that if len(b) == 0 and len(oob) > 0, this function will still write 1 byte to the connection.
func (*UnixConn) WriteTo ¶
WriteTo implements the net.PacketConn WriteTo method.
func (*UnixConn) WriteToUnix ¶
WriteToUnix acts like UnixConn.WriteTo but takes a net.UnixAddr.
type UnixListener ¶
type UnixListener struct {
// contains filtered or unexported fields
}
UnixListener is a Unix domain socket listener. Clients should typically use variables of type net.Listener instead of assuming Unix domain sockets.
func ListenUnix ¶
func ListenUnix(network string, addr *net.UnixAddr) (*UnixListener, error)
ListenUnix acts like Listen for Unix networks.
The network must be "unix" or "unixpacket".
func (*UnixListener) Accept ¶
func (ln *UnixListener) Accept() (net.Conn, error)
Accept implements the Accept method in the net.Listener interface. Returned connections will be of type *UnixConn.
func (*UnixListener) AcceptUnix ¶
func (ln *UnixListener) AcceptUnix() (c *UnixConn, err error)
AcceptUnix accepts the next incoming call and returns the new connection.
func (*UnixListener) Addr ¶
func (ln *UnixListener) Addr() net.Addr
Addr returns the listener's network address, a [*TCPAddr]. The Addr returned is shared by all invocations of Addr, so do not modify it.
func (*UnixListener) Close ¶
func (ln *UnixListener) Close() error
Close stops listening on the Unix address. Already accepted connections are not closed.
func (*UnixListener) File ¶
func (ln *UnixListener) File() (f *os.File, err error)
File returns a copy of the underlying os.File. It is the caller's responsibility to close f when finished. Closing l does not affect f, and closing f does not affect l.
The returned os.File's file descriptor is different from the connection's. Attempting to change properties of the original using this duplicate may or may not have the desired effect.
func (*UnixListener) FixedFdInstalled ¶ added in v1.5.6
func (ln *UnixListener) FixedFdInstalled() bool
FixedFdInstalled implements the FixedFdInstalled method in the FixedFd interface.
func (*UnixListener) InstallFixedFd ¶ added in v1.5.6
func (ln *UnixListener) InstallFixedFd() error
InstallFixedFd implements the InstallFixedFd method in the FixedFd interface.
func (*UnixListener) SetDeadline ¶
func (ln *UnixListener) SetDeadline(t time.Time) error
SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline. Only valid when not multishot accept mode.
func (*UnixListener) SetUnlinkOnClose ¶
func (ln *UnixListener) SetUnlinkOnClose(unlink bool)
SetUnlinkOnClose sets whether the underlying socket file should be removed from the file system when the listener is closed.
The default behavior is to unlink the socket file only when package net created it. That is, when the listener and the underlying socket file were created by a call to Listen or ListenUnix, then by default closing the listener will remove the socket file. but if the listener was created by a call to FileListener to use an already existing socket file, then by default closing the listener will not remove the socket file.
func (*UnixListener) SyscallConn ¶
func (ln *UnixListener) SyscallConn() (syscall.RawConn, error)
SyscallConn returns a raw network connection. This implements the syscall.Conn interface.
The returned RawConn only supports calling Control. Read and Write return an error.