https://github.com/scriptllh/tfg
基于epoll+协程池的golang网络库。支持epoll事件触发,读数据和业务逻辑处理分离,最大化利用cpu,防止内存急剧暴涨,适用于长连接、短连接,支持请求对象池和连接对象池
https://github.com/scriptllh/tfg
epoll golang goroutine-pool networking pool socket tcp
Last synced: 5 days ago
JSON representation
基于epoll+协程池的golang网络库。支持epoll事件触发,读数据和业务逻辑处理分离,最大化利用cpu,防止内存急剧暴涨,适用于长连接、短连接,支持请求对象池和连接对象池
- Host: GitHub
- URL: https://github.com/scriptllh/tfg
- Owner: scriptllh
- License: apache-2.0
- Created: 2019-07-05T11:30:04.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-07-28T07:17:40.000Z (almost 6 years ago)
- Last Synced: 2024-10-16T09:28:50.913Z (7 months ago)
- Topics: epoll, golang, goroutine-pool, networking, pool, socket, tcp
- Language: Go
- Homepage:
- Size: 1.06 MB
- Stars: 31
- Watchers: 5
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Welcome to tfg 👋
### 🏠 [Homepage](https://github.com/scriptllh/tfg)
## Install
```sh
go get -u github.com/scriptllh/tfg
```## Usage
```sh
echopackage main
import (
"fmt"
"github.com/scriptllh/tfg"
log "github.com/sirupsen/logrus"
"time"
)type HandleConn struct {
tfg.BaseHandleConn
}func (hc *HandleConn) PreOpen(c tfg.Conn) {
log.Infof("pre conn open: [conn:%v]", c)
}type req struct {
s string
}/**
* req : in => 请求读出来的字节 lastRemain => 上一次read 操作没有处理完的数据
*
* resp: packet => 处理完后生成业务的结构数据 remain => 这一节数据不够转换成业务数据下次read的时候再处理
* isFinRead => 是否继续处理这次留下来的remain,如果为false,则下次数据会按顺序过来,加上这次留下的remain,且是同一个协程处理
* 如果为true => 则下次数据不会带上remain,且下次read是下一个协程来处理
* isHandle => 是否有数据packet给handle执行 ,因为handle是异步执行的
*/
func (hc *HandleConn) Read(in []byte, lastRemain []byte) (packet interface{}, remain []byte, isFinRead bool, isHandle bool, err error) {
s := string(in)
req := &req{
s: s,
}
log.Infof("read [data:%v]", req)
return req, nil, false, true, nil
}/**
* req : conn => 连接 packet => read 处理过后的业务数据packet
*
*/func (hc *HandleConn) Handle(conn tfg.Conn, packet interface{}, err error) {
req := packet.(*req)
log.Infof("handle req [data:%v]", req)
time.Sleep(time.Millisecond * time.Duration(10))
n, err := conn.Write([]byte("tfg la la la"))
if err != nil {
fmt.Println(err)
return
}
log.Infof("handle write [n:%v]", n)
}func main() {
var handleConn HandleConn
s, err := tfg.NewServer(":6000", &handleConn, 0, tfg.RoundRobin)
if err != nil {
log.Errorf("new server [err:%v]", err)
return
}
log.Infof("server start :%v", "6000")
if err := s.Serve(); err != nil {
log.Errorf("serve [err:%v]", err)
return
}
}```
## Run
```sh
git clone https://github.com/scriptllh/tfg-example.git
1. echo
* make dev
* 用PacketSender访问
2. http
* make http
* 用Postman访问
```## 🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/scriptllh/tfg/issues).## Show your support
Give a ⭐️ if this project helped you!