https://github.com/alberliu/gn
golang epoll实现
https://github.com/alberliu/gn
epoll golang
Last synced: 6 months ago
JSON representation
golang epoll实现
- Host: GitHub
- URL: https://github.com/alberliu/gn
- Owner: alberliu
- License: mit
- Created: 2020-04-07T06:31:39.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-12-20T03:19:34.000Z (over 1 year ago)
- Last Synced: 2024-12-08T21:57:27.105Z (6 months ago)
- Topics: epoll, golang
- Language: Go
- Homepage:
- Size: 2.37 MB
- Stars: 86
- Watchers: 2
- Forks: 52
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gn
### 简述
gn是一个基于linux下epoll的网络框架,目前只能运行在Linux环境下,gn可以配置处理网络事件的goroutine数量,相比golang原生库,在海量链接下,可以减少goroutine的开销,从而减少系统资源占用。
### 支持功能
1.tcp拆包粘包
支持多种编解码方式,使用sync.pool申请读写使用的字节数组,减少内存申请开销以及GC压力。
2.客户端超时踢出
可以设置超时时间,gn会定时检测超出超时的TCP连接(在指定时间内没有发送数据的连接),进行释放。
### 使用方式
```go
package mainimport (
"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()
}
```