Skip to content

Commit

Permalink
other: 修改 server.WithTicker 将不再使用标准池的定时器,而是自行维护定时器池
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Dec 21, 2023
1 parent ae98963 commit 4f3b4eb
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ package main
import "github.com/kercylan98/minotaur/server"

func main() {
srv := server.New(server.NetworkWebsocket, server.WithTicker(50, 10, false))
srv := server.New(server.NetworkWebsocket, server.WithTicker(-1, 50, 10, false))
if err := srv.Run(":9999"); err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/internal/ticker-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import "github.com/kercylan98/minotaur/server"

func main() {
srv := server.New(server.NetworkWebsocket, server.WithTicker(50, 10, false))
srv := server.New(server.NetworkWebsocket, server.WithTicker(-1, 50, 10, false))
if err := srv.Run(":9999"); err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ func (slf *Conn) Write(packet []byte, callback ...func(err error)) {
func (slf *Conn) init() {
if slf.server.ticker != nil && slf.server.connTickerSize > 0 {
if slf.server.tickerAutonomy {
slf.ticker = timer.GetTicker(slf.server.connTickerSize)
slf.ticker = slf.server.tickerPool.GetTicker(slf.server.connTickerSize)
} else {
slf.ticker = timer.GetTicker(slf.server.connTickerSize, timer.WithCaller(func(name string, caller func()) {
slf.ticker = slf.server.tickerPool.GetTicker(slf.server.connTickerSize, timer.WithCaller(func(name string, caller func()) {
slf.server.PushShuntTickerMessage(slf, name, caller)
}))
}
Expand Down
14 changes: 10 additions & 4 deletions server/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type runtime struct {
supportMessageTypes map[int]bool // websocket模式下支持的消息类型
certFile, keyFile string // TLS文件
messagePoolSize int // 消息池大小
tickerPool *timer.Pool // 定时器池
ticker *timer.Ticker // 定时器
tickerAutonomy bool // 定时器是否独立运行
connTickerSize int // 连接定时器大小
Expand Down Expand Up @@ -130,17 +131,22 @@ func WithWebsocketReadDeadline(t time.Duration) Option {
}

// WithTicker 通过定时器创建服务器,为服务器添加定时器功能
// - poolSize:指定服务器定时器池大小,当池子内的定时器数量超出该值后,多余的定时器在释放时将被回收,该值小于等于 0 时将使用 timer.DefaultTickerPoolSize
// - size:服务器定时器时间轮大小
// - connSize:服务器连接定时器时间轮大小
// - connSize:服务器连接定时器时间轮大小,当该值小于等于 0 的时候,在新连接建立时将不再为其创建定时器
// - autonomy:定时器是否独立运行(独立运行的情况下不会作为服务器消息运行,会导致并发问题)
func WithTicker(size, connSize int, autonomy bool) Option {
func WithTicker(poolSize, size, connSize int, autonomy bool) Option {
return func(srv *Server) {
if poolSize <= 0 {
poolSize = timer.DefaultTickerPoolSize
}
srv.tickerPool = timer.NewPool(poolSize)
srv.connTickerSize = connSize
srv.tickerAutonomy = autonomy
if !autonomy {
srv.ticker = timer.GetTicker(size)
srv.ticker = srv.tickerPool.GetTicker(size)
} else {
srv.ticker = timer.GetTicker(size, timer.WithCaller(func(name string, caller func()) {
srv.ticker = srv.tickerPool.GetTicker(size, timer.WithCaller(func(name string, caller func()) {
srv.PushTickerMessage(name, caller)
}))
}
Expand Down
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ func (slf *Server) shutdown(err error) {
log.Error("Server", log.Err(shutdownErr))
}
}
if slf.tickerPool != nil {
slf.tickerPool.Release()
}
if slf.ticker != nil {
slf.ticker.Release()
}
Expand Down
2 changes: 1 addition & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestNew(t *testing.T) {
//limiter := rate.NewLimiter(rate.Every(time.Second), 100)
srv := server.New(server.NetworkWebsocket, server.WithTicker(200, 10, false), server.WithMessageBufferSize(1024*1024), server.WithPProf())
srv := server.New(server.NetworkWebsocket, server.WithTicker(-1, 200, 10, false), server.WithMessageBufferSize(1024*1024), server.WithPProf())
//srv.RegMessageExecBeforeEvent(func(srv *server.Server, message *server.Message) bool {
// t, c := srv.TimeoutContext(time.Second * 5)
// defer c()
Expand Down

0 comments on commit 4f3b4eb

Please sign in to comment.