README
¶
fastnet 简介
fastnet
是一个高性能、轻量级、非阻塞的 TCP 网络库,用户可以使用fastnet
来实现自己的应用层网络协议,从而构建出自己的应用层网络应用;- 在某些极端的网络业务场景,比如海量连接、高频短连接、网络小包等场景,
fastnet
在性能和资源占用上都超过Go
原生net
包的goroutine-per-connection
模式。
fastnet 特性
- 使用
Epoll
水平触发的IO多路复用技术,非阻塞IO,使用Reactor
模式; - 使用多线程充分利用多核CPU,使用动态扩容
Ring Buffer
实现读写缓冲区; - 支持异步读写操作、支持
SO_REUSEPORT
端口重用; - 灵活的事件定时器,可以定时任务,延时任务;
- 支持
WebSocket
,同时支持自定义协议,处理TCP
粘包;
TODO:
- 更多的负载方式(轮询(Round-Robin)、一致性哈希(
consistent hashing
)等);
Installation
- 获得并安装
fastnet
$ go get -u github.com/dongxiem/fastnet
- 进行
import
import "github.com/dongxiem/fastnet"
fastnet 性能测试
Example
package main
import (
"flag"
"net/http"
_ "net/http/pprof"
"strconv"
"time"
"github.com/Dongxiem/fastnet"
"github.com/Dongxiem/fastnet/connection"
"github.com/Dongxiem/fastnet/log"
"github.com/Dongxiem/fastnet/tool/sync/atomic"
)
// 定义 example 类型,并实现 Handler 接口对应的所有方法
type example struct {
Count atomic.Int64
}
func (s *example) OnConnect(c *connection.Connection) {
s.Count.Add(1)
//log.Println(" OnConnect : ", c.PeerAddr())
}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
//log.Println("OnMessage")
out = data
return
}
func (s *example) OnClose(c *connection.Connection) {
s.Count.Add(-1)
//log.Println("OnClose")
}
func main() {
go func() {
if err := http.ListenAndServe(":6060", nil); err != nil {
panic(err)
}
}()
handler := new(example)
var port int
var loops int
flag.IntVar(&port, "port", 1833, "server port")
flag.IntVar(&loops, "loops", -1, "num loops")
flag.Parse()
// new 一个 server,根据传递进来的 loops 进行工作线程循环的调配
s, err := fastnet.NewServer(handler,
fastnet.Network("tcp"),
fastnet.Address(":"+strconv.Itoa(port)),
fastnet.NumLoops(loops))
if err != nil {
panic(err)
}
// 每两秒打印一下 handle 统计
s.RunEvery(time.Second*2, func() {
log.Info("connections :", handler.Count.Get())
})
// 启动 server
s.Start()
}
Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler interface { connection.CallBack OnConnect(c *connection.Connection) }
Handler:Server 注册接口
type Options ¶
type Options struct { Network string // 网络协议 Address string // 监听端口地址 NumLoops int // work 协程个数,负责处理已连接客户端的读写事件 ReusePort bool // 是否开启端口复用 IdleTime time.Duration // 最大空闲时间(秒) Protocol connection.Protocol // 连接协议 // contains filtered or unexported fields }
Options:服务配置
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server:fastnet Server
func (*Server) RunAfter ¶
func (s *Server) RunAfter(d time.Duration, f func()) *timingwheel.Timer
RunAfter:延时任务开启
Example ¶
Output: RunAfter
Directories
¶
Path | Synopsis |
---|---|
benchmarks
|
|
example
|
|
Package log is from https://github.com/micro/go-micro/blob/master/util/log/log.go
|
Package log is from https://github.com/micro/go-micro/blob/master/util/log/log.go |
plugins
|
|
tool
|
|
Click to show internal directories.
Click to hide internal directories.