README
¶
RIO
基于IOURING
的AIO
网络库,非CGO
方式,且遵循标准库使用设计模式。
支持协议:TCP
、UDP
、UNIX
、UNIXGRAM
(IP
为代理标准库)。
Linux 内核版本需要>= 5.14
,推荐版本为>= 6.1
。
性能
TCPKALI
服务端环境:Win11(Hyper-V)、Ubuntu24.10(6.11.0-8-generic)、CPU(4核)。
客户端环境:Win11(WSL2)、内核(6.6.36.6-microsoft-standard-WSL2)、CPU(4核)。
tcpkali --workers 1 -c 50 -T 10s -m "PING" 192.168.100.120:9000
注意:请不要本地压测本地。

类型 | packet rate estimate |
---|---|
RIO | 27791.8 |
GNET | 22095.3 |
EVIO | 14272.9 |
NET(STD) | 15161.3 |
详细结果
------ RIO ------
Destination: [192.168.100.120]:9000
Interface eth0 address [192.168.100.1]:0
Using interface eth0 to connect to [192.168.100.120]:9000
Ramped up to 50 connections.
Total data sent: 287.6 MiB (301548988 bytes)
Total data received: 286.4 MiB (300361173 bytes)
Bandwidth per channel: 9.627⇅ Mbps (1203.3 kBps)
Aggregate bandwidth: 240.188↓, 241.138↑ Mbps
Packet rate estimate: 27791.8↓, 20820.1↑ (3↓, 32↑ TCP MSS/op)
Test duration: 10.0042 s.
------ GNET ------
Destination: [192.168.100.120]:9000
Interface eth0 address [192.168.100.1]:0
Using interface eth0 to connect to [192.168.100.120]:9000
Ramped up to 50 connections.
Total data sent: 219.4 MiB (230096896 bytes)
Total data received: 217.7 MiB (228243396 bytes)
Bandwidth per channel: 7.329⇅ Mbps (916.1 kBps)
Aggregate bandwidth: 182.481↓, 183.963↑ Mbps
Packet rate estimate: 22095.3↓, 15777.4↑ (3↓, 44↑ TCP MSS/op)
Test duration: 10.0062 s.
------ EVIO ------
Destination: [192.168.100.120]:9000
Interface eth0 address [192.168.100.1]:0
Using interface eth0 to connect to [192.168.100.120]:9000
Ramped up to 50 connections.
Total data sent: 200.4 MiB (210108416 bytes)
Total data received: 198.6 MiB (208234360 bytes)
Bandwidth per channel: 6.688⇅ Mbps (836.0 kBps)
Aggregate bandwidth: 166.458↓, 167.956↑ Mbps
Packet rate estimate: 14272.9↓, 14412.0↑ (2↓, 44↑ TCP MSS/op)
Test duration: 10.0078 s.
------ NET ------
Destination: [192.168.100.120]:9000
Interface eth0 address [192.168.100.1]:0
Using interface eth0 to connect to [192.168.100.120]:9000
Ramped up to 50 connections.
Total data sent: 199.2 MiB (208928768 bytes)
Total data received: 197.8 MiB (207359332 bytes)
Bandwidth per channel: 6.654⇅ Mbps (831.7 kBps)
Aggregate bandwidth: 165.720↓, 166.974↑ Mbps
Packet rate estimate: 15161.3↓, 14565.3↑ (2↓, 45↑ TCP MSS/op)
Test duration: 10.0101 s.
使用
go get -u github.com/brickingsoft/rio
基本使用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场景:
// server("github.com/brickingsoft/rio/tls")
ln, _ = tls.Listen("tcp", "127.0.0.1:9000", tls.ConfigFrom(config))
// server(use wrap)
ln, _ := rio.Listen("tcp", ":9000")
ln, _ := tls.NewListener(ln, config)
// client("github.com/brickingsoft/rio/tls")
conn, _ = tls.Dial("tcp", "127.0.0.1:9000", tls.ConfigFrom(config))
// client(use 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 sendfile
reader, ok := conn.(io.ReaderFrom)
// 转换成 TCP 链接
tcpConn, ok := conn.(*rio.TCPConn)
// 转换成 UDP 链接
udpConn, ok := conn.(*rio.UDPConn)
// 转换成 UNIX 链接
unixConn, ok := conn.(*rio.UnixConn)
纯客户端场景:
建议PIN
住IOURING
,直到程序退出再UNPIN
。
因为IOURING
的生命周期为当被使用时开启,当被没有被使用时关闭。
因为Listen
的生命周期往往和程序是一致的,所以IOURING
为常驻状况。
而Dial
的生命周期是短的,往往是频繁Dial
,所以需要PIN
来常驻IOURING
,而不是频繁启停。
// 程序启动位置
rio.Pin()
// 程序退出位置
rio.Unpin()
HTTP场景:
Server 使用Listener
代替法。
Client 使用RoundTripper
代替法。
// http server
http.Serve(ln, handler)
// fasthttp server
fasthttp.Serve(ln, handler)
REUSE PORT(监听TCP时自动启用):
lc := rio.ListenConfig{}
lc.SetReusePort(true)
ln, lnErr := lc.Listen(...)
进阶调参
通过设置环境变量进行调控,具体详见 IOURING。
名称 | 值 | 说明 |
---|---|---|
IOURING_ENTRIES | 数字 | 环大小,默认为最大值 16384。 |
IOURING_SETUP_FLAGS | 文本 | 标识,如IORING_SETUP_SQPOLL, IORING_SETUP_SUBMIT_ALL 等。 |
IOURING_SETUP_FLAGS_SCHEMA | 文本 | 标识方案,DEFAULT 或 PERFORMANCE 。 |
IOURING_SQ_THREAD_CPU | 数字 | 设置环锁亲和的CPUID。 |
IOURING_SQ_THREAD_IDLE | 数字 | 在含有IORING_SETUP_SQPOLL 标识时,设置空闲时长,单位为毫秒,默认是 1 毫秒。 |
IOURING_PREPARE_BATCH_SIZE | 数字 | 准备 SQE 的缓冲大小,默认为 SQ 的大小。 |
IOURING_USE_CPU_AFFILIATE | 布尔 | 是否使用 CPU AFFILIATE。 |
IOURING_CURVE_TRANSMISSION | 文本 | 设置等待 CQ 策略曲线,如 1:1us, 8:2us 。 |
注意事项:
IOURING_SETUP_FLAGS
与系统内核版本有关联,请务必确认版本。IORING_SETUP_SQPOLL
取决于运行环境,请自行测试效果。IOURING_SETUP_FLAGS_SCHEMA
优先级低于IOURING_SETUP_FLAGS
。PERFORMANCE
为IORING_SETUP_SQPOLL
IORING_SETUP_SUBMIT_ALL
IORING_SETUP_SINGLE_ISSUER
的组合。DEFAULT
为IORING_SETUP_SUBMIT_ALL
。
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 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 Pin() (err error)
- func PrepareIOURingSetupOptions(options ...aio.Option)
- func Unpin() (err error)
- func UseProcessPriority(level process.PriorityLevel)
- func UseReadFromFilePolicy(policy int32)
- 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) SetFastOpen(use bool)
- func (d *Dialer) SetMultipathTCP(use bool)
- func (d *Dialer) SetQuickAck(use bool)
- func (d *Dialer) SetSendZC(use bool)
- 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) SetFastOpen(use bool)
- func (lc *ListenConfig) SetMultipathTCP(use bool)
- func (lc *ListenConfig) SetQuickAck(use bool)
- func (lc *ListenConfig) SetReusePort(use bool)
- func (lc *ListenConfig) SetSendZC(use bool)
- type TCPConn
- 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) 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) ReadFrom(r io.Reader) (int64, error)
- 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) SetWriteBuffer(bytes int) error
- func (c *TCPConn) SetWriteDeadline(t time.Time) error
- func (c *TCPConn) SyscallConn() (syscall.RawConn, error)
- func (c *TCPConn) Write(b []byte) (n int, err error)
- func (c *TCPConn) WriteBuffer() (int, 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) SetDeadline(t time.Time) error
- func (ln *TCPListener) SyscallConn() (syscall.RawConn, error)
- type UDPConn
- func (c *UDPConn) Close() error
- func (c *UDPConn) Context() context.Context
- func (c *UDPConn) File() (f *os.File, 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) 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) 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) Write(b []byte) (n int, err error)
- func (c *UDPConn) WriteBuffer() (int, 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) Close() error
- func (c *UnixConn) Context() context.Context
- func (c *UnixConn) File() (f *os.File, 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) 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) 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) Write(b []byte) (n int, err error)
- func (c *UnixConn) WriteBuffer() (int, 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) SetDeadline(t time.Time) error
- func (ln *UnixListener) SetUnlinkOnClose(unlink bool)
- func (ln *UnixListener) SyscallConn() (syscall.RawConn, error)
Constants ¶
View Source
const ( ReadFromFileUseMMapPolicy = int32(iota) ReadFromFileUseMixPolicy )
Variables ¶
Functions ¶
func DialContext ¶
func DialTimeout ¶
func ListenPacket ¶
func ListenPacket(network string, addr string) (c net.PacketConn, err error)
func Pin ¶
func Pin() (err error)
Pin 钉住 IOURING 。 一般用于程序启动时。 这用手动管理 IOURING 的生命周期,一般用于只有 Dial 的使用。 注意:必须 Unpin 来关闭 IOURING 。
func PrepareIOURingSetupOptions ¶ added in v1.2.2
func UseProcessPriority ¶
func UseProcessPriority(level process.PriorityLevel)
func UseReadFromFilePolicy ¶
func UseReadFromFilePolicy(policy int32)
Types ¶
type Dialer ¶
type Dialer struct { Timeout time.Duration Deadline time.Time KeepAlive time.Duration KeepAliveConfig net.KeepAliveConfig MultipathTCP bool FastOpen bool QuickAck bool UseSendZC bool Control func(network, address string, c syscall.RawConn) error ControlContext func(ctx context.Context, network, address string, c syscall.RawConn) error }
func (*Dialer) DialContext ¶
func (*Dialer) SetFastOpen ¶
func (*Dialer) SetMultipathTCP ¶
func (*Dialer) SetQuickAck ¶
type ListenConfig ¶
type ListenConfig struct { Control func(network, address string, c syscall.RawConn) error KeepAlive time.Duration KeepAliveConfig net.KeepAliveConfig UseSendZC bool MultipathTCP bool FastOpen bool QuickAck bool ReusePort bool }
func (*ListenConfig) ListenMulticastUDP ¶
func (*ListenConfig) ListenPacket ¶
func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (c net.PacketConn, err error)
func (*ListenConfig) ListenTCP ¶
func (lc *ListenConfig) ListenTCP(ctx context.Context, network string, addr *net.TCPAddr) (*TCPListener, error)
func (*ListenConfig) ListenUnix ¶
func (lc *ListenConfig) ListenUnix(ctx context.Context, network string, addr *net.UnixAddr) (*UnixListener, error)
func (*ListenConfig) ListenUnixgram ¶
func (*ListenConfig) SetFastOpen ¶
func (lc *ListenConfig) SetFastOpen(use bool)
func (*ListenConfig) SetMultipathTCP ¶
func (lc *ListenConfig) SetMultipathTCP(use bool)
func (*ListenConfig) SetQuickAck ¶
func (lc *ListenConfig) SetQuickAck(use bool)
func (*ListenConfig) SetReusePort ¶
func (lc *ListenConfig) SetReusePort(use bool)
func (*ListenConfig) SetSendZC ¶
func (lc *ListenConfig) SetSendZC(use bool)
type TCPConn ¶
type TCPConn struct {
// contains filtered or unexported fields
}
func (*TCPConn) CloseWrite ¶
func (*TCPConn) MultipathTCP ¶
func (*TCPConn) ReadBuffer ¶
func (*TCPConn) RemoteAddr ¶
func (*TCPConn) SetDeadline ¶
func (*TCPConn) SetKeepAlive ¶
func (*TCPConn) SetKeepAliveConfig ¶
func (c *TCPConn) SetKeepAliveConfig(config net.KeepAliveConfig) error
func (*TCPConn) SetKeepAlivePeriod ¶
func (*TCPConn) SetNoDelay ¶
func (*TCPConn) SetReadBuffer ¶
func (*TCPConn) SetReadDeadline ¶
func (*TCPConn) SetWriteBuffer ¶
func (*TCPConn) SetWriteDeadline ¶
func (*TCPConn) SyscallConn ¶
func (*TCPConn) WriteBuffer ¶
type TCPListener ¶
type TCPListener struct {
// contains filtered or unexported fields
}
func (*TCPListener) AcceptTCP ¶
func (ln *TCPListener) AcceptTCP() (tc *TCPConn, err error)
func (*TCPListener) Addr ¶
func (ln *TCPListener) Addr() net.Addr
func (*TCPListener) Close ¶
func (ln *TCPListener) Close() error
func (*TCPListener) SetDeadline ¶
func (ln *TCPListener) SetDeadline(t time.Time) error
func (*TCPListener) SyscallConn ¶
func (ln *TCPListener) SyscallConn() (syscall.RawConn, error)
type UDPConn ¶
type UDPConn struct {
// contains filtered or unexported fields
}
func ListenMulticastUDP ¶
func (*UDPConn) ReadBuffer ¶
func (*UDPConn) ReadFromUDP ¶
func (*UDPConn) ReadFromUDPAddrPort ¶
func (*UDPConn) ReadMsgUDP ¶
func (*UDPConn) ReadMsgUDPAddrPort ¶
func (*UDPConn) RemoteAddr ¶
func (*UDPConn) SetDeadline ¶
func (*UDPConn) SetReadBuffer ¶
func (*UDPConn) SetReadDeadline ¶
func (*UDPConn) SetWriteBuffer ¶
func (*UDPConn) SetWriteDeadline ¶
func (*UDPConn) SyscallConn ¶
func (*UDPConn) WriteBuffer ¶
func (*UDPConn) WriteMsgUDP ¶
func (*UDPConn) WriteMsgUDPAddrPort ¶
func (*UDPConn) WriteToUDP ¶
type UnixConn ¶
type UnixConn struct {
// contains filtered or unexported fields
}
func (*UnixConn) ReadBuffer ¶
func (*UnixConn) ReadFromUnix ¶
func (*UnixConn) ReadMsgUnix ¶
func (*UnixConn) RemoteAddr ¶
func (*UnixConn) SetDeadline ¶
func (*UnixConn) SetReadBuffer ¶
func (*UnixConn) SetReadDeadline ¶
func (*UnixConn) SetWriteBuffer ¶
func (*UnixConn) SetWriteDeadline ¶
func (*UnixConn) SyscallConn ¶
func (*UnixConn) WriteBuffer ¶
func (*UnixConn) WriteMsgUnix ¶
type UnixListener ¶
type UnixListener struct {
// contains filtered or unexported fields
}
func ListenUnix ¶
func ListenUnix(network string, addr *net.UnixAddr) (*UnixListener, error)
func (*UnixListener) AcceptUnix ¶
func (ln *UnixListener) AcceptUnix() (c *UnixConn, err error)
func (*UnixListener) Addr ¶
func (ln *UnixListener) Addr() net.Addr
func (*UnixListener) Close ¶
func (ln *UnixListener) Close() error
func (*UnixListener) SetDeadline ¶
func (ln *UnixListener) SetDeadline(t time.Time) error
func (*UnixListener) SetUnlinkOnClose ¶
func (ln *UnixListener) SetUnlinkOnClose(unlink bool)
func (*UnixListener) SyscallConn ¶
func (ln *UnixListener) SyscallConn() (syscall.RawConn, error)
Source Files
¶
Click to show internal directories.
Click to hide internal directories.