nets

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2023 License: MIT Imports: 10 Imported by: 0

README

Go 语言的轻量网络库

📖 简介

nets是一个高性能、轻量级的Go标准库net.Conn封装,每个conn仅启用一个goroutine,资源复用,达到更优的任务效果。 特别说明: 设计灵感来自Gnet(包括本文档 ^_^),不依赖第三方库。

🚀 功能:

  • 非阻塞的异步网络工具库
  • 使用sync.Pool管理buff资源,达到复用目的并合理自旋。
  • 提供简约而不简单的连接管理
  • 优雅处理连接panic,防止程序崩溃(未处理OnBoot/OnShutdown/OnTick, 个人认为在服务启动、停止时进行回收错误没有任何意义并可能照成无法预估的后果。 欢迎讨论)
  • 连接事务非阻塞机制,优雅的处理多重关闭事件,杜绝重复关闭。

🛠 使用

📄 证书

nets 的源码允许用户在遵循 MIT 开源证书 规则的前提下使用。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRunning = errors.New(`running`)
	ErrStopped = errors.New(`stopped`)
	ErrClosed  = net.ErrClosed
)

Functions

This section is empty.

Types

type Buffs

type Buffs struct {
	// contains filtered or unexported fields
}

func NewBuffs

func NewBuffs(c uint32) *Buffs

func (*Buffs) GetBuf

func (p *Buffs) GetBuf() []byte

func (*Buffs) GetBuff

func (p *Buffs) GetBuff() *bytes.Buffer

func (*Buffs) PutBuf

func (p *Buffs) PutBuf(b []byte)

func (*Buffs) PutBuff

func (p *Buffs) PutBuff(b *bytes.Buffer)

type CliHandler

type CliHandler interface {
	OnBoot(cli Client) (err error) // 启动客户端触发
	OnShutdown(cli Client)         // 关闭客户端触发
	OnTick() (dur time.Duration)   // 定时触发
	ConnHandler
}

type CliOption

type CliOption func(*client)

func WithCliBufCap

func WithCliBufCap(size int) CliOption

WithCliBufCap 自定义buf容量

func WithCliDialConf

func WithCliDialConf(conf net.Dialer) CliOption

WithCliDialConf 选择连接配置

func WithCliTick

func WithCliTick(flag bool) CliOption

WithCliTick 选择是否启用定时器

type Client

type Client interface {
	Serve() error           // 启动服务
	Shutdown() error        // 关停服务
	NewConn() (Conn, error) // 创建新测连接
	Conns() int32           // 当前连接数
}

func NewClient

func NewClient(protoAddr string, handler CliHandler, opts ...CliOption) Client

NewClient 返回一个新的服务对象

type Conn

type Conn interface {
	net.Conn
	IsClosed() bool             // 连接是否已关闭
	Context() interface{}       // 获取上下文信息
	SetContext(ctx interface{}) // 设置上下文信息
}

type ConnHandler

type ConnHandler interface {
	OnOpened(conn Conn) (err error)   // 打开连接触发
	OnClosed(conn Conn, err error)    // 关闭连接触发
	OnActivate(conn Conn) (err error) // 收到数据触发
}

type Server

type Server interface {
	Serve() error
	Shutdown() error
	Conns() int32
}

func NewServer

func NewServer(protoAddr string, handler SvrHandler, opts ...SvrOption) Server

NewServer 返回一个新的服务对象

type SvrHandler

type SvrHandler interface {
	OnBoot(svr Server) (err error) // 启动服务端触发
	OnShutdown(svr Server)         // 关闭服务端触发
	OnTick() (dur time.Duration)   // 定时触发
	ConnHandler
}

type SvrOption

type SvrOption func(*server)

func WithSvrBufCap

func WithSvrBufCap(size int) SvrOption

WithSvrBufCap 自定义buf容量

func WithSvrListenConf

func WithSvrListenConf(conf net.ListenConfig) SvrOption

WithSvrListenConf 选择连接配置

func WithSvrTick

func WithSvrTick(flag bool) SvrOption

WithSvrTick 选择是否启用定时器

Directories

Path Synopsis
examples
client command
server command

Jump to

Keyboard shortcuts

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