rio

package module
v1.5.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2025 License: LGPL-3.0 Imports: 18 Imported by: 0

README

RIO

基于IOURINGAIO网络库,非CGO方式,且遵循标准库使用设计模式。

支持协议:TCPUDPUNIXUNIXGRAMIP为代理标准库)。

RIO 是遵循标准库使用方式的,是可以非常方便的投入使用,所以它不是个玩具。

Linux 内核版本需要>= 5.19,推荐版本为>= 6.6

特性

  • 基于 IOURING 的实现
  • 基于 net.Listener net.Connnet.PacketConn 的实现
  • 使用批处理方式来减少系统调用的开销
  • 支持 TLS
  • 支持 FIXED BUFFERFIXED FILE
  • 支持 SEND_ZCSENDMSG_ZC

性能

echo benchmark echo benchmark
详细信息

使用 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 24043.6 稳定在23000 100 %
EVIO 19010.4 稳定在19000 79 %
GNET 18598.8 稳定在18000 77 %
NET 14586.9 稳定在14000 60 %
明细输出
------ 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:     245.0 MiB (256897392 bytes)
Total data received: 244.2 MiB (256063278 bytes)
Bandwidth per channel: 8.202⇅ Mbps (1025.3 kBps)
Aggregate bandwidth: 204.723↓, 205.390↑ Mbps
Packet rate estimate: 24043.6↓, 17801.4↑ (3↓, 25↑ 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:     182.6 MiB (191496192 bytes)
Total data received: 181.1 MiB (189878896 bytes)
Bandwidth per channel: 6.100⇅ Mbps (762.5 kBps)
Aggregate bandwidth: 151.862↓, 153.156↑ Mbps
Packet rate estimate: 19010.4↓, 13192.0↑ (3↓, 44↑ TCP MSS/op)
Test duration: 10.0027 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:     183.8 MiB (192741376 bytes)
Total data received: 182.3 MiB (191161224 bytes)
Bandwidth per channel: 6.136⇅ Mbps (767.0 kBps)
Aggregate bandwidth: 152.776↓, 154.039↑ Mbps
Packet rate estimate: 18598.8↓, 13340.6↑ (3↓, 44↑ TCP MSS/op)
Test duration: 10.01 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:     183.4 MiB (192282624 bytes)
Total data received: 181.7 MiB (190500400 bytes)
Bandwidth per channel: 6.119⇅ Mbps (764.9 kBps)
Aggregate bandwidth: 152.274↓, 153.698↑ Mbps
Packet rate estimate: 14586.9↓, 13171.4↑ (2↓, 44↑ TCP MSS/op)
Test duration: 10.0083 s.
C50 R5k

50链接重复5000次。

tcpkali --workers 1 -c 50 -r 5k -m "PING" 192.168.100.120:9000

结果:

种类 速率 (pps) 说明 性能
RIO 44138.9 稳定在44000 100 %
EVIO 29327.7 稳定在29000 66 %
GNET 28936.6 稳定在29000 65 %
NET 28394.5 稳定在28000 64 %
明细输出
------ 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:     9.6 MiB (10019512 bytes)
Total data received: 9.6 MiB (10019132 bytes)
Bandwidth per channel: 0.320⇅ Mbps (40.0 kBps)
Aggregate bandwidth: 8.010↓, 8.010↑ Mbps
Packet rate estimate: 44138.9↓, 44153.1↑ (1↓, 1↑ TCP MSS/op)
Test duration: 10.0069 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:     9.5 MiB (10011756 bytes)
Total data received: 9.5 MiB (10011756 bytes)
Bandwidth per channel: 0.320⇅ Mbps (40.0 kBps)
Aggregate bandwidth: 8.009↓, 8.009↑ Mbps
Packet rate estimate: 29327.7↓, 29375.9↑ (1↓, 1↑ TCP MSS/op)
Test duration: 10.0011 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:     9.5 MiB (10011192 bytes)
Total data received: 9.5 MiB (10011192 bytes)
Bandwidth per channel: 0.320⇅ Mbps (40.0 kBps)
Aggregate bandwidth: 8.008↓, 8.008↑ Mbps
Packet rate estimate: 28936.6↓, 28957.4↑ (1↓, 1↑ TCP MSS/op)
Test duration: 10.0007 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:     9.5 MiB (10011720 bytes)
Total data received: 9.5 MiB (10011720 bytes)
Bandwidth per channel: 0.320⇅ Mbps (40.0 kBps)
Aggregate bandwidth: 8.009↓, 8.009↑ Mbps
Packet rate estimate: 28394.5↓, 28431.6↑ (1↓, 1↑ TCP MSS/op)
Test duration: 10.0008 s.

使用

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)
Pin 与 Unpin

一般用于只有 Dial 场景。或者有多个 Listen 的场景。

因为单一 Listen 的生命周期往往和程序是一致的,所以 IOURING 实例的生命周期是与程序是一致的。

Dial 的生命周期是短的,往往是频繁 Dial ,多个 Listen 意味着生命周期是与程序是不一致的。

所以需要 PinUnpin IOURING 实例,而不是频繁启停。

// 程序启动位置
err := rio.Pin()
// 程序退出位置
err := rio.Unpin()
Config

rio.ListenConfignet.ListenConfig 是类似的,通过配置来监听。


config := rio.ListenConfig{
    Control:            nil,                     // 设置控制器
    KeepAlive:          0,                       // 设置 KeepAlive 时长
    KeepAliveConfig:    net.KeepAliveConfig{},   // 设置 KeepAlive 详细配置
    MultipathTCP:       false,                   // 是否多路TCP模式
    FastOpen:           false,                   // 是否快速打开
    QuickAck:           false,                   // 是否快速应答
    ReusePort:          false,                   // 是否重用端口(同时开启cBPF)
    SendZC:             false,                   // 是否使用 Zero-Copy 方式发送(某些场景会遥测不到但其实是发送了,如 TCPKALI)
    MultishotAccept:    false,                   // 是否单投多发模式来接受链接
    AutoFixedFdInstall: false,                   // 是否启用接受到的链接进行自动安装描述符(超出后会退回到非注册模式)
}
ln, lnErr := config.Listen(context.Background(), "tcp", ":9000")

rio.Dialernet.Dialer 是类似的,通过配置来拨号。

dialer := rio.Dialer{
    Timeout:            0,                          // 超时
    Deadline:           time.Time{},                // 死期
    KeepAlive:          0,                          // 设置 KeepAlive 时长
    KeepAliveConfig:    net.KeepAliveConfig{},      // 设置 KeepAlive 详细配置
    LocalAddr:          nil,                        // 本地地址
    MultipathTCP:       false,                      // 是否多路TCP模式
    FastOpen:           false,                      // 是否快速打开
    QuickAck:           false,                      // 是否快速应答
    SendZC:             false,                      // 是否使用 Zero-Copy 方式发送(某些场景会遥测不到但其实是发送了,如 TCPKALI)
    AutoFixedFdInstall: false,                      // 是否启用接受到的链接进行自动安装描述符(超出后会退回到非注册模式)
    Control:            nil,                        // 设置控制器
    ControlContext:     nil,                        // 设置带上下文的控制器
}
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 文本 标识方案,DEFAULTPERFORMANCE
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 数字 设置注册固定描述符,当大于软上限时,会使用软上线值。
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_ISSUERAutoFixedFdInstall 冲突。
  • RIO_IOURING_SETUP_FLAGS_SCHEMA 优先级低于 RIO_IOURING_SETUP_FLAGS
    • DEFAULTIORING_SETUP_COOP_TASKRUN
    • PERFORMANCEIORING_SETUP_SQPOLLIORING_SETUP_SQ_AFF ,所以非常吃配置,但是会减少系统调用。
  • RIO_IOURING_REG_FIXED_BUFFERSrio.FixedReaderWriter 的前置必要条件,如果使用固定读写,必须设置该变量来注册。
  • RIO_IOURING_REG_FIXED_FILESrio.FixedReaderWriterrio.FixedFdAutoFixedFdInstall 的前置必要条件,如果使用固定文件,必须设置该变量来注册。
  • RIO_PREP_SQE_BATCH_IDLE_TIME 是用来处理忙等待问题,所以值会大一点。
  • RIO_PREP_SQE_BATCH_TIME_WINDOW 是批处理窗口,过小和过大都会影响性能。
  • RIO_WAIT_CQE_BATCH_TIME_CURVE 的第一个节点的时长建议大一些,太小会引发忙等待,后续节点会具体影响性能。

Documentation

Index

Constants

View Source
const (
	ReadFromFileUseMMapPolicy = int32(iota)
	ReadFromFileUseMixPolicy
)

Variables

View Source
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

func Dial(network string, address string) (net.Conn, error)

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

func DialContext(ctx context.Context, network string, address string) (net.Conn, error)

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

func DialTimeout(network string, address string, timeout time.Duration) (net.Conn, error)

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

func Listen(network string, addr string) (ln net.Listener, err error)

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 Pin

func Pin() error

Pin usually used during program startup. such as only use Dial case, Pin before Dial and Unpin when program exit.

func Presets added in v1.5.6

func Presets(options ...aio.Option)

Presets preset aio options, must be called before Pin, Dial and Listen.

func Unpin

func Unpin() error

Unpin usually used during program exit.

func UseProcessPriority

func UseProcessPriority(level process.PriorityLevel)

UseProcessPriority set process priority

Types

type Dialer

type Dialer struct {
	Timeout            time.Duration
	Deadline           time.Time
	KeepAlive          time.Duration
	KeepAliveConfig    net.KeepAliveConfig
	LocalAddr          net.Addr
	MultipathTCP       bool
	FastOpen           bool
	QuickAck           bool
	SendZC             bool
	AutoFixedFdInstall bool
	Control            func(network, address string, c syscall.RawConn) error
	ControlContext     func(ctx context.Context, network, address string, c syscall.RawConn) error
}

func (*Dialer) Dial

func (d *Dialer) Dial(network string, address string) (c net.Conn, err error)

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

func (d *Dialer) DialContext(ctx context.Context, network, address string) (c net.Conn, err error)

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

func (d *Dialer) SetAutoFixedFdInstall(auto bool)

SetAutoFixedFdInstall set auto install fixed fd.

auto install fixed fd when connected. available after [RIO_IOURING_REG_FIXED_FILES] set.

func (*Dialer) SetFastOpen

func (d *Dialer) SetFastOpen(use bool)

SetFastOpen set fast open.

func (*Dialer) SetMultipathTCP

func (d *Dialer) SetMultipathTCP(use bool)

SetMultipathTCP set multi-path tcp.

func (*Dialer) SetQuickAck

func (d *Dialer) SetQuickAck(use bool)

SetQuickAck set quick ack.

func (*Dialer) SetSendZC added in v1.2.0

func (d *Dialer) SetSendZC(use bool)

SetSendZC set send zero-copy.

available after 6.0

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

func ConvertToFixedFd(v any) (fixed FixedFd, ok bool)

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

type IPConn struct {
	*net.IPConn
}

IPConn is the implementation of the net.Conn and net.PacketConn interfaces for IP network connections.

func DialIP

func DialIP(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 ListenIP

func ListenIP(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.

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
}

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)

func (*ListenConfig) SetFastOpen

func (lc *ListenConfig) SetFastOpen(use bool)

func (*ListenConfig) SetMultipathTCP

func (lc *ListenConfig) SetMultipathTCP(use bool)

func (*ListenConfig) SetMultishotAccept added in v1.5.6

func (lc *ListenConfig) SetMultishotAccept(multi 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
}

TCPConn is an implementation of the net.Conn interface for TCP network connections.

func DialTCP

func DialTCP(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 (*TCPConn) AcquireRegisteredBuffer added in v1.3.0

func (c *TCPConn) AcquireRegisteredBuffer() *aio.FixedBuffer

AcquireRegisteredBuffer implements the FixedReaderWriter AcquireRegisteredBuffer method.

func (*TCPConn) Close

func (c *TCPConn) Close() error

Close implements the net.Conn Close method.

func (*TCPConn) CloseRead

func (c *TCPConn) CloseRead() error

CloseRead shuts down the reading side of the TCP connection. Most callers should just use Close.

func (*TCPConn) CloseWrite

func (c *TCPConn) CloseWrite() error

CloseWrite shuts down the writing side of the TCP connection. Most callers should just use Close.

func (*TCPConn) Context

func (c *TCPConn) Context() context.Context

Context get context of conn.

func (*TCPConn) File

func (c *TCPConn) 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 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) LocalAddr

func (c *TCPConn) LocalAddr() net.Addr

LocalAddr implements the net.Conn LocalAddr method.

func (*TCPConn) MultipathTCP

func (c *TCPConn) MultipathTCP() (bool, error)

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) Read

func (c *TCPConn) Read(b []byte) (n int, err error)

Read implements the net.Conn Read method.

func (*TCPConn) ReadBuffer

func (c *TCPConn) ReadBuffer() (int, error)

ReadBuffer get SO_RCVBUF.

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

func (c *TCPConn) ReadFrom(r io.Reader) (int64, error)

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

func (c *TCPConn) RemoteAddr() net.Addr

RemoteAddr implements the net.Conn RemoteAddr method.

func (*TCPConn) SetDeadline

func (c *TCPConn) SetDeadline(t time.Time) error

SetDeadline implements the net.Conn SetDeadline method.

func (*TCPConn) SetKeepAlive

func (c *TCPConn) SetKeepAlive(keepalive bool) error

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

func (c *TCPConn) SetKeepAlivePeriod(period time.Duration) error

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

func (c *TCPConn) SetLinger(sec int) error

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

func (c *TCPConn) SetNoDelay(noDelay bool) error

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

func (c *TCPConn) SetReadBuffer(bytes int) error

SetReadBuffer set SO_RCVBUF.

func (*TCPConn) SetReadDeadline

func (c *TCPConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements the net.Conn SetReadDeadline method.

func (*TCPConn) SetReadFromFilePolicy added in v1.5.6

func (c *TCPConn) SetReadFromFilePolicy(policy int32)

func (*TCPConn) SetWriteBuffer

func (c *TCPConn) SetWriteBuffer(bytes int) error

SetWriteBuffer set SO_SNDBUF.

func (*TCPConn) SetWriteDeadline

func (c *TCPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the net.Conn SetWriteDeadline method.

func (*TCPConn) SyscallConn

func (c *TCPConn) SyscallConn() (syscall.RawConn, error)

SyscallConn returns a raw network connection. This implements the syscall.Conn interface.

func (*TCPConn) UseSendZC added in v1.5.6

func (c *TCPConn) UseSendZC(use bool) bool

UseSendZC try to enable send_zc.

func (*TCPConn) Write

func (c *TCPConn) Write(b []byte) (n int, err error)

Write implements the net.Conn Write method.

func (*TCPConn) WriteBuffer

func (c *TCPConn) WriteBuffer() (int, error)

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.

func (*TCPConn) WriteTo

func (c *TCPConn) WriteTo(w io.Writer) (int64, error)

WriteTo implements the io.WriterTo WriteTo 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

func DialUDP(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 ListenMulticastUDP

func ListenMulticastUDP(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 ListenUDP

func ListenUDP(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 (*UDPConn) AcquireRegisteredBuffer added in v1.3.0

func (c *UDPConn) AcquireRegisteredBuffer() *aio.FixedBuffer

AcquireRegisteredBuffer implements the FixedReaderWriter AcquireRegisteredBuffer method.

func (*UDPConn) Close

func (c *UDPConn) Close() error

Close implements the net.Conn Close method.

func (*UDPConn) Context

func (c *UDPConn) Context() context.Context

Context get context of conn.

func (*UDPConn) File

func (c *UDPConn) 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 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) LocalAddr

func (c *UDPConn) LocalAddr() net.Addr

LocalAddr implements the net.Conn LocalAddr method.

func (*UDPConn) Read

func (c *UDPConn) Read(b []byte) (n int, err error)

Read implements the net.Conn Read method.

func (*UDPConn) ReadBuffer

func (c *UDPConn) ReadBuffer() (int, error)

ReadBuffer get SO_RCVBUF.

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

func (c *UDPConn) ReadFrom(b []byte) (n int, addr net.Addr, err error)

ReadFrom implements the net.PacketConn ReadFrom method.

func (*UDPConn) ReadFromUDP

func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)

ReadFromUDP acts like UDPConn.ReadFrom but returns a net.UDPAddr.

func (*UDPConn) ReadFromUDPAddrPort

func (c *UDPConn) ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error)

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

func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error)

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

func (c *UDPConn) RemoteAddr() net.Addr

RemoteAddr implements the net.Conn RemoteAddr method.

func (*UDPConn) SetDeadline

func (c *UDPConn) SetDeadline(t time.Time) error

SetDeadline implements the net.Conn SetDeadline method.

func (*UDPConn) SetReadBuffer

func (c *UDPConn) SetReadBuffer(bytes int) error

SetReadBuffer set SO_RCVBUF.

func (*UDPConn) SetReadDeadline

func (c *UDPConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements the net.Conn SetReadDeadline method.

func (*UDPConn) SetWriteBuffer

func (c *UDPConn) SetWriteBuffer(bytes int) error

SetWriteBuffer set SO_SNDBUF.

func (*UDPConn) SetWriteDeadline

func (c *UDPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the net.Conn SetWriteDeadline method.

func (*UDPConn) SyscallConn

func (c *UDPConn) SyscallConn() (syscall.RawConn, error)

SyscallConn returns a raw network connection. This implements the syscall.Conn interface.

func (*UDPConn) UseSendMSGZC added in v1.5.6

func (c *UDPConn) UseSendMSGZC(use bool) bool

UseSendMSGZC try to enable sendmsg_zc.

func (*UDPConn) UseSendZC added in v1.5.6

func (c *UDPConn) UseSendZC(use bool) bool

UseSendZC try to enable send_zc.

func (*UDPConn) Write

func (c *UDPConn) Write(b []byte) (n int, err error)

Write implements the net.Conn Write method.

func (*UDPConn) WriteBuffer

func (c *UDPConn) WriteBuffer() (int, error)

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

func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)

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

func (c *UDPConn) WriteMsgUDPAddrPort(b, oob []byte, addr netip.AddrPort) (n, oobn int, err error)

WriteMsgUDPAddrPort is like UDPConn.WriteMsgUDP but takes a netip.AddrPort instead of a net.UDPAddr.

func (*UDPConn) WriteTo

func (c *UDPConn) WriteTo(b []byte, addr net.Addr) (n int, err error)

WriteTo implements the net.PacketConn WriteTo method.

func (*UDPConn) WriteToUDP

func (c *UDPConn) WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)

WriteToUDP acts like UDPConn.WriteTo but takes a [UDPAddr].

func (*UDPConn) WriteToUDPAddrPort

func (c *UDPConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (n int, err error)

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

func DialUnix(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 ListenUnixgram

func ListenUnixgram(network string, addr *net.UnixAddr) (*UnixConn, error)

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) Context

func (c *UnixConn) Context() context.Context

Context get context of conn.

func (*UnixConn) File

func (c *UnixConn) 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 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) LocalAddr

func (c *UnixConn) LocalAddr() net.Addr

LocalAddr implements the net.Conn LocalAddr method.

func (*UnixConn) Read

func (c *UnixConn) Read(b []byte) (n int, err error)

Read implements the net.Conn Read method.

func (*UnixConn) ReadBuffer

func (c *UnixConn) ReadBuffer() (int, error)

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

func (c *UnixConn) ReadFrom(b []byte) (int, net.Addr, error)

ReadFrom implements the net.PacketConn ReadFrom method.

func (*UnixConn) ReadFromUnix

func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *net.UnixAddr, err error)

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

func (c *UnixConn) RemoteAddr() net.Addr

RemoteAddr implements the net.Conn RemoteAddr method.

func (*UnixConn) SetDeadline

func (c *UnixConn) SetDeadline(t time.Time) error

SetDeadline implements the net.Conn SetDeadline method.

func (*UnixConn) SetReadBuffer

func (c *UnixConn) SetReadBuffer(bytes int) error

SetReadBuffer set SO_RCVBUF.

func (*UnixConn) SetReadDeadline

func (c *UnixConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements the net.Conn SetReadDeadline method.

func (*UnixConn) SetWriteBuffer

func (c *UnixConn) SetWriteBuffer(bytes int) error

SetWriteBuffer set SO_SNDBUF.

func (*UnixConn) SetWriteDeadline

func (c *UnixConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the net.Conn SetWriteDeadline method.

func (*UnixConn) SyscallConn

func (c *UnixConn) SyscallConn() (syscall.RawConn, error)

SyscallConn returns a raw network connection. This implements the syscall.Conn interface.

func (*UnixConn) UseSendMSGZC added in v1.5.6

func (c *UnixConn) UseSendMSGZC(use bool) bool

UseSendMSGZC try to enable sendmsg_zc.

func (*UnixConn) UseSendZC added in v1.5.6

func (c *UnixConn) UseSendZC(use bool) bool

UseSendZC try to enable send_zc.

func (*UnixConn) Write

func (c *UnixConn) Write(b []byte) (n int, err error)

Write implements the net.Conn Write method.

func (*UnixConn) WriteBuffer

func (c *UnixConn) WriteBuffer() (int, error)

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

func (c *UnixConn) WriteTo(b []byte, addr net.Addr) (int, error)

WriteTo implements the net.PacketConn WriteTo method.

func (*UnixConn) WriteToUnix

func (c *UnixConn) WriteToUnix(b []byte, addr *net.UnixAddr) (int, error)

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.

Directories

Path Synopsis
pkg
sys

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL