pollio

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultReadBufferSize .
	DefaultReadBufferSize = 1024 * 64

	// DefaultMaxWriteBufferSize .
	DefaultMaxWriteBufferSize = 1024 * 1024

	// DefaultMaxConnReadTimesPerEventLoop .
	DefaultMaxConnReadTimesPerEventLoop = 3

	// DefaultUDPReadTimeout .
	DefaultUDPReadTimeout = 120 * time.Second
)
View Source
const (
	// EPOLLLT .
	EPOLLLT = 0

	// EPOLLET .
	EPOLLET = 0x80000000

	// EPOLLONESHOT .
	EPOLLONESHOT = syscall.EPOLLONESHOT
)
View Source
const (
	IPPROTO_TCP   = syscall.IPPROTO_TCP
	TCP_KEEPINTVL = syscall.TCP_KEEPINTVL
	TCP_KEEPIDLE  = syscall.TCP_KEEPIDLE
)

Variables

View Source
var (
	// MaxOpenFiles .
	MaxOpenFiles = 1024 * 1024 * 2
)

Functions

This section is empty.

Types

type Config

type Config struct {
	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

	// 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 16k by default.
	ReadBufferSize int

	// MaxWriteBufferSize represents max write buffer size for Conn, it's set to 1m by default.
	MaxWriteBufferSize int

	// MaxConnReadTimesPerEventLoop represents max read times in one poller loop for one fd
	MaxConnReadTimesPerEventLoop int

	// LockListener represents listener's goroutine to lock thread or not, it's set to false by default.
	LockListener bool

	// LockPoller represents poller's goroutine to lock thread or not, it's set to false by default.
	LockPoller bool

	// EpollMod sets the epoll mod, EPOLLLT by default.
	EpollMod uint32

	// EPOLLONESHOT .
	EPOLLONESHOT uint32

	// UDPReadTimeout sets the timeout for udp sessions.
	UDPReadTimeout time.Duration

	// TimerExecute sets the executor for timer callbacks.
	TimerExecute func(f func())

	// Listen is used to create listener for Engine.
	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)
}

Config Of Engine.

type Conn

type Conn struct {
	ReadBuffer []byte

	DataHandler func(c *Conn, data []byte)
	// contains filtered or unexported fields
}

Conn implements net.Conn.

func Dial

func Dial(network string, address string) (*Conn, error)

Dial wraps net.Dial.

func DialTimeout

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

DialTimeout wraps net.DialTimeout.

func EPConn

func EPConn(conn net.Conn) (*Conn, error)

func (*Conn) Close

func (c *Conn) Close() error

Close implements Close.

func (*Conn) CloseWithError

func (c *Conn) CloseWithError(err error) error

CloseWithError .

func (*Conn) Execute

func (c *Conn) Execute(f func()) bool

Execute .

func (*Conn) ExecuteLen

func (c *Conn) ExecuteLen() int

ExecuteLen .

func (*Conn) Hash

func (c *Conn) Hash() int

Hash returns a hash code.

func (*Conn) IsClosed

func (c *Conn) IsClosed() (bool, error)

IsClosed .

func (*Conn) IsTCP

func (c *Conn) IsTCP() bool

IsTCP .

func (*Conn) IsUDP

func (c *Conn) IsUDP() bool

IsUDP .

func (*Conn) IsUnix

func (c *Conn) IsUnix() bool

IsUnix .

func (*Conn) LocalAddr

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

LocalAddr implements LocalAddr.

func (*Conn) Lock

func (c *Conn) Lock()

Lock .

func (*Conn) MustExecute

func (c *Conn) MustExecute(f func())

MustExecute .

func (*Conn) OnData

func (c *Conn) OnData(h func(conn *Conn, data []byte))

OnData registers callback for data.

func (*Conn) Read

func (c *Conn) Read(b []byte) (int, error)

Read implements Read.

func (*Conn) ReadAndGetConn

func (c *Conn) ReadAndGetConn(b []byte) (*Conn, int, error)

ReadAndGetConn .

func (*Conn) ReadUDP

func (c *Conn) ReadUDP(b []byte) (*Conn, int, error)

ReadUDP .

func (*Conn) RemoteAddr

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

RemoteAddr implements RemoteAddr.

func (*Conn) ResetPollerEvent

func (c *Conn) ResetPollerEvent()

func (*Conn) Session

func (c *Conn) Session() interface{}

Session returns user session.

func (*Conn) SetDeadline

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

SetDeadline implements SetDeadline.

func (*Conn) SetKeepAlive

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

SetKeepAlive implements SetKeepAlive.

func (*Conn) SetKeepAlivePeriod

func (c *Conn) SetKeepAlivePeriod(d time.Duration) error

SetKeepAlivePeriod implements SetKeepAlivePeriod.

func (*Conn) SetLinger

func (c *Conn) SetLinger(onoff int32, linger int32) error

SetLinger implements SetLinger.

func (*Conn) SetNoDelay

func (c *Conn) SetNoDelay(nodelay bool) error

SetNoDelay implements SetNoDelay.

func (*Conn) SetReadBuffer

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

SetReadBuffer implements SetReadBuffer.

func (*Conn) SetReadDeadline

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

SetReadDeadline implements SetReadDeadline.

func (*Conn) SetSession

func (c *Conn) SetSession(session interface{})

SetSession sets user session.

func (*Conn) SetWriteBuffer

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

SetWriteBuffer implements SetWriteBuffer.

func (*Conn) SetWriteDeadline

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

SetWriteDeadline implements SetWriteDeadline.

func (*Conn) Type

func (c *Conn) Type() ConnType

Type .

func (*Conn) Unlock

func (c *Conn) Unlock()

Unlock .

func (*Conn) Write

func (c *Conn) Write(b []byte) (int, error)

Write implements Write.

func (*Conn) Writev

func (c *Conn) Writev(in [][]byte) (int, error)

Writev implements Writev.

type ConnType

type ConnType = int8

ConnType .

const (
	// ConnTypeTCP .
	ConnTypeTCP ConnType = iota + 1
	// ConnTypeUDPServer .
	ConnTypeUDPServer
	// ConnTypeUDPClientFromRead .
	ConnTypeUDPClientFromRead
	// ConnTypeUDPClientFromDial .
	ConnTypeUDPClientFromDial
	// ConnTypeUnix .
	ConnTypeUnix
)

type Engine

type Engine struct {
	*timer.Timer
	sync.WaitGroup

	Name string

	Execute      func(f func())
	TimerExecute func(f func())
	// contains filtered or unexported fields
}

Engine is a manager of poller.

func NewEngine

func NewEngine(conf Config) *Engine

NewEngine is a factory impl.

func (*Engine) AddConn

func (g *Engine) AddConn(conn net.Conn) (*Conn, error)

AddConn adds conn to a poller.

func (*Engine) AfterRead

func (g *Engine) AfterRead(h func(c *Conn))

AfterRead registers callback after syscall.Read the handler would be called on *nix.

func (*Engine) BeforeRead

func (g *Engine) BeforeRead(h func(c *Conn))

BeforeRead registers callback before syscall.Read the handler would be called on windows.

func (*Engine) BeforeWrite

func (g *Engine) BeforeWrite(h func(c *Conn))

BeforeWrite registers callback befor syscall.Write and syscall.Writev the handler would be called on windows.

func (*Engine) OnClose

func (g *Engine) OnClose(h func(c *Conn, err error))

OnClose registers callback for disconnected.

func (*Engine) OnData

func (g *Engine) OnData(h func(c *Conn, data []byte))

OnData registers callback for data.

func (*Engine) OnOpen

func (g *Engine) OnOpen(h func(c *Conn))

OnOpen registers callback for new connection.

func (*Engine) OnRead

func (g *Engine) OnRead(h func(c *Conn))

OnRead registers callback for reading event.

func (*Engine) OnReadBufferAlloc

func (g *Engine) OnReadBufferAlloc(h func(c *Conn) []byte)

OnReadBufferAlloc registers callback for memory allocating.

func (*Engine) OnReadBufferFree

func (g *Engine) OnReadBufferFree(h func(c *Conn, b []byte))

OnReadBufferFree registers callback for memory release.

func (*Engine) OnStop

func (g *Engine) OnStop(h func())

OnStop registers callback before Engine is stopped.

func (*Engine) PollerBuffer

func (g *Engine) PollerBuffer(c *Conn) []byte

PollerBuffer returns Poller's buffer by Conn, can be used on linux/bsd.

func (*Engine) Shutdown

func (g *Engine) Shutdown(ctx context.Context) error

Shutdown stops Engine gracefully with context.

func (*Engine) Start

func (g *Engine) Start() error

Start init and start pollers.

func (*Engine) Stop

func (g *Engine) Stop()

Stop closes listeners/pollers/conns/timer.

type Gopher

type Gopher = Engine

Gopher keeps old type to compatible with new name Engine.

func NewGopher

func NewGopher(conf Config) *Gopher

Jump to

Keyboard shortcuts

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