From a9af2de8cb19a63c732c87da840157968d45d28b Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Thu, 11 Jan 2024 16:20:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=20ants=20=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=EF=BC=8C=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8=20gorout?= =?UTF-8?q?ine=EF=BC=8C=E6=8B=92=E7=BB=9D=E6=8F=90=E5=89=8D=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- engine.go | 27 +++++---------------------- go.mod | 5 +---- go.sum | 16 ---------------- options.go | 16 ---------------- server_tcp.go | 8 +------- server_ws.go | 11 ++--------- test/test_nnet.go | 19 ------------------- 7 files changed, 9 insertions(+), 93 deletions(-) diff --git a/engine.go b/engine.go index 0fffb7c..7bd2988 100644 --- a/engine.go +++ b/engine.go @@ -9,8 +9,6 @@ import ( "git.noahlan.cn/noahlan/nnet/session" "git.noahlan.cn/noahlan/ntool/ndef" "git.noahlan.cn/noahlan/ntool/nlog" - "github.com/panjf2000/ants/v2" - "math" "net" "time" ) @@ -24,7 +22,6 @@ type Engine struct { dieChan chan struct{} // 应用程序退出信号 packerBuilder packet.PackerBuilder // 封包、拆包器 serializer ndef.Serializer // 消息 序列化/反序列化 - pool *ants.Pool // goroutine池 connMgr *conn.ConnManager // 连接管理器 evtMgr event.EventManager // 事件管理器 sessIdMgr *session.IDMgr // SessionId管理器 @@ -56,17 +53,9 @@ func NewEngine(conf config.EngineConf, opts ...RunOption) *Engine { ngin.EngineConf.WriteChannelSize = config.DefaultWriteChannelSize } - if ngin.pool == nil { - ngin.pool, _ = ants.NewPool(math.MaxInt32) - } - return ngin } -func (ngin *Engine) Pool() *ants.Pool { - return ngin.pool -} - func (ngin *Engine) Use(middleware ...rt.Middleware) { ngin.middlewares = append(ngin.middlewares, middleware...) } @@ -126,15 +115,11 @@ func (ngin *Engine) handle(rawC net.Conn) *conn.Connection { } func (ngin *Engine) serveConn(nc *conn.Connection, packer packet.Packer) { - _ = ngin.pool.Submit(func() { - ngin.readLoop(nc, packer) - }) + go ngin.readLoop(nc, packer) - _ = ngin.pool.Submit(func() { - ngin.writeLoop(nc, packer) - }) + go ngin.writeLoop(nc, packer) - _ = ngin.pool.Submit(func() { + go func() { select { case <-nc.ChDie(): if ngin.ShallLogDebug() { @@ -143,7 +128,7 @@ func (ngin *Engine) serveConn(nc *conn.Connection, packer packet.Packer) { _ = ngin.connMgr.Remove(nc) ngin.evtMgr.OnClose(nc) } - }) + }() } func (ngin *Engine) readLoop(nc *conn.Connection, packer packet.Packer) { @@ -321,8 +306,6 @@ func (ngin *Engine) processPacket(nc *conn.Connection, p packet.IPacket) { if nc.Status() == conn.StatusWorking { // 处理包消息 - _ = ngin.pool.Submit(func() { - ngin.router.Handle(nc, p) - }) + go ngin.router.Handle(nc, p) } } diff --git a/go.mod b/go.mod index 836eb01..1fa2c32 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,7 @@ module git.noahlan.cn/noahlan/nnet go 1.20 -require ( - github.com/gorilla/websocket v1.5.1 - github.com/panjf2000/ants/v2 v2.9.0 -) +require github.com/gorilla/websocket v1.5.1 require ( git.noahlan.cn/noahlan/ntool v1.2.5 diff --git a/go.sum b/go.sum index 1e08469..18a4ba1 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,6 @@ git.noahlan.cn/noahlan/ntool v1.2.5 h1:sbafaRPKZJh89kAE6G0hyAjomZYhdofLnBQEhI6LbJA= git.noahlan.cn/noahlan/ntool v1.2.5/go.mod h1:pzoXErnQDLaHhvEHOGDoINf5VP1MDiU8NAcnaMEhxa4= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA= @@ -23,17 +21,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/panjf2000/ants/v2 v2.9.0 h1:SztCLkVxBRigbg+vt0S5QvF5vxAbxbKt09/YfAJ0tEo= -github.com/panjf2000/ants/v2 v2.9.0/go.mod h1:7ZxyxsqE4vvW0M7LSD8aI3cKwgFhBHbxnlN8mDqHa1I= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM= @@ -46,8 +35,6 @@ golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= -golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= @@ -60,7 +47,4 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8 h1:KR8+MyP7/qOlV+8Af01LtjL04bu7on42eVsxT4EyBQk= google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/options.go b/options.go index cc5ebab..67ff0d0 100644 --- a/options.go +++ b/options.go @@ -6,8 +6,6 @@ import ( "git.noahlan.cn/noahlan/nnet/packet" rt "git.noahlan.cn/noahlan/nnet/router" "git.noahlan.cn/noahlan/ntool/ndef" - "git.noahlan.cn/noahlan/ntool/npool" - "github.com/panjf2000/ants/v2" "time" ) @@ -85,17 +83,3 @@ func WithSerializer(s ndef.Serializer) RunOption { ngin.serializer = s } } - -// WithPool 设置使用自定义的工作池 -func WithPool(pl *ants.Pool) RunOption { - return func(ngin *Engine) { - ngin.pool = pl - } -} - -// WithPoolCfg 设置工作池配置 -func WithPoolCfg(cfg npool.Config) RunOption { - return func(ngin *Engine) { - ngin.pool, _ = ants.NewPool(cfg.PoolSize, ants.WithOptions(cfg.Options())) - } -} diff --git a/server_tcp.go b/server_tcp.go index cbc8441..739d1b4 100644 --- a/server_tcp.go +++ b/server_tcp.go @@ -35,12 +35,6 @@ func (ngin *Engine) ListenTCP(conf config.TCPServerConf) error { continue } - err = ngin.pool.Submit(func() { - ngin.handle(rc) - }) - if err != nil { - nlog.Errorf("%s submit conn pool err: %ng", ngin.LogPrefix(), err.Error()) - continue - } + go ngin.handle(rc) } } diff --git a/server_ws.go b/server_ws.go index f9f506c..f143375 100644 --- a/server_ws.go +++ b/server_ws.go @@ -6,7 +6,6 @@ import ( "git.noahlan.cn/noahlan/ntool/nlog" "github.com/gorilla/websocket" "net/http" - "os" "strings" ) @@ -61,15 +60,9 @@ func (ngin *Engine) upgradeWebsocket(conf config.WSServerFullConf) { http.HandleFunc(path, func(writer http.ResponseWriter, request *http.Request) { wc, err := upgrade.Upgrade(writer, request, nil) if err != nil { - nlog.Errorf("%s Upgrade failure, URI=%ng, Error=%ng", ngin.LogPrefix(), request.RequestURI, err.Error()) + nlog.Errorf("%s Upgrade failure, URI=%s, Error=%s", ngin.LogPrefix(), request.RequestURI, err.Error()) return } - err = ngin.pool.Submit(func() { - _ = ngin.handleWS(wc, conf.WSEvent) - }) - if err != nil { - nlog.Errorf("%s submit conn pool err: %v", ngin.LogPrefix(), err.Error()) - os.Exit(1) - } + go ngin.handleWS(wc, conf.WSEvent) }) } diff --git a/test/test_nnet.go b/test/test_nnet.go index 90fdd0b..e05e165 100644 --- a/test/test_nnet.go +++ b/test/test_nnet.go @@ -10,9 +10,6 @@ import ( protocol_nnet "git.noahlan.cn/noahlan/nnet/protocol/nnet" rt "git.noahlan.cn/noahlan/nnet/router" "git.noahlan.cn/noahlan/ntool/nlog" - "git.noahlan.cn/noahlan/ntool/npool" - "math" - "time" ) var ttt event.OnDisconnectedFn = func(nc *conn.Connection, err error) { @@ -21,14 +18,6 @@ var ttt event.OnDisconnectedFn = func(nc *conn.Connection, err error) { func runServer(addr string) { nginOpts := make([]nnet.RunOption, 0) - nginOpts = append(nginOpts, nnet.WithPoolCfg(npool.Config{ - PoolSize: math.MaxInt32, - ExpiryDuration: time.Second, - PreAlloc: false, - MaxBlockingTasks: 0, - Nonblocking: false, - DisablePurge: false, - })) nginOpts = append(nginOpts, protocol_nnet.WithNNetProtocol(protocol_nnet.Config{ HeartbeatInterval: 0, HandshakeValidator: nil, @@ -76,14 +65,6 @@ func runClient(addr string) (*nnet.Engine, *conn.Connection) { chReady := make(chan struct{}) nginOpts := make([]nnet.RunOption, 0) - nginOpts = append(nginOpts, nnet.WithPoolCfg(npool.Config{ - PoolSize: math.MaxInt32, - ExpiryDuration: time.Second, - PreAlloc: false, - MaxBlockingTasks: 0, - Nonblocking: false, - DisablePurge: false, - })) nginOpts = append(nginOpts, protocol_nnet.WithNNetClientProtocol(func() { chReady <- struct{}{} })...)