README
¶
RIO
基于IOURING
的AIO
网络库,非CGO
方式,且遵循标准库使用设计模式。
支持协议:TCP
、UDP
、UNIX
、UNIXGRAM
(IP
为代理标准库)。
Linux 内核版本需要>= 5.14
,推荐版本为>= 6.1
。
性能
测试环境:Win11(WSL2)、内核(6.6.36.6-microsoft-standard-WSL2)、CPU(13600K)。
基于默认参数的测试,10线程,每线程1000链接,共计10000链接。
RIO 相比 STD(标准库)约快13%,详见 Benchmark 。
注意:CurveWaitTransmission 在不同环境下的性能体现是不同的,需按需调整来发挥出高效的性能。
------ Benchmark ------
Port: 9000
Workers: 10
Count: 1000
NBytes: 1024
ECHO-RIO benching complete(1.564700361s): 6391 conn/sec, 6.2M inbounds/sec, 6.2M outbounds/sec, 0 failures
ECHO-STD benching complete(1.821161901s): 5491 conn/sec, 5.4M inbounds/sec, 5.4M outbounds/sec, 0 failures
HTTP-RIO benching complete(1.722059583s): 5807 conn/sec, 5.8M inbounds/sec, 5.8M outbounds/sec, 0 failures
HTTP-STD benching complete(1.948937829s): 5131 conn/sec, 5M inbounds/sec, 5M outbounds/sec, 0 failures
使用
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(...)
进阶调参
//go:build linux
package main
import (
"github.com/brickingsoft/rio"
)
func setup() {
// 设置进程等级
rio.UseProcessPriority()
// 设置 IOURING 的大小
rio.UseEntries()
// 设置 IOURING 的 Flags
rio.UseFlags()
// 设置 IOURING 的 Features
rio.UseFeatures()
// 设置IOURING个数
// 不是越多越好,与 CPU 数量相关,默认是 CPU 数量的1/8。
rio.UseVortexNum(1)
// 设置IOURING加载平衡器
rio.UseLoadBalancer()
// 设置任务准备批大小
// 默认是任务数
rio.UsePrepareBatchSize()
// 设置完成事件等待变速器构建器
// 这与性能息息相关,调整变速器实现不同的性能。
// 默认是曲线变速器,
rio.UseWaitTransmissionBuilder()
// 设置从可读方读数据策略
rio.UseReadFromFilePolicy()
// 使用爆操性能模式
rio.UsePreformMode()
}
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 Unpin() (err error)
- func UseCPUAffinity(use bool)
- func UseEntries(entries int)
- func UseFeatures(feats uint32)
- func UseFlags(flags uint32)
- func UseLoadBalancer(lb aio.LoadBalancer)
- func UsePreformMode()
- func UsePrepareBatchSize(size uint32)
- func UseProcessPriority(level process.PriorityLevel)
- func UseReadFromFilePolicy(policy int32)
- func UseVortexNum(n int)
- func UseWaitTransmissionBuilder(builder aio.TransmissionBuilder)
- 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)
- 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 钉住 aio.Vortexes 。 一般用于程序启动时。 这用手动管理 aio.Vortexes 的生命周期,一般用于只有 Dial 的使用。 注意:必须 Unpin 来关闭 aio.Vortexes 。
func UseCPUAffinity ¶ added in v0.7.0
func UseCPUAffinity(use bool)
func UseEntries ¶
func UseEntries(entries int)
func UseFeatures ¶
func UseFeatures(feats uint32)
func UseLoadBalancer ¶ added in v0.9.0
func UseLoadBalancer(lb aio.LoadBalancer)
func UsePreformMode ¶
func UsePreformMode()
func UsePrepareBatchSize ¶
func UsePrepareBatchSize(size uint32)
func UseProcessPriority ¶
func UseProcessPriority(level process.PriorityLevel)
func UseReadFromFilePolicy ¶
func UseReadFromFilePolicy(policy int32)
func UseVortexNum ¶ added in v0.9.0
func UseVortexNum(n int)
func UseWaitTransmissionBuilder ¶
func UseWaitTransmissionBuilder(builder aio.TransmissionBuilder)
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.