gn

package module
v1.13.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: MIT Imports: 15 Imported by: 2

README

gn

简述

gn是一个基于linux下epoll的网络框架,目前只能运行在Linux环境下,gn可以配置处理网络事件的goroutine数量,相比golang原生库,在海量链接下,可以减少goroutine的开销,从而减少系统资源占用。

支持功能

1.tcp拆包粘包
支持多种编解码方式,使用sync.pool申请读写使用的字节数组,减少内存申请开销以及GC压力。
2.客户端超时踢出
可以设置超时时间,gn会定时检测超出超时的TCP连接(在指定时间内没有发送数据的连接),进行释放。

使用方式
package main

import (
	"github.com/alberliu/gn"
	"github.com/alberliu/gn/codec"
	"net"
	"strconv"
	"time"
)

var (
	decoder = codec.NewUvarintDecoder()
	encoder = codec.NewUvarintEncoder(1024)
)

var log = gn.GetLogger()

type Handler struct{}

func (*Handler) OnConnect(c *gn.Conn) {
	log.Info("server:connect:", c.GetFd(), c.GetAddr())
}
func (*Handler) OnMessage(c *gn.Conn, bytes []byte) {
	c.WriteWithEncoder(bytes)
	log.Info("server:read:", string(bytes))
}
func (*Handler) OnClose(c *gn.Conn, err error) {
	log.Info("server:close:", c.GetFd(), err)
}

func startServer() {
	server, err := gn.NewServer(":8080", &Handler{},
		gn.WithDecoder(decoder),
		gn.WithEncoder(encoder),
		gn.WithTimeout(5*time.Second),
		gn.WithReadBufferLen(10))
	if err != nil {
		log.Info("err")
		return
	}

	server.Run()
}

Documentation

Index

Constants

对端关闭连接 8193

View Source
const (
	EventIn      = 1 // 数据流入
	EventClose   = 2 // 断开连接
	EventTimeout = 3 // 检测到超时
)

Variables

View Source
var (
	ErrReadTimeout = errors.New("tcp read timeout")
)

Functions

func GetLogger added in v1.5.0

func GetLogger() logger

func SetLogger added in v1.1.0

func SetLogger(l logger)

func TimeEncoder

func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

Types

type Conn

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

Conn 客户端长连接

func (*Conn) Close

func (c *Conn) Close()

Close 关闭连接

func (*Conn) CloseRead added in v1.7.0

func (c *Conn) CloseRead() error

CloseRead 关闭连接

func (*Conn) GetAddr

func (c *Conn) GetAddr() string

GetAddr 获取客户端地址

func (*Conn) GetBuffer added in v1.8.1

func (c *Conn) GetBuffer() *codec.Buffer

GetBuffer 获取客户端地址

func (*Conn) GetData

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

GetData 获取数据

func (*Conn) GetFd

func (c *Conn) GetFd() int32

GetFd 获取文件描述符

func (*Conn) SetData

func (c *Conn) SetData(data interface{})

SetData 设置数据

func (*Conn) Write

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

Write 写入数据 todo 这里可能未能把所有数据写进去

func (*Conn) WriteWithEncoder added in v1.10.0

func (c *Conn) WriteWithEncoder(bytes []byte) error

WriteWithEncoder 使用编码器写入

type Handler

type Handler interface {
	OnConnect(c *Conn)               // OnConnect 当TCP长连接建立成功是回调
	OnMessage(c *Conn, bytes []byte) // OnMessage 当客户端有数据写入是回调
	OnClose(c *Conn, err error)      // OnClose 当客户端主动断开链接或者超时时回调,err返回关闭的原因
}

Handler Server 注册接口

type Option added in v1.1.0

type Option interface {
	// contains filtered or unexported methods
}

func WithAcceptGNum added in v1.1.0

func WithAcceptGNum(num int) Option

WithAcceptGNum 设置建立连接的goroutine数量

func WithDecoder added in v1.10.0

func WithDecoder(decoder codec.Decoder) Option

WithDecoder 设置解码器

func WithEncoder added in v1.10.0

func WithEncoder(encoder codec.Encoder) Option

WithEncoder 设置解码器

func WithIOEventQueueLen added in v1.2.1

func WithIOEventQueueLen(num int) Option

WithIOEventQueueLen 设置IO事件队列长度,默认值是1024

func WithIOGNum added in v1.1.0

func WithIOGNum(num int) Option

WithIOGNum 设置处理IO的goroutine数量

func WithReadBufferLen added in v1.5.0

func WithReadBufferLen(len int) Option

WithReadBufferLen 设置缓存区大小

func WithTimeout added in v1.1.0

func WithTimeout(timeout time.Duration) Option

WithTimeout 设置TCP超时检查的间隔时间以及超时时间

type Server

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

Server TCP服务

func NewServer

func NewServer(address string, handler Handler, opts ...Option) (*Server, error)

NewServer 创建server服务器

func (*Server) GetConn

func (s *Server) GetConn(fd int32) (*Conn, bool)

GetConn 获取Conn

func (*Server) GetConnsNum added in v1.1.0

func (s *Server) GetConnsNum() int64

GetConnsNum 获取当前长连接的数量

func (*Server) Run

func (s *Server) Run()

Run 启动服务

func (*Server) Stop

func (s *Server) Stop()

Stop 启动服务

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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