Documentation ¶
Overview ¶
Package tnet provides event loop networking framework.
Index ¶
- Variables
- func EnablePollerGoschedAfterEvent()
- func Listen(network, address string) (net.Listener, error)
- func NumPollers() int
- func SetNumPollers(n int) error
- func Submit(task func()) error
- type BaseConn
- type Conn
- type OnTCPClosed
- type OnTCPOpened
- type OnUDPClosed
- type Option
- func WithFlushWrite(flush bool) Option
- func WithMaxUDPPacketSize(size int) Option
- func WithNonBlocking(nonblock bool) Option
- func WithOnTCPClosed(onTCPClosed OnTCPClosed) Option
- func WithOnTCPOpened(onTCPOpened OnTCPOpened) Option
- func WithOnUDPClosed(onUDPClosed OnUDPClosed) Option
- func WithSafeWrite(safeWrite bool) Option
- func WithTCPFlushWrite(flush bool) Option
- func WithTCPIdleTimeout(idleTimeout time.Duration) Option
- func WithTCPKeepAlive(keepAlive time.Duration) Option
- type Packet
- type PacketConn
- type Service
- type TCPHandler
- type UDPHandler
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultCleanUpThrottle is a default connections number throttle to determine // whether to enable buffer clean up feature. DefaultCleanUpThrottle = 10000 // ErrConnClosed connection is closed. ErrConnClosed = netError{/* contains filtered or unexported fields */} // EAGAIN represents error of not enough data. EAGAIN = netError{/* contains filtered or unexported fields */} )
var MassiveConnections bool
MassiveConnections denotes whether this is under heavy connections scenario.
Functions ¶
func EnablePollerGoschedAfterEvent ¶
func EnablePollerGoschedAfterEvent()
EnablePollerGoschedAfterEvent enables calling runtime.Gosched() after processing of each event during epoll wait handling. This function can only be called inside func init().
func Listen ¶
Listen announces on the local network address. The network must be "tcp", "tcp4", "tcp6".
func SetNumPollers ¶
SetNumPollers is used to set the number of pollers. Generally it is not actively used. Note that n can't be smaller than the current poller numbers.
NOTE: the default poller number is 1.
Types ¶
type BaseConn ¶
type BaseConn interface { // Conn extends net.Conn, just for interface compatibility. net.Conn // Len returns the total length of the readable data in the reader. Len() int // IsActive checks whether the connection is active or not. IsActive() bool // SetNonBlocking sets conn to nonblocking. Read APIs will return EAGAIN when there is no // enough data for reading SetNonBlocking(nonblock bool) // SetFlushWrite sets whether to flush the data or not. // Default value is false. // Deprecated: whether enable this feature is controlled by system automatically. SetFlushWrite(flushWrite bool) // SetMetaData sets metadata. Through this method, users can bind some custom data to a connection. SetMetaData(m any) // GetMetaData gets metadata. GetMetaData() any }
BaseConn is common for stream and packet oriented network connection.
type Conn ¶
type Conn interface { BaseConn // Peek returns the next n bytes without advancing the reader. It waits until it has // read at least n bytes or error occurs such as connection closed or read timeout. // The bytes stop being valid at the next ReadN or Release call. // Zero-Copy API. Peek(n int) ([]byte, error) // Next returns the next n bytes with advancing the reader, It waits until it has // read at least n bytes or error occurs such as connection closed or read timeout. // The bytes stop being valid at the next ReadN or Release call. // Zero-Copy API. Next(n int) ([]byte, error) // Skip the next n bytes and advance the reader. It waits until the underlayer has at // least n bytes or error occurs such as connection closed or read timeout. // Zero-Copy API. Skip(n int) error // Release releases underlayer buffer when using Peek() and Skip() Zero-Copy APIs. Release() // ReadN is similar to Peek(), except that it will copy the n bytes data from the underlayer, // and advance the reader. ReadN(n int) ([]byte, error) // Writev provides multiple data slice write in order. // The default behavior of Write/Writev will hold a reference to the given byte slices p, // therefore if the caller want to reuse byte slice p after calling Write/Writev, the // SetSafeWrite(true) option is required. Writev(p ...[]byte) (int, error) // SetKeepAlive sets keep alive time for tcp connection. // By default, keep alive is turned on with value defaultKeepAlive. // If keepAlive <= 0, keep alive will be turned off. // Otherwise, keep alive value will be round up to seconds. SetKeepAlive(t time.Duration) error // SetOnRequest can set or replace the TCPHandler method for a connection. // Generally, on the server side the handler is set when the connection is established. // On the client side, if necessary, make sure that TCPHandler is set before sending data. SetOnRequest(handle TCPHandler) error // SetOnClosed sets the additional close process for a connection. // Handle is executed when the connection is closed. SetOnClosed(handle OnTCPClosed) error // SetIdleTimeout sets the idle timeout to close connection. SetIdleTimeout(d time.Duration) error // SetSafeWrite sets whether writing on connection is safe or not. // Default is unsafe. // // This option affects the behavior of Write/Writev. // If safeWrite = false: the lifetime of buffers passed into Write/Writev will // be handled by tnet, which means users cannot reuse the buffers after passing // them into Write/Writev. // If safeWrite = true: the given buffers is copied into tnet's own buffer. // Therefore users can reuse the buffers passed into Write/Writev. SetSafeWrite(safeWrite bool) }
Conn is generic for stream oriented network connection.
type OnTCPClosed ¶
OnTCPClosed fires when the tcp connection is closed. In this method, please do not perform read-write operations, because the connection has been closed. But you can still manipulate the MetaData in the connection.
type OnTCPOpened ¶
OnTCPOpened fires when the tcp connection is established.
type OnUDPClosed ¶
type OnUDPClosed func(conn PacketConn) error
OnUDPClosed fires when the udp connection is closed. In this method, please do not perform read-write operations, because the connection has been closed. But you can still manipulate the MetaData in the connection.
type Option ¶
type Option struct {
// contains filtered or unexported fields
}
Option tnet service option.
func WithFlushWrite ¶
WithFlushWrite sets whether use flush write for TCP and UDP connection or not. Default value is false. Deprecated: whether enable this feature is controlled by system automatically.
func WithMaxUDPPacketSize ¶
WithMaxUDPPacketSize sets maximal UDP packet size when receiving UDP packets.
func WithNonBlocking ¶
WithNonBlocking set conn/packconn to nonblocking mode
func WithOnTCPClosed ¶
func WithOnTCPClosed(onTCPClosed OnTCPClosed) Option
WithOnTCPClosed registers the OnTCPClosed method that is fired when tcp connection is closed.
func WithOnTCPOpened ¶
func WithOnTCPOpened(onTCPOpened OnTCPOpened) Option
WithOnTCPOpened registers the OnTCPOpened method that is fired when connection is established.
func WithOnUDPClosed ¶
func WithOnUDPClosed(onUDPClosed OnUDPClosed) Option
WithOnUDPClosed registers the OnUDPClosed method that is fired when udp connection is closed.
func WithSafeWrite ¶
WithSafeWrite sets the value of safeWrite for TCP. Default value is false.
This option affects the behavior of Write/Writev.
If safeWrite = false: the lifetime of buffers passed into Write/Writev will be handled by tnet, which means users cannot reuse the buffers after passing them into Write/Writev. If safeWrite = true: the given buffers is copied into tnet's own buffer. Therefore users can reuse the buffers passed into Write/Writev.
func WithTCPFlushWrite ¶
WithTCPFlushWrite sets whether use flush write for TCP connection or not. Default value is false. Deprecated: whether enable this feature is controlled by system automatically.
func WithTCPIdleTimeout ¶
WithTCPIdleTimeout sets the idle timeout to close tcp connection.
func WithTCPKeepAlive ¶
WithTCPKeepAlive sets the tcp keep alive interval.
type Packet ¶
type Packet interface { // Data returns the data of the packet. Data() ([]byte, error) // Free will release the underlying buffer. // It will recycle the underlying buffer for better performance. // The bytes will be invalid after free, so free it only when it is no longer in use. Free() }
Packet represents a UDP packet, created by PacketConn Zero-Copy API ReadPacket.
type PacketConn ¶
type PacketConn interface { BaseConn // PacketConn extends net.PacketConn, just for interface compatibility. net.PacketConn // ReadPacket reads a packet from the connection, without copying the underlying buffer. // Get the actual data of packet by Packet.Data(). // Please call Packet.Free() when it is unused, free will recycle the underlying buffer // for better performance. // Zero-copy API ReadPacket() (Packet, net.Addr, error) // SetMaxPacketSize sets maximal UDP packet size when receiving UDP packets. SetMaxPacketSize(size int) // SetOnRequest can set or replace the UDPHandler method for a connection. // However, the handler can't be set to nil. // Generally, on the server side the handler is set when the connection is established. // On the client side, if necessary, make sure that UDPHandler is set before sending data. SetOnRequest(handle UDPHandler) error // SetOnClosed sets the additional close process for a connection. // Handle is executed when the connection is closed. SetOnClosed(handle OnUDPClosed) error }
PacketConn is generic for packet oriented network connection.
func DialUDP ¶
func DialUDP(network, address string, timeout time.Duration) (PacketConn, error)
DialUDP connects to the address on the named network within the timeout. Valid networks for DialUDP are "udp", "udp4" (IPv4-only), "udp6" (IPv6-only).
func ListenPackets ¶
func ListenPackets(network, address string, reuseport bool) ([]PacketConn, error)
ListenPackets announces on the local network address. Reuseport sets whether to enable reuseport when creating PacketConns, it will return multiple PacketConn if reuseprot is true. Generally, enabling reuseport can make effective use of multi-core and improve performance.
func NewPacketConn ¶
func NewPacketConn(conn net.PacketConn) (PacketConn, error)
NewPacketConn creates a tnet.PacketConn from net.PacketConn. Note that conn must listen on UDP and make sure that conn implements syscall.Conn.
type Service ¶
type Service interface { // Serve registers a listener and runs blockingly to provide service, including listening to ports, // accepting connections and reading trans data. // Param ctx is used to shutdown the service with all connections gracefully. Serve(ctx context.Context) error }
Service provides startup method to udp/tcp server.
func NewTCPService ¶
NewTCPService creates a tcp Service and binds it to a listener. It is recommended to create listener by func tnet.Listen, otherwise make sure that listener implements syscall.Conn interface.
type syscall.Conn interface { SyscallConn() (RawConn, error) }
func NewUDPService ¶
func NewUDPService(lns []PacketConn, handler UDPHandler, opt ...Option) (Service, error)
NewUDPService creates a udp service. Ensure that all listeners are listening to the same address.
type TCPHandler ¶
TCPHandler fires when the tcp connection receives data.
type UDPHandler ¶
type UDPHandler func(conn PacketConn) error
UDPHandler fires when the udp connection receives data.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
module
|
|
extensions
|
|
websocket
Module
|
|
internal
|
|
asynctimer
Package asynctimer provides asynchronous timer function, which is implemented by time wheel.
|
Package asynctimer provides asynchronous timer function, which is implemented by time wheel. |
autopostpone
Package autopostpone provides utilities to decide whether to postpone write.
|
Package autopostpone provides utilities to decide whether to postpone write. |
buffer
Package buffer provides linked buffers.
|
Package buffer provides linked buffers. |
cache/mcache
Package mcache provides cache for byte slice.
|
Package mcache provides cache for byte slice. |
cache/systype
Package systype provides system type such as unix.Ioves.
|
Package systype provides system type such as unix.Ioves. |
iovec
Package iovec provides utilities to work with unix.Iovec.
|
Package iovec provides utilities to work with unix.Iovec. |
locker
Package locker provides locking utilities.
|
Package locker provides locking utilities. |
poller
Package poller provides event driven polling system to monitor file description events.
|
Package poller provides event driven polling system to monitor file description events. |
poller/event
Package event provides definitions of event data.
|
Package event provides definitions of event data. |
safejob
Package safejob provides functions to call job in a concurrent-safe manner.
|
Package safejob provides functions to call job in a concurrent-safe manner. |
timer
Package timer provides functions of timer.
|
Package timer provides functions of timer. |
Package log provides logging utilities for tnet.
|
Package log provides logging utilities for tnet. |
Package metrics provides a lot of tnet runtime monitoring data, such as monitoring the efficiency of batch reads and writes, which is a good tool for performance tuning.
|
Package metrics provides a lot of tnet runtime monitoring data, such as monitoring the efficiency of batch reads and writes, which is a good tool for performance tuning. |
Package tls provides tls connection utilities.
|
Package tls provides tls connection utilities. |
examples/echo/client
Package main is the main package.
|
Package main is the main package. |
examples/echo/server
Package main is the main package.
|
Package main is the main package. |