nbio

package module
v1.3.18 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: MIT Imports: 16 Imported by: 0

README

NBIO - NON-BLOCKING IO

Slack

Mentioned in Awesome Go MIT licensed Go Version Build Status Go Report Card Coverage Statusd

Contents

Features

Cross Platform
  • Linux: Epoll with LT/ET/ET+ONESHOT supported, LT as default
  • BSD(MacOS): Kqueue
  • Windows: Based on std net, for debugging only
Protocols Supported
  • TCP/UDP/Unix Socket supported
  • TLS supported
  • HTTP/HTTPS 1.x supported
  • Websocket supported, Passes the Autobahn Test Suite, OnOpen/OnMessage/OnClose order guaranteed
Interfaces
  • Implements a non-blocking net.Conn(except windows)
  • SetDeadline/SetReadDeadline/SetWriteDeadline supported
  • Concurrent Write/Close supported(both nbio.Conn and nbio/nbhttp/websocket.Conn)

Quick Start

package main

import (
	"log"

	"github.com/soar4/nbio"
)

func main() {
	engine := nbio.NewEngine(nbio.Config{
		Network:            "tcp",//"udp", "unix"
		Addrs:              []string{":8888"},
		MaxWriteBufferSize: 6 * 1024 * 1024,
	})

	// hanlde new connection
	engine.OnOpen(func(c *nbio.Conn) {
		log.Println("OnOpen:", c.RemoteAddr().String())
	})
	// hanlde connection closed
	engine.OnClose(func(c *nbio.Conn, err error) {
		log.Println("OnClose:", c.RemoteAddr().String(), err)
	})
	// handle data
	engine.OnData(func(c *nbio.Conn, data []byte) {
		c.Write(append([]byte{}, data...))
	})

	err := engine.Start()
	if err != nil {
		log.Fatalf("nbio.Start failed: %v\n", err)
		return
	}
	defer engine.Stop()

	<-make(chan int)
}

TCP Echo Examples

UDP Echo Examples

TLS Examples

HTTP Examples

HTTPS Examples

Websocket Examples

Websocket TLS Examples

Use With Other STD Based Frameworkds

Magics For HTTP and Websocket

Different IOMod
IOMod Remarks
IOModNonBlocking There's no difference between this IOMod and the old version with no IOMod. All the connections will be handled by poller.
IOModBlocking All the connections will be handled by at least one goroutine, for websocket, we can set Upgrader.BlockingModAsyncWrite=true to handle writting with a separated goroutine and then avoid Head-of-line blocking on broadcasting scenarios.
IOModMixed We set the Engine.MaxBlockingOnline, if the online num is smaller than it, the new connection will be handled by single goroutine as IOModBlocking, else the new connection will be handled by poller.

The IOModBlocking aims to improve the performance for low online service, it runs faster than std. The IOModMixed aims to keep a balance between performance and cpu/mem cost in different scenarios: when there are not too many online connections, it performs better than std, or else it can serve lots of online connections and keep healthy.

Using Websocket With Std Server
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/soar4/nbio/nbhttp/websocket"
)

func echo(w http.ResponseWriter, r *http.Request) {
	u := websocket.NewUpgrader()
	u.OnMessage(func(c *websocket.Conn, mt websocket.MessageType, data []byte) {
		c.WriteMessage(mt, data)
	})
	_, err := u.Upgrade(w, r, nil)
	if err != nil {
		log.Print("upgrade:", err)
		return
	}
}

func main() {
	mux := &http.ServeMux{}
	mux.HandleFunc("/ws", echo)
	server := http.Server{
		Addr:    "localhost:8080",
		Handler: mux,
	}
	fmt.Println("server exit:", server.ListenAndServe())
}

More Examples

Credits

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 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 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 NBConn

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

NBConn converts net.Conn to *Conn.

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

func (c *Conn) Sendfile(f *os.File, remain int64) (int64, error)

Sendfile .

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

func (c *Conn) WBufLen() int

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

Directories

Path Synopsis
autobahn
extension
tls

Jump to

Keyboard shortcuts

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