Documentation ¶
Index ¶
- Constants
- Variables
- type Config
- type Conn
- func (c *Conn) AsyncRead()
- func (c *Conn) Close() error
- func (c *Conn) CloseWithError(err error) error
- func (c *Conn) DataHandler() func(conn *Conn, data []byte)
- func (c *Conn) Execute(job func()) bool
- func (c *Conn) ExecuteLen() int
- func (c *Conn) Hash() int
- func (c *Conn) IsClosed() (bool, error)
- func (c *Conn) IsTCP() bool
- func (c *Conn) IsUDP() bool
- func (c *Conn) IsUnix() bool
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) Lock()
- func (c *Conn) MustExecute(job func())
- func (c *Conn) OnData(h func(conn *Conn, data []byte))
- func (c *Conn) Read(b []byte) (int, error)
- func (c *Conn) ReadAndGetConn(b []byte) (*Conn, int, error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) ResetPollerEvent()
- func (c *Conn) Sendfile(f *os.File, remain int64) (int64, error)
- func (c *Conn) Session() interface{}
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetKeepAlive(keepalive bool) error
- func (c *Conn) SetKeepAlivePeriod(d time.Duration) error
- func (c *Conn) SetLinger(onoff int32, linger int32) error
- func (c *Conn) SetNoDelay(nodelay bool) error
- func (c *Conn) SetReadBuffer(bytes int) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetSession(session interface{})
- func (c *Conn) SetWriteBuffer(bytes int) error
- func (c *Conn) SetWriteDeadline(t time.Time) error
- func (c *Conn) SyscallConn() (syscall.RawConn, error)
- func (c *Conn) Type() ConnType
- func (c *Conn) Unlock()
- func (c *Conn) Write(b []byte) (int, error)
- func (c *Conn) Writev(in [][]byte) (int, error)
- type ConnType
- type Engine
- func (g *Engine) AddConn(conn net.Conn) (*Conn, error)
- func (engine *Engine) DialAsync(network, addr string, onConnected func(*Conn, error)) error
- func (engine *Engine) DialAsyncTimeout(network, addr string, timeout time.Duration, onConnected func(*Conn, error)) error
- func (g *Engine) OnClose(h func(c *Conn, err error))
- func (g *Engine) OnData(h func(c *Conn, data []byte))
- func (g *Engine) OnOpen(h func(c *Conn))
- func (g *Engine) OnRead(h func(c *Conn))
- func (g *Engine) OnReadBufferAlloc(h func(c *Conn) []byte)
- func (g *Engine) OnReadBufferFree(h func(c *Conn, b []byte))
- func (g *Engine) OnStop(h func())
- func (g *Engine) OnUDPListen(h func(c *Conn))
- func (g *Engine) OnWrittenSize(h func(c *Conn, b []byte, n int))
- func (g *Engine) PollerBuffer(c *Conn) []byte
- func (e *Engine) SetETAsyncRead()
- func (e *Engine) SetLTSyncRead()
- func (g *Engine) Shutdown(ctx context.Context) error
- func (g *Engine) Start() error
- func (g *Engine) Stop()
- type Gopher
Constants ¶
const ( // DefaultReadBufferSize . DefaultReadBufferSize = 1024 * 64 // DefaultMaxWriteBufferSize . DefaultMaxWriteBufferSize = 1024 * 1024 // DefaultMaxConnReadTimesPerEventLoop . DefaultMaxConnReadTimesPerEventLoop = 3 // DefaultUDPReadTimeout . DefaultUDPReadTimeout = 120 * time.Second )
const ( NETWORK_TCP = "tcp" NETWORK_TCP4 = "tcp4" NETWORK_TCP6 = "tcp6" NETWORK_UDP = "udp" NETWORK_UDP4 = "udp4" NETWORK_UDP6 = "udp6" NETWORK_UNIX = "unix" NETWORK_UNIXGRAM = "unixgram" NETWORK_UNIXPACKET = "unixpacket" )
const ( // EPOLLLT . EPOLLLT = 0 // EPOLLET . EPOLLET = 0x80000000 // EPOLLONESHOT . EPOLLONESHOT = syscall.EPOLLONESHOT )
const ( IPPROTO_TCP = syscall.IPPROTO_TCP TCP_KEEPINTVL = syscall.TCP_KEEPINTVL TCP_KEEPIDLE = syscall.TCP_KEEPIDLE )
Variables ¶
var ( ErrReadTimeout = errors.New("read timeout") ErrWriteTimeout = errors.New("write timeout") ErrOverflow = errors.New("write overflow") ErrDialTimeout = errors.New("dial timeout") ErrUnsupported = errors.New("unsupported operation") )
var (
// MaxOpenFiles .
MaxOpenFiles = 1024 * 1024 * 2
)
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // Name describes your gopher name for logging, it's set to "NB" by default. Name string // Network is the listening protocol, used with Addrs together. // tcp* supported only by now, there's no plan for other protocol such as udp, // because it's too easy to write udp server/client. Network string // Addrs is the listening addr list for a nbio server. // if it is empty, no listener created, then the Engine is used for client by default. Addrs []string // NPoller represents poller goroutine num, it's set to runtime.NumCPU() by default. NPoller int // ReadBufferSize represents buffer size for reading, it's set to 64k by default. ReadBufferSize int // MaxWriteBufferSize represents max write buffer size for Conn, it's set to 1m by default. // if the connection's Send-Q is full and the data cached by nbio is // more than MaxWriteBufferSize, the connection would be closed by nbio. MaxWriteBufferSize int // MaxConnReadTimesPerEventLoop represents max read times in one poller loop for one fd MaxConnReadTimesPerEventLoop int // LockListener represents whether to lock thread for listener's goroutine, false by default. LockListener bool // LockPoller represents whether to lock thread for poller's goroutine, false by default. LockPoller bool // EpollMod sets the epoll mod, EPOLLLT by default. EpollMod uint32 // EPOLLONESHOT sets EPOLLONESHOT, 0 by default. EPOLLONESHOT uint32 // UDPReadTimeout sets the timeout for udp sessions. UDPReadTimeout time.Duration // Listen is used to create listener for Engine. // Users can set this func to customize listener, such as reuseport. Listen func(network, addr string) (net.Listener, error) // ListenUDP is used to create udp listener for Engine. ListenUDP func(network string, laddr *net.UDPAddr) (*net.UDPConn, error) // AsyncReadInPoller represents how the reading events and reading are handled // by epoll goroutine: // true : epoll goroutine handles the reading events only, another goroutine // pool will handles the reading. // false: epoll goroutine handles both the reading events and the reading. AsyncReadInPoller bool // IOExecute is used to handle the aysnc reading, users can customize it. IOExecute func(f func([]byte)) // BodyAllocator sets the buffer allocator for write cache. BodyAllocator mempool.Allocator }
Config Of Engine.
type Conn ¶
type Conn struct { Unix int64 // contains filtered or unexported fields }
Conn implements net.Conn with non-blocking interfaces.
func DialTimeout ¶
Dial calls net.DialTimeout to make a net.Conn and convert it to *nbio.Conn.
func (*Conn) AsyncRead ¶
func (c *Conn) AsyncRead()
AsyncReadInPoller is used for reading data async.
func (*Conn) CloseWithError ¶
CloseWithError closes connection with user specified error.
func (*Conn) DataHandler ¶
DataHandler returns Conn's data handler.
func (*Conn) Execute ¶
Execute is used to run the job.
How it works: If the job is the head/first of the Conn's job list, it will call the nbio.Engine.Execute to run all the jobs in the job list that include:
- This job
- New jobs that are pushed to the back of the list before this job is done.
- nbio.Engine.Execute returns until there's no more jobs in the job list.
Else if the job is not the head/first of the job list, it will push the job to the back of the job list and wait to be called. This guarantees there's at most one flow or goroutine running job/jobs for each Conn. This guarantees all the jobs are executed in order.
Notice:
- The job wouldn't run or pushed to the back of the job list if the connection is closed.
- nbio.Engine.Execute is handled by a goroutine pool by default, users can customize it.
func (*Conn) ExecuteLen ¶
ExecuteLen returns the length of the Conn's job list.
func (*Conn) MustExecute ¶
func (c *Conn) MustExecute(job func())
MustExecute implements a similar function as Execute did, but will still execute or push the job to the back of the job list no matter whether Conn has been closed, it guarantees the job to be executed. This is used to handle the close event in nbio/nbhttp.
func (*Conn) OnData ¶
OnData registers Conn's data handler. Notice:
- The data readed by the poller is not handled by this Conn's data handler by default.
- The data readed by the poller is handled by nbio.Engine's data handler which is registered by nbio.Engine.OnData by default.
- This Conn's data handler is used to customize your implementation, you can set different data handler for different Conns, and call Conn's data handler in nbio.Engine's data handler. For example: engine.OnData(func(c *nbio.Conn, data byte){ c.DataHandler()(c, data) }) conn1.OnData(yourDatahandler1) conn2.OnData(yourDatahandler2)
func (*Conn) Read ¶
Read . Depracated . It was used to customize users' reading implementation, but better to use `ReadAndGetConn` instead, which can handle different types of connection and returns the consistent connection instance for UDP. Notice: non-blocking interface, should not be used as you use std.
func (*Conn) ReadAndGetConn ¶
ReadAndGetConn handles reading for different types of connection. It returns the real connection:
- For Non-UDP connection, it returns the Conn itself.
- For UDP connection, it may be a UDP Server fd, then it returns consistent Conn for the same socket which has the same local addr and remote addr.
Notice: non-blocking interface, should not be used as you use std.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the remote network address, if known.
func (*Conn) ResetPollerEvent ¶
func (c *Conn) ResetPollerEvent()
func (*Conn) SetDeadline ¶
SetDeadline sets deadline for both read and write. If it is time.Zero, SetDeadline will clear the deadlines.
func (*Conn) SetKeepAlive ¶
SetKeepAlive sets whether the operating system should send keep-alive messages on the connection.
func (*Conn) SetKeepAlivePeriod ¶
SetKeepAlivePeriod sets period between keep-alives.
func (*Conn) SetNoDelay ¶
SetNoDelay controls whether the operating system should delay packet transmission in hopes of sending fewer packets (Nagle's algorithm). The default is true (no delay), meaning that data is sent as soon as possible after a Write.
func (*Conn) SetReadBuffer ¶
SetReadBuffer sets the size of the operating system's receive buffer associated with the connection.
func (*Conn) SetReadDeadline ¶
SetReadDeadline sets the deadline for future Read calls. When the user doesn't update the deadline and the deadline exceeds, the connection will be closed. If it is time.Zero, SetReadDeadline will clear the deadline.
Notice:
- Users should update the read deadline in time.
- For example, call SetReadDeadline whenever a new WebSocket message is received.
func (*Conn) SetSession ¶
func (c *Conn) SetSession(session interface{})
SetSession sets user session.
func (*Conn) SetWriteBuffer ¶
SetWriteBuffer sets the size of the operating system's transmit buffer associated with the connection.
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline sets the deadline for future data writing. If it is time.Zero, SetReadDeadline will clear the deadline.
If the next Write call writes all the data successfully and there's no data left to bewritten, the deadline timer will be cleared automatically; Else when the user doesn't update the deadline and the deadline exceeds, the connection will be closed.
type ConnType ¶
type ConnType = int8
ConnType is used to identify different types of Conn.
const ( // ConnTypeTCP represents TCP Conn. ConnTypeTCP ConnType = iota + 1 // ConnTypeUDPServer represents UDP Conn used as a listener. ConnTypeUDPServer // ConnTypeUDPClientFromRead represents UDP connection that // is sending data to our UDP Server from peer. ConnTypeUDPClientFromRead // ConnTypeUDPClientFromDial represents UDP Conn that is sending // data to other UDP Server from ourselves. ConnTypeUDPClientFromDial // ConnTypeUnix represents Unix Conn. ConnTypeUnix )
type Engine ¶
type Engine struct { Config *timer.Timer sync.WaitGroup Execute func(f func()) // contains filtered or unexported fields }
Engine is a manager of poller.
func (*Engine) DialAsyncTimeout ¶
func (engine *Engine) DialAsyncTimeout(network, addr string, timeout time.Duration, onConnected func(*Conn, error)) error
DialAsync connects asynchrony to the address on the named network with timeout.
func (*Engine) OnReadBufferAlloc ¶
OnReadBufferAlloc registers callback for memory allocating.
func (*Engine) OnReadBufferFree ¶
OnReadBufferFree registers callback for memory release.
func (*Engine) OnStop ¶
func (g *Engine) OnStop(h func())
OnStop registers callback before Engine is stopped.
func (*Engine) OnUDPListen ¶
OnOpen registers callback for new connection.
func (*Engine) OnWrittenSize ¶
OnWrittenSize registers callback for written size. If len(b) is bigger than 0, it represents that it's writing a buffer, else it's operating by Sendfile.
func (*Engine) PollerBuffer ¶
PollerBuffer returns Poller's buffer by Conn, can be used on linux/bsd.