epoll

package module
v1.3.11 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2022 License: Apache-2.0 Imports: 11 Imported by: 1

README

Golang high-performance asynchronous TCP using Epoll (only supports Linux)

Example

package main

import (
	"fmt"
	"syscall"
	"time"

	"github.com/gotcp/epoll"
)

// Asynchronous event
func OnAccept(fd int) {
	fmt.Printf("OnAccept -> %d\n", fd)
}

// Asynchronous event
func OnReceive(fd int, msg []byte, n int) {
	// var _, err = epoll.Write(fd, msg)
	var _, err = epoll.WriteWithTimeout(fd, msg, 3*time.Second)
	if err != nil {
		fmt.Printf("OnReceive -> %d, %v\n", fd, err)
	}
}

// Synchronous event. The event will be triggered before closing fd
func OnClose(fd int) {
	fmt.Printf("OnClose -> %d\n", fd)
}

// Asynchronous event
func OnError(fd int, code epoll.ErrorCode, err error) {
	fmt.Printf("OnError -> %d, %d, %v\n", fd, code, err)
}

var ep *epoll.EP

func main() {
	var err error

	var rLimit syscall.Rlimit
	if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
		panic(err)
	}
	rLimit.Cur = rLimit.Max
	if err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
		panic(err)
	}

	// parameters: readBuffer, threads, queueLength
	ep, err = epoll.New(2048, 3000, 4096)
	if err != nil {
		panic(err)
	}
	defer ep.Stop()

	ep.OnReceive = OnReceive // must have
	ep.OnError = OnError     // optional
	ep.OnAccept = OnAccept   // optional
	ep.OnClose = OnClose     // optional

	ep.Start("0.0.0.0", 8001)
}

Documentation

Index

Constants

View Source
const (
	EPOLL_EVENTS          = unix.EPOLLET
	EPOLL_EVENTS_EPOLLIN  = unix.EPOLLIN | unix.EPOLLET
	EPOLL_EVENTS_EPOLLOUT = unix.EPOLLIN | unix.EPOLLOUT | unix.EPOLLET
)
View Source
const (
	DEFAULT_EPOLL_EVENTS        = 4096
	DEFAULT_EPOLL_READ_TIMEOUT  = 6
	DEFAULT_EPOLL_WRITE_TIMEOUT = 6
	DEFAULT_POOL_MULTIPLE       = 6
)
View Source
const (
	SSL_ERROR_TIMEOUT      = -9
	SSL_ERROR_NONE         = int(C.SSL_ERROR_NONE)
	SSL_ERROR_SSL          = int(C.SSL_ERROR_SSL)
	SSL_ERROR_WANT_READ    = int(C.SSL_ERROR_WANT_READ)
	SSL_ERROR_WANT_WRITE   = int(C.SSL_ERROR_WANT_WRITE)
	SSL_ERROR_SYSCALL      = int(C.SSL_ERROR_SYSCALL)
	SSL_ERROR_ZERO_RETURN  = int(C.SSL_ERROR_ZERO_RETURN)
	SSL_ERROR_WANT_CONNECT = int(C.SSL_ERROR_WANT_CONNECT)
)
View Source
const (
	DEFAULT_C_MALLOC_TRIM_INTERVAL = 5000
)

Variables

View Source
var (
	ErrorSSLUnableCreate = errors.New("unable to create SSL connection")
	ErrorSSLUnknow       = errors.New("ssl error unknow")
	ErrorSSLWantWrite    = errors.New("ssl error want write")
	ErrorSSLWantRead     = errors.New("ssl error want read")
	ErrorSSLZeroReturn   = errors.New("ssl error zero return")
	ErrorSSLWantConnect  = errors.New("ssl error want connect")
	ErrorSSLTimeout      = errors.New("ssl error timeout")
	ErrorSSL             = errors.New("ssl error")
	ErrorSSLSyscall      = errors.New("ssl error syscall")
)
View Source
var (
	ErrorGetPoolBuffer = errors.New("get pool buffer error")
)
View Source
var (
	ErrorTemplateNotFound = "%d not found in the list"
)

Functions

func GetSSLError added in v1.3.8

func GetSSLError(errno int) error

func GetSSLErrorNumber added in v1.3.8

func GetSSLErrorNumber(ssl *C.SSL, ret int) int

func Write

func Write(fd int, msg []byte) (int, error)

func WriteWithTimeout added in v1.0.7

func WriteWithTimeout(fd int, msg []byte, timeout time.Duration) (int, error)

Types

type Conn added in v1.2.0

type Conn struct {
	Id         uint64
	Fd         int
	SSL        *SSL
	Data       interface{}
	SequenceId int
	Timestamp  int64
	Status     int
}

type EP added in v1.0.3

type EP struct {
	Host         string
	Port         int
	Epfd         int
	Fd           int
	Connections  *hashmap.HM
	SSLCtx       *C.SSL_CTX
	IsSSL        bool
	Threads      int
	QueueLength  int
	ReadBuffer   int
	WriteBuffer  int
	EpollEvents  int
	WaitTimeout  int
	ReadTimeout  int
	WriteTimeout int
	KeepAlive    int
	ReuseAddr    int
	ReusePort    int

	OnAccept   OnAcceptEvent
	OnReceive  OnReceiveEvent
	OnEpollOut OnEpollOutEvent
	OnClose    OnCloseEvent
	OnError    OnErrorEvent
	// contains filtered or unexported fields
}

func New

func New(readBuffer int, threads int, queueLength int) (*EP, error)

func (*EP) Add added in v1.0.3

func (ep *EP) Add(fd int) error

func (*EP) AddConnection added in v1.2.0

func (ep *EP) AddConnection(fd int, sequenceId int)

func (*EP) AddConnectionSSL added in v1.3.0

func (ep *EP) AddConnectionSSL(fd int, ssl *SSL, sequenceId int)

func (*EP) Close added in v1.1.0

func (ep *EP) Close(fd int) error

func (*EP) CloseAction added in v1.0.3

func (ep *EP) CloseAction(sequenceId int, fd int) error

func (*EP) CloseAll added in v1.2.0

func (ep *EP) CloseAll()

func (*EP) CloseFd added in v1.2.0

func (ep *EP) CloseFd(fd int) error

func (*EP) Delete added in v1.1.0

func (ep *EP) Delete(fd int) error

func (*EP) DeleteConnection added in v1.2.0

func (ep *EP) DeleteConnection(fd int) bool

func (*EP) DestroyConnection added in v1.2.1

func (ep *EP) DestroyConnection(fd int) error

called externally

func (*EP) DisableEpollIn added in v1.3.8

func (ep *EP) DisableEpollIn(fd int) error

func (*EP) DisableEpollOut added in v1.3.8

func (ep *EP) DisableEpollOut(fd int) error

func (*EP) EnableEpollIn added in v1.3.8

func (ep *EP) EnableEpollIn(fd int) error

func (*EP) EnableEpollOut added in v1.3.8

func (ep *EP) EnableEpollOut(fd int) error

func (*EP) EstablishConnection added in v1.2.1

func (ep *EP) EstablishConnection(fd int) error

called externally

func (*EP) GetBuffer added in v1.3.8

func (ep *EP) GetBuffer() (*[]byte, error)

func (*EP) GetConnection added in v1.3.0

func (ep *EP) GetConnection(fd int) *Conn

func (*EP) GetConnectionAndSequenceId added in v1.3.0

func (ep *EP) GetConnectionAndSequenceId(fd int) (*Conn, int)

func (*EP) GetConnectionCount added in v1.3.0

func (ep *EP) GetConnectionCount() int

func (*EP) GetConnectionData added in v1.3.0

func (ep *EP) GetConnectionData(fd int) (interface{}, bool)

func (*EP) GetConnectionSSL added in v1.3.0

func (ep *EP) GetConnectionSSL(fd int) *SSL

func (*EP) GetConnectionSequenceId added in v1.2.0

func (ep *EP) GetConnectionSequenceId(fd int) int

func (*EP) GetConnectionSequenceIdAndSSL added in v1.3.0

func (ep *EP) GetConnectionSequenceIdAndSSL(fd int) (int, *SSL)

func (*EP) GetConnectionStatus added in v1.3.0

func (ep *EP) GetConnectionStatus(fd int) (int, bool)

func (*EP) GetSequenceId added in v1.1.3

func (ep *EP) GetSequenceId() int

func (*EP) InitEpoll added in v1.0.3

func (ep *EP) InitEpoll(host string, port int) error

func (*EP) InvokeAccept added in v1.2.0

func (ep *EP) InvokeAccept()

func (*EP) InvokeClose added in v1.2.0

func (ep *EP) InvokeClose(sequenceId int, fd int)

func (*EP) InvokeEpollOut added in v1.3.8

func (ep *EP) InvokeEpollOut(fd int)

func (*EP) InvokeError added in v1.2.0

func (ep *EP) InvokeError(sequenceId int, fd int, code ErrorCode, err error)

func (*EP) InvokeReceive added in v1.2.0

func (ep *EP) InvokeReceive(sequenceId int, fd int, msg *[]byte, n int)

func (*EP) Listen added in v1.0.6

func (ep *EP) Listen()

pure EPOLL, only listening, needs to use ep.Add(fd)

func (*EP) PutBuffer added in v1.3.8

func (ep *EP) PutBuffer(buffer *[]byte)

func (*EP) SetConnectionData added in v1.3.0

func (ep *EP) SetConnectionData(fd int, data interface{}) bool

func (*EP) SetConnectionStatus added in v1.3.0

func (ep *EP) SetConnectionStatus(fd int, status int) bool

func (*EP) SetEpollEvents added in v1.1.0

func (ep *EP) SetEpollEvents(n int)

func (*EP) SetKeepAlive added in v1.0.3

func (ep *EP) SetKeepAlive(n int)

func (*EP) SetReadTimeout added in v1.2.0

func (ep *EP) SetReadTimeout(n int)

func (*EP) SetReuseAddr added in v1.3.0

func (ep *EP) SetReuseAddr(n int)

func (*EP) SetReusePort added in v1.3.0

func (ep *EP) SetReusePort(n int)

func (*EP) SetWaitTimeout added in v1.1.0

func (ep *EP) SetWaitTimeout(n int)

func (*EP) SetWriteBuffer added in v1.2.0

func (ep *EP) SetWriteBuffer(n int)

func (*EP) SetWriteTimeout added in v1.2.0

func (ep *EP) SetWriteTimeout(n int)

func (*EP) Start added in v1.0.3

func (ep *EP) Start(host string, port int)

pure EPOLL

func (*EP) StartSSL added in v1.3.0

func (ep *EP) StartSSL(host string, port int, certFile string, keyFile string)

func (*EP) Stop added in v1.0.3

func (ep *EP) Stop() error

func (*EP) UpdateConnection added in v1.2.0

func (ep *EP) UpdateConnection(fd int)

func (*EP) UpdateConnectionDataWithFunc added in v1.3.0

func (ep *EP) UpdateConnectionDataWithFunc(fd int, updateDataFunc UpdateDataFunc) bool

func (*EP) WriteSSL added in v1.3.0

func (ep *EP) WriteSSL(fd int, msg []byte, n int) (int, int)

func (*EP) WriteSSLWithTimeout added in v1.3.0

func (ep *EP) WriteSSLWithTimeout(fd int, msg []byte, n int, timeout time.Duration) (int, int)

type ErrorCode

type ErrorCode int
const (
	ERROR_UNKNOW                ErrorCode = -1
	ERROR_ACCEPT                ErrorCode = 1
	ERROR_ADD_CONNECTION        ErrorCode = 2
	ERROR_SSL_CONNECTION_CREATE ErrorCode = 3
	ERROR_CLOSE_CONNECTION      ErrorCode = 4
	ERROR_READ                  ErrorCode = 5
	ERROR_SSL_READ              ErrorCode = 6
	ERROR_SSL_WRITE             ErrorCode = 7
	ERROR_EPOLL_WAIT            ErrorCode = 8
	ERROR_STOP                  ErrorCode = 9
	ERROR_POOL_BUFFER           ErrorCode = 10
)

type OnAcceptEvent

type OnAcceptEvent func(fd int)

type OnCloseEvent

type OnCloseEvent func(fd int)

type OnEpollOutEvent added in v1.3.8

type OnEpollOutEvent func(fd int)

type OnErrorEvent

type OnErrorEvent func(fd int, code ErrorCode, err error)

type OnReceiveEvent

type OnReceiveEvent func(fd int, msg []byte, n int)

type OpCode

type OpCode int
const (
	OP_UNKNOW   OpCode = -1
	OP_ACCEPT   OpCode = 1
	OP_RECEIVE  OpCode = 2
	OP_EPOLLOUT OpCode = 3
	OP_CLOSE    OpCode = 4
	OP_ERROR    OpCode = 5
)

type Request added in v1.3.1

type Request struct {
	Id         uint64
	Op         OpCode
	Fd         int
	Msg        []byte
	N          int
	SequenceId int
	ErrCode    ErrorCode
	Err        error
}

type SSL added in v1.3.0

type SSL struct {
	Id  uint64
	SSL *C.SSL
}

type UpdateDataFunc added in v1.3.0

type UpdateDataFunc func(data interface{})

Jump to

Keyboard shortcuts

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