diff --git a/.github/workflows/cross-compile-bsd.yml b/.github/workflows/cross-compile-bsd.yml index 0e782a704..e91a22672 100644 --- a/.github/workflows/cross-compile-bsd.yml +++ b/.github/workflows/cross-compile-bsd.yml @@ -31,7 +31,7 @@ jobs: strategy: fail-fast: false matrix: - go: ['1.17', '1.22'] + go: ['1.20', '1.23'] os: - ubuntu-latest name: Go ${{ matrix.go }} @ ${{ matrix.os }} diff --git a/.github/workflows/gh-translator.yml b/.github/workflows/gh-translator.yml index c3a0f6d92..60bfe048a 100644 --- a/.github/workflows/gh-translator.yml +++ b/.github/workflows/gh-translator.yml @@ -20,4 +20,4 @@ jobs: with: BOT_GITHUB_TOKEN: ${{ secrets.GH_TRANSLATOR_TOKEN }} IS_MODIFY_TITLE: true - CUSTOM_BOT_NOTE: 🤖 Non-English text detected, translating... + CUSTOM_BOT_NOTE: 🤖 Non-English text detected, translating ... diff --git a/.github/workflows/stale-bot.yml b/.github/workflows/stale-bot.yml new file mode 100644 index 000000000..762a5b316 --- /dev/null +++ b/.github/workflows/stale-bot.yml @@ -0,0 +1,48 @@ +name: Monitor inactive issues and PRs +on: + schedule: + - cron: '0 0 * * *' + workflow_dispatch: + +jobs: + stale-issues: + runs-on: ubuntu-latest + permissions: + actions: write + issues: write + pull-requests: write + steps: + - uses: actions/stale@v9 + with: + operations-per-run: 100 + days-before-issue-stale: 30 + days-before-issue-close: 7 + stale-issue-label: 'stale' + stale-issue-message: | + This issue is marked as stale because it has been open for 30 days with no activity. + + You should take one of the following actions: + - Manually close this issue if it is no longer relevant + - Comment if you have more information to share + + This issue will be automatically closed in 7 days if no further activity occurs. + close-issue-message: | + This issue was closed because it has been inactive for 7 days since being marked as stale. + + If you believe this is a false alarm, please leave a comment for it or open a new issue, you can also reopen this issue directly if you have permission. + days-before-pr-stale: 21 + days-before-pr-close: 7 + stale-pr-label: 'stale' + stale-pr-message: | + This PR is marked as stale because it has been open for 21 days with no activity. + + You should take one of the following actions: + - Manually close this PR if it is no longer relevant + - Push new commits or comment if you have more information to share + + This PR will be automatically closed in 7 days if no further activity occurs. + close-pr-message: | + This PR was closed because it has been inactive for 7 days since being marked as stale. + + If you believe this is a false alarm, feel free to reopen this PR or create a new one. + repo-token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 202ac0c89..3e964a415 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -43,20 +43,20 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' cache: false - name: Setup and run golangci-lint - uses: golangci/golangci-lint-action@v4 + uses: golangci/golangci-lint-action@v6 with: - version: v1.56.2 + version: v1.61.0 args: -v -E gofumpt -E gocritic -E misspell -E revive -E godot --timeout 5m test: needs: lint strategy: fail-fast: false matrix: - go: ['1.17', '1.22'] + go: ['1.20', '1.23'] os: - ubuntu-latest - macos-latest diff --git a/.github/workflows/test_gc_opt.yml b/.github/workflows/test_gc_opt.yml index 36e592aa5..df99ae8e2 100644 --- a/.github/workflows/test_gc_opt.yml +++ b/.github/workflows/test_gc_opt.yml @@ -43,20 +43,20 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' cache: false - name: Setup and run golangci-lint - uses: golangci/golangci-lint-action@v4 + uses: golangci/golangci-lint-action@v6 with: - version: v1.56.2 + version: v1.61.0 args: -v -E gofumpt -E gocritic -E misspell -E revive -E godot --timeout 5m test: needs: lint strategy: fail-fast: false matrix: - go: ['1.17', '1.22'] + go: ['1.20', '1.23'] os: - ubuntu-latest - macos-latest diff --git a/.github/workflows/test_poll_opt.yml b/.github/workflows/test_poll_opt.yml index 91092992a..5d5ae19fc 100644 --- a/.github/workflows/test_poll_opt.yml +++ b/.github/workflows/test_poll_opt.yml @@ -42,20 +42,20 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' cache: false - name: Setup and run golangci-lint - uses: golangci/golangci-lint-action@v4 + uses: golangci/golangci-lint-action@v6 with: - version: v1.56.2 + version: v1.61.0 args: -v -E gofumpt -E gocritic -E misspell -E revive -E godot test: needs: lint strategy: fail-fast: false matrix: - go: ['1.17', '1.22'] + go: ['1.20', '1.23'] os: [ubuntu-latest, macos-latest] name: Go ${{ matrix.go }} @ ${{ matrix.os }} runs-on: ${{ matrix.os }} @@ -68,7 +68,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' - name: Print Go environment id: go-env diff --git a/.github/workflows/test_poll_opt_gc_opt.yml b/.github/workflows/test_poll_opt_gc_opt.yml index 3cefb09a5..a5aef4062 100644 --- a/.github/workflows/test_poll_opt_gc_opt.yml +++ b/.github/workflows/test_poll_opt_gc_opt.yml @@ -42,20 +42,20 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' cache: false - name: Setup and run golangci-lint - uses: golangci/golangci-lint-action@v4 + uses: golangci/golangci-lint-action@v6 with: - version: v1.56.2 + version: v1.61.0 args: -v -E gofumpt -E gocritic -E misspell -E revive -E godot test: needs: lint strategy: fail-fast: false matrix: - go: ['1.17', '1.22'] + go: ['1.20', '1.23'] os: [ubuntu-latest, macos-latest] name: Go ${{ matrix.go }} @ ${{ matrix.os }} runs-on: ${{ matrix.os }} @@ -68,7 +68,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' - name: Print Go environment id: go-env diff --git a/README.md b/README.md index bdba1a6f3..fd9b0f4d9 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ - +
@@ -246,9 +246,9 @@ Become a bronze sponsor with a monthly donation of $10 and get your logo on our # 🔑 JetBrains OS licenses -`gnet` had been being developed with `GoLand` IDE under the **free JetBrains Open Source license(s)** granted by JetBrains s.r.o., hence I would like to express my thanks here. +`gnet` has been being developed with `GoLand` IDE under the ***free JetBrains Open Source license(s)*** granted by JetBrains s.r.o., hence I would like to express my thanks here. - +JetBrains logo. # 🔋 Sponsorship diff --git a/README_ZH.md b/README_ZH.md index fe5a4f28f..04fae4bee 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -4,7 +4,7 @@ - +
@@ -246,9 +246,9 @@ Test duration : 15s # 🔑 JetBrains 开源证书支持 -`gnet` 项目一直以来都是在 JetBrains 公司旗下的 GoLand 集成开发环境中进行开发,基于 **free JetBrains Open Source license(s)** 正版免费授权,在此表达我的谢意。 +`gnet` 项目一直以来都是在 JetBrains 公司旗下的 GoLand 集成开发环境中进行开发,基于 ***free JetBrains Open Source license(s)*** 正版免费授权,在此表达我的谢意。 - +JetBrains logo. # 🔋 赞助商 diff --git a/acceptor_windows.go b/acceptor_windows.go index ce6005c0e..d9d6339cd 100644 --- a/acceptor_windows.go +++ b/acceptor_windows.go @@ -39,11 +39,7 @@ func (eng *engine) listenStream(ln net.Listener) (err error) { if atomic.LoadInt32(&eng.beingShutdown) == 0 { eng.opts.Logger.Errorf("Accept() fails due to error: %v", err) } else if errors.Is(err, net.ErrClosed) { - err = errorx.ErrEngineShutdown - // TODO: errors.Join() is not supported until Go 1.20, - // we will uncomment this line after we bump up the - // minimal supported go version to 1.20. - // err = errors.Join(err, errorx.ErrEngineShutdown) + err = errors.Join(err, errorx.ErrEngineShutdown) } return } @@ -81,9 +77,7 @@ func (eng *engine) ListenUDP(pc net.PacketConn) (err error) { if atomic.LoadInt32(&eng.beingShutdown) == 0 { eng.opts.Logger.Errorf("failed to receive data from UDP fd due to error:%v", err) } else if errors.Is(err, net.ErrClosed) { - err = errorx.ErrEngineShutdown - // TODO: ditto. - // err = errors.Join(err, errorx.ErrEngineShutdown) + err = errors.Join(err, errorx.ErrEngineShutdown) } return } diff --git a/client_test.go b/client_test.go index 1f446501c..3f270ba0e 100644 --- a/client_test.go +++ b/client_test.go @@ -5,6 +5,7 @@ package gnet import ( "bytes" + crand "crypto/rand" "io" "math/rand" "net" @@ -462,7 +463,6 @@ func runClient(t *testing.T, network, addr string, et, reuseport, multicore, asy } func startGnetClient(t *testing.T, cli *Client, network, addr string, multicore, async, netDial bool) { - rand.Seed(time.Now().UnixNano()) var ( c Conn err error @@ -492,7 +492,7 @@ func startGnetClient(t *testing.T, cli *Client, network, addr string, multicore, if network == "udp" { reqData = reqData[:datagramLen] } - _, err = rand.Read(reqData) + _, err = crand.Read(reqData) require.NoError(t, err) err = c.AsyncWrite(reqData, nil) require.NoError(t, err) diff --git a/engine_unix.go b/engine_unix.go index 83a1ae864..3bc488f95 100644 --- a/engine_unix.go +++ b/engine_unix.go @@ -19,6 +19,7 @@ package gnet import ( "context" + "errors" "runtime" "strings" "sync" @@ -29,7 +30,7 @@ import ( "github.com/panjf2000/gnet/v2/internal/gfd" "github.com/panjf2000/gnet/v2/internal/netpoll" "github.com/panjf2000/gnet/v2/internal/queue" - "github.com/panjf2000/gnet/v2/pkg/errors" + errorx "github.com/panjf2000/gnet/v2/pkg/errors" "github.com/panjf2000/gnet/v2/pkg/logging" ) @@ -59,7 +60,7 @@ func (eng *engine) isInShutdown() bool { // shutdown signals the engine to shut down. func (eng *engine) shutdown(err error) { - if err != nil && err != errors.ErrEngineShutdown { + if err != nil && !errors.Is(err, errorx.ErrEngineShutdown) { eng.opts.Logger.Errorf("engine is being shutdown with error: %v", err) } @@ -211,14 +212,14 @@ func (eng *engine) stop(s Engine) { // Notify all event-loops to exit. eng.eventLoops.iterate(func(i int, el *eventloop) bool { - err := el.poller.Trigger(queue.HighPriority, func(_ interface{}) error { return errors.ErrEngineShutdown }, nil) + err := el.poller.Trigger(queue.HighPriority, func(_ interface{}) error { return errorx.ErrEngineShutdown }, nil) if err != nil { eng.opts.Logger.Errorf("failed to enqueue shutdown signal of high-priority for event-loop(%d): %v", i, err) } return true }) if eng.ingress != nil { - err := eng.ingress.poller.Trigger(queue.HighPriority, func(_ interface{}) error { return errors.ErrEngineShutdown }, nil) + err := eng.ingress.poller.Trigger(queue.HighPriority, func(_ interface{}) error { return errorx.ErrEngineShutdown }, nil) if err != nil { eng.opts.Logger.Errorf("failed to enqueue shutdown signal of high-priority for main event-loop: %v", err) } diff --git a/eventloop_unix.go b/eventloop_unix.go index 7df6e7e1d..8fc9670ce 100644 --- a/eventloop_unix.go +++ b/eventloop_unix.go @@ -378,7 +378,7 @@ func (el *eventloop) ticker(ctx context.Context) { for { delay, action = el.eventHandler.OnTick() switch action { - case None: + case None, Close: case Shutdown: // It seems reasonable to mark this as low-priority, waiting for some tasks like asynchronous writes // to finish up before shutting down the service. diff --git a/eventloop_unix_test.go b/eventloop_unix_test.go index e21bb83f1..c166dde0a 100644 --- a/eventloop_unix_test.go +++ b/eventloop_unix_test.go @@ -275,7 +275,7 @@ func (s *testServerGC) GC(secs int) { runtime.GC() gcTime := time.Since(now) gcAllTime += gcTime - s.tester.Log(s.tester.Name(), s.network, " server gc:", gcTime, ", average gc time:", gcAllTime/gcAllCount) + s.tester.Log(s.tester.Name(), s.network, "server gc:", gcTime, "average gc time:", gcAllTime/gcAllCount) if time.Since(gcStart) >= time.Second*time.Duration(secs) { break } diff --git a/eventloop_windows.go b/eventloop_windows.go index 565f3ef30..906d8924f 100644 --- a/eventloop_windows.go +++ b/eventloop_windows.go @@ -155,7 +155,7 @@ func (el *eventloop) ticker(ctx context.Context) { for { delay, action = el.eventHandler.OnTick() switch action { - case None: + case None, Close: case Shutdown: if !shutdown { shutdown = true diff --git a/gnet.go b/gnet.go index 3e2d9ab9a..99f6f4038 100644 --- a/gnet.go +++ b/gnet.go @@ -293,8 +293,6 @@ type Socket interface { // algorithm). // The default is true (no delay), meaning that data is sent as soon as possible after a Write. SetNoDelay(noDelay bool) error - // CloseRead() error - // CloseWrite() error } // Conn is an interface of underlying connection. diff --git a/gnet_test.go b/gnet_test.go index 50abb6e0c..3e20b0c00 100644 --- a/gnet_test.go +++ b/gnet_test.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + crand "crypto/rand" "encoding/binary" "errors" "io" @@ -660,7 +661,6 @@ func runServer(t *testing.T, addrs []string, et, reuseport, multicore, async, wr } func startClient(t *testing.T, network, addr string, multicore, async bool) { - rand.Seed(time.Now().UnixNano()) c, err := net.Dial(network, addr) require.NoError(t, err) defer c.Close() @@ -678,7 +678,7 @@ func startClient(t *testing.T, network, addr string, multicore, async bool) { if network == "udp" { reqData = reqData[:datagramLen] } - _, err = rand.Read(reqData) + _, err = crand.Read(reqData) require.NoError(t, err) _, err = c.Write(reqData) require.NoError(t, err) @@ -1206,9 +1206,15 @@ type testStopServer struct { *BuiltinEventEngine tester *testing.T network, addr, protoAddr string + eng Engine action bool } +func (t *testStopServer) OnBoot(eng Engine) (action Action) { + t.eng = eng + return +} + func (t *testStopServer) OnClose(Conn, error) (action Action) { logging.Debugf("closing connection...") return @@ -1237,7 +1243,7 @@ func (t *testStopServer) OnTick() (delay time.Duration, action Action) { go func() { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - logging.Debugf("stop engine...", Stop(ctx, t.protoAddr)) + logging.Debugf("stop engine...", t.eng.Stop(ctx)) }() // waiting the engine shutdown. @@ -1365,7 +1371,7 @@ type testClosedWakeUpServer struct { clientClosed chan struct{} } -func (s *testClosedWakeUpServer) OnBoot(_ Engine) (action Action) { +func (s *testClosedWakeUpServer) OnBoot(eng Engine) (action Action) { go func() { c, err := net.Dial(s.network, s.addr) require.NoError(s.tester, err) @@ -1380,7 +1386,7 @@ func (s *testClosedWakeUpServer) OnBoot(_ Engine) (action Action) { close(s.clientClosed) <-s.serverClosed - logging.Debugf("stop engine...", Stop(context.TODO(), s.protoAddr)) + logging.Debugf("stop engine...", eng.Stop(context.TODO())) }() return None @@ -1653,7 +1659,6 @@ func runSimServer(t *testing.T, addr string, et bool, nclients, packetSize, pack } func runSimClient(t *testing.T, network, addr string, packetSize, batch int) { - rand.Seed(time.Now().UnixNano()) c, err := net.Dial(network, addr) require.NoError(t, err) defer c.Close() @@ -1689,7 +1694,7 @@ func batchSendAndRecv(t *testing.T, c net.Conn, rd *bufio.Reader, packetSize, ba ) for i := 0; i < batch; i++ { req := make([]byte, packetSize) - _, err := rand.Read(req) + _, err := crand.Read(req) require.NoError(t, err) requests = append(requests, req) packet, _ := codec.Encode(req) diff --git a/go.mod b/go.mod index e9d18bb67..42fc71bac 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,8 @@ require ( github.com/stretchr/testify v1.9.0 github.com/valyala/bytebufferpool v1.0.0 go.uber.org/zap v1.21.0 // don't upgrade this one - golang.org/x/sync v0.7.0 - golang.org/x/sys v0.21.0 + golang.org/x/sync v0.8.0 + golang.org/x/sys v0.25.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) diff --git a/go.sum b/go.sum index e8b0d5fce..e4f6e6e62 100644 --- a/go.sum +++ b/go.sum @@ -21,13 +21,11 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN 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/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 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/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= @@ -54,15 +52,15 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/internal/bs/bs.go b/internal/bs/bs.go index d23cb34a4..7afb5136e 100644 --- a/internal/bs/bs.go +++ b/internal/bs/bs.go @@ -15,27 +15,15 @@ package bs import ( - "reflect" "unsafe" ) -// BytesToString converts byte slice to a string without memory allocation. -// -// Note it may break if the implementation of string or slice header changes in the future go versions. +// BytesToString converts byte slice to a string without any memory allocation. func BytesToString(b []byte) string { - /* #nosec G103 */ - return *(*string)(unsafe.Pointer(&b)) + return unsafe.String(unsafe.SliceData(b), len(b)) } -// StringToBytes converts string to a byte slice without memory allocation. -// -// Note it may break if the implementation of string or slice header changes in the future go versions. -func StringToBytes(s string) (b []byte) { - /* #nosec G103 */ - sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) - /* #nosec G103 */ - bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - - bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len - return b +// StringToBytes converts string to a byte slice without any memory allocation. +func StringToBytes(s string) []byte { + return unsafe.Slice(unsafe.StringData(s), len(s)) } diff --git a/internal/netpoll/poller_epoll_default.go b/internal/netpoll/poller_epoll_default.go index d5e4d4e48..ec47d0b46 100644 --- a/internal/netpoll/poller_epoll_default.go +++ b/internal/netpoll/poller_epoll_default.go @@ -19,6 +19,7 @@ package netpoll import ( + "errors" "os" "runtime" "sync/atomic" @@ -27,7 +28,7 @@ import ( "golang.org/x/sys/unix" "github.com/panjf2000/gnet/v2/internal/queue" - "github.com/panjf2000/gnet/v2/pkg/errors" + errorx "github.com/panjf2000/gnet/v2/pkg/errors" "github.com/panjf2000/gnet/v2/pkg/logging" ) @@ -133,12 +134,9 @@ func (p *Poller) Polling(callback PollEventHandler) error { if fd := int(ev.Fd); fd == p.efd { // poller is awakened to run tasks in queues. doChores = true } else { - switch err = callback(fd, ev.Events, 0); err { - case nil: - case errors.ErrAcceptSocket, errors.ErrEngineShutdown: + err = callback(fd, ev.Events, 0) + if errors.Is(err, errorx.ErrAcceptSocket) || errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in event-loop: %v", err) } } } @@ -147,12 +145,9 @@ func (p *Poller) Polling(callback PollEventHandler) error { doChores = false task := p.urgentAsyncTaskQueue.Dequeue() for ; task != nil; task = p.urgentAsyncTaskQueue.Dequeue() { - switch err = task.Run(task.Arg); err { - case nil: - case errors.ErrEngineShutdown: + err = task.Run(task.Arg) + if errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in user-defined function, %v", err) } queue.PutTask(task) } @@ -160,12 +155,9 @@ func (p *Poller) Polling(callback PollEventHandler) error { if task = p.asyncTaskQueue.Dequeue(); task == nil { break } - switch err = task.Run(task.Arg); err { - case nil: - case errors.ErrEngineShutdown: + err = task.Run(task.Arg) + if errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in user-defined function, %v", err) } queue.PutTask(task) } diff --git a/internal/netpoll/poller_epoll_ultimate.go b/internal/netpoll/poller_epoll_ultimate.go index fec70097e..730e0485b 100644 --- a/internal/netpoll/poller_epoll_ultimate.go +++ b/internal/netpoll/poller_epoll_ultimate.go @@ -18,6 +18,7 @@ package netpoll import ( + "errors" "os" "runtime" "sync/atomic" @@ -26,7 +27,7 @@ import ( "golang.org/x/sys/unix" "github.com/panjf2000/gnet/v2/internal/queue" - "github.com/panjf2000/gnet/v2/pkg/errors" + errorx "github.com/panjf2000/gnet/v2/pkg/errors" "github.com/panjf2000/gnet/v2/pkg/logging" ) @@ -135,12 +136,9 @@ func (p *Poller) Polling() error { if pollAttachment.FD == p.epa.FD { // poller is awakened to run tasks in queues. doChores = true } else { - switch err = pollAttachment.Callback(pollAttachment.FD, ev.events, 0); err { - case nil: - case errors.ErrAcceptSocket, errors.ErrEngineShutdown: + err = pollAttachment.Callback(pollAttachment.FD, ev.events, 0) + if errors.Is(err, errorx.ErrAcceptSocket) || errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in event-loop: %v", err) } } } @@ -149,12 +147,9 @@ func (p *Poller) Polling() error { doChores = false task := p.urgentAsyncTaskQueue.Dequeue() for ; task != nil; task = p.urgentAsyncTaskQueue.Dequeue() { - switch err = task.Run(task.Arg); err { - case nil: - case errors.ErrEngineShutdown: + err = task.Run(task.Arg) + if errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in user-defined function, %v", err) } queue.PutTask(task) } @@ -162,12 +157,9 @@ func (p *Poller) Polling() error { if task = p.asyncTaskQueue.Dequeue(); task == nil { break } - switch err = task.Run(task.Arg); err { - case nil: - case errors.ErrEngineShutdown: + err = task.Run(task.Arg) + if errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in user-defined function, %v", err) } queue.PutTask(task) } diff --git a/internal/netpoll/poller_kqueue_default.go b/internal/netpoll/poller_kqueue_default.go index c44be9027..4fbcd8e60 100644 --- a/internal/netpoll/poller_kqueue_default.go +++ b/internal/netpoll/poller_kqueue_default.go @@ -19,6 +19,7 @@ package netpoll import ( + "errors" "os" "runtime" "sync/atomic" @@ -26,7 +27,7 @@ import ( "golang.org/x/sys/unix" "github.com/panjf2000/gnet/v2/internal/queue" - "github.com/panjf2000/gnet/v2/pkg/errors" + errorx "github.com/panjf2000/gnet/v2/pkg/errors" "github.com/panjf2000/gnet/v2/pkg/logging" ) @@ -118,12 +119,9 @@ func (p *Poller) Polling(callback PollEventHandler) error { doChores = true p.drainWakeupEvent() } else { - switch err = callback(fd, ev.Filter, ev.Flags); err { - case nil: - case errors.ErrAcceptSocket, errors.ErrEngineShutdown: + err = callback(fd, ev.Filter, ev.Flags) + if errors.Is(err, errorx.ErrAcceptSocket) || errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in event-loop: %v", err) } } } @@ -132,12 +130,9 @@ func (p *Poller) Polling(callback PollEventHandler) error { doChores = false task := p.urgentAsyncTaskQueue.Dequeue() for ; task != nil; task = p.urgentAsyncTaskQueue.Dequeue() { - switch err = task.Run(task.Arg); err { - case nil: - case errors.ErrEngineShutdown: + err = task.Run(task.Arg) + if errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in user-defined function, %v", err) } queue.PutTask(task) } @@ -145,12 +140,9 @@ func (p *Poller) Polling(callback PollEventHandler) error { if task = p.asyncTaskQueue.Dequeue(); task == nil { break } - switch err = task.Run(task.Arg); err { - case nil: - case errors.ErrEngineShutdown: + err = task.Run(task.Arg) + if errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in user-defined function, %v", err) } queue.PutTask(task) } diff --git a/internal/netpoll/poller_kqueue_ultimate.go b/internal/netpoll/poller_kqueue_ultimate.go index fc50c6521..e12caf0c1 100644 --- a/internal/netpoll/poller_kqueue_ultimate.go +++ b/internal/netpoll/poller_kqueue_ultimate.go @@ -19,6 +19,7 @@ package netpoll import ( + "errors" "os" "runtime" "sync/atomic" @@ -27,7 +28,7 @@ import ( "golang.org/x/sys/unix" "github.com/panjf2000/gnet/v2/internal/queue" - "github.com/panjf2000/gnet/v2/pkg/errors" + errorx "github.com/panjf2000/gnet/v2/pkg/errors" "github.com/panjf2000/gnet/v2/pkg/logging" ) @@ -120,12 +121,9 @@ func (p *Poller) Polling() error { p.drainWakeupEvent() } else { pollAttachment := restorePollAttachment(unsafe.Pointer(&ev.Udata)) - switch err = pollAttachment.Callback(int(ev.Ident), ev.Filter, ev.Flags); err { - case nil: - case errors.ErrAcceptSocket, errors.ErrEngineShutdown: + err = pollAttachment.Callback(int(ev.Ident), ev.Filter, ev.Flags) + if errors.Is(err, errorx.ErrAcceptSocket) || errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in event-loop: %v", err) } } } @@ -134,12 +132,9 @@ func (p *Poller) Polling() error { doChores = false task := p.urgentAsyncTaskQueue.Dequeue() for ; task != nil; task = p.urgentAsyncTaskQueue.Dequeue() { - switch err = task.Run(task.Arg); err { - case nil: - case errors.ErrEngineShutdown: + err = task.Run(task.Arg) + if errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in user-defined function, %v", err) } queue.PutTask(task) } @@ -147,12 +142,9 @@ func (p *Poller) Polling() error { if task = p.asyncTaskQueue.Dequeue(); task == nil { break } - switch err = task.Run(task.Arg); err { - case nil: - case errors.ErrEngineShutdown: + err = task.Run(task.Arg) + if errors.Is(err, errorx.ErrEngineShutdown) { return err - default: - logging.Warnf("error occurs in user-defined function, %v", err) } queue.PutTask(task) } diff --git a/os_unix_test.go b/os_unix_test.go index 0612cfd06..696ec1514 100644 --- a/os_unix_test.go +++ b/os_unix_test.go @@ -5,6 +5,7 @@ package gnet import ( "context" + crand "crypto/rand" "errors" "fmt" "math/rand" @@ -104,7 +105,6 @@ type testMcastServer struct { } func (s *testMcastServer) startMcastClient() { - rand.Seed(time.Now().UnixNano()) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() c, err := net.Dial("udp", s.addr) @@ -117,7 +117,7 @@ func (s *testMcastServer) startMcastClient() { start := time.Now() for time.Since(start) < duration { reqData := make([]byte, 1024) - _, err = rand.Read(reqData) + _, err = crand.Read(reqData) require.NoError(s.t, err) _, err = c.Write(reqData) require.NoError(s.t, err) diff --git a/pkg/buffer/elastic/elastic_buffer_test.go b/pkg/buffer/elastic/elastic_buffer_test.go index f000ac00d..80c219fac 100644 --- a/pkg/buffer/elastic/elastic_buffer_test.go +++ b/pkg/buffer/elastic/elastic_buffer_test.go @@ -2,10 +2,10 @@ package elastic import ( "bytes" + crand "crypto/rand" "math/rand" "runtime" "testing" - "time" "github.com/stretchr/testify/require" ) @@ -15,8 +15,8 @@ func TestMixedBuffer_Basic(t *testing.T) { mb, _ := New(maxStaticSize) const dataLen = 5 * 1024 data := make([]byte, dataLen) - rand.Seed(time.Now().Unix()) - rand.Read(data) + _, err := crand.Read(data) + require.NoError(t, err) n, err := mb.Write(data) require.NoError(t, err) require.EqualValues(t, dataLen, n) @@ -27,7 +27,8 @@ func TestMixedBuffer_Basic(t *testing.T) { mb.Reset(-1) newDataLen := rbn + 2*1024 data = make([]byte, newDataLen) - rand.Read(data) + _, err = crand.Read(data) + require.NoError(t, err) n, err = mb.Write(data) require.NoError(t, err) require.EqualValues(t, newDataLen, n) @@ -71,7 +72,8 @@ func TestMixedBuffer_Basic(t *testing.T) { n := rand.Intn(512) + 128 cum += n data := make([]byte, n) - rand.Read(data) + _, err := crand.Read(data) + require.NoError(t, err) buf.Write(data) if i < 3 { headCum += n @@ -107,15 +109,16 @@ func TestMixedBuffer_ReadFrom(t *testing.T) { mb, _ := New(maxStaticSize) const dataLen = 2 * 1024 data := make([]byte, dataLen) - rand.Seed(time.Now().Unix()) - rand.Read(data) + _, err := crand.Read(data) + require.NoError(t, err) r := bytes.NewReader(data) n, err := mb.ReadFrom(r) require.NoError(t, err) require.EqualValues(t, dataLen, n) require.EqualValues(t, dataLen, mb.Buffered()) newData := make([]byte, dataLen) - rand.Read(newData) + _, err = crand.Read(newData) + require.NoError(t, err) r.Reset(newData) n, err = mb.ReadFrom(r) require.NoError(t, err) @@ -155,12 +158,12 @@ func TestMixedBuffer_WriteTo(t *testing.T) { buf bytes.Buffer ) - rand.Seed(time.Now().Unix()) for i := 0; i < maxBlocks; i++ { n := rand.Intn(512) + 128 cum += n data := make([]byte, n) - rand.Read(data) + _, err := crand.Read(data) + require.NoError(t, err) buf.Write(data) if i < 3 { headCum += n diff --git a/pkg/buffer/linkedlist/llbuffer_test.go b/pkg/buffer/linkedlist/llbuffer_test.go index 1a755ca0d..4caabaa20 100644 --- a/pkg/buffer/linkedlist/llbuffer_test.go +++ b/pkg/buffer/linkedlist/llbuffer_test.go @@ -2,9 +2,9 @@ package linkedlist import ( "bytes" + crand "crypto/rand" "math/rand" "testing" - "time" "github.com/stretchr/testify/require" ) @@ -16,12 +16,12 @@ func TestLinkedListBuffer_Basic(t *testing.T) { cum int buf bytes.Buffer ) - rand.Seed(time.Now().Unix()) for i := 0; i < maxBlocks; i++ { n := rand.Intn(1024) + 128 cum += n data := make([]byte, n) - rand.Read(data) + _, err := crand.Read(data) + require.NoError(t, err) llb.PushBack(data) buf.Write(data) } @@ -39,8 +39,10 @@ func TestLinkedListBuffer_Basic(t *testing.T) { require.EqualValues(t, buf.Bytes()[:pn], p) tmpA := make([]byte, cum/16) tmpB := make([]byte, cum/16) - rand.Read(tmpA) - rand.Read(tmpB) + _, err = crand.Read(tmpA) + require.NoError(t, err) + _, err = crand.Read(tmpB) + require.NoError(t, err) bs, err = llb.PeekWithBytes(cum/4, tmpA, tmpB) require.NoError(t, err) p = p[:0] @@ -69,8 +71,8 @@ func TestLinkedListBuffer_ReadFrom(t *testing.T) { var llb Buffer const dataLen = 4 * 1024 data := make([]byte, dataLen) - rand.Seed(time.Now().Unix()) - rand.Read(data) + _, err := crand.Read(data) + require.NoError(t, err) r := bytes.NewReader(data) n, err := llb.ReadFrom(r) require.NoError(t, err) @@ -80,9 +82,11 @@ func TestLinkedListBuffer_ReadFrom(t *testing.T) { llb.Reset() const headLen = 256 head := make([]byte, headLen) - rand.Read(head) + _, err = crand.Read(head) + require.NoError(t, err) llb.PushBack(head) - rand.Read(data) + _, err = crand.Read(data) + require.NoError(t, err) r.Reset(data) n, err = llb.ReadFrom(r) require.NoError(t, err) @@ -104,12 +108,12 @@ func TestLinkedListBuffer_WriteTo(t *testing.T) { cum int buf bytes.Buffer ) - rand.Seed(time.Now().Unix()) for i := 0; i < maxBlocks; i++ { n := rand.Intn(1024) + 128 cum += n data := make([]byte, n) - rand.Read(data) + _, err := crand.Read(data) + require.NoError(t, err) llb.PushBack(data) buf.Write(data) } @@ -130,7 +134,8 @@ func TestLinkedListBuffer_WriteTo(t *testing.T) { n := rand.Intn(1024) + 128 cum += n data := make([]byte, n) - rand.Read(data) + _, err := crand.Read(data) + require.NoError(t, err) llb.PushBack(data) buf.Write(data) } diff --git a/pkg/buffer/ring/ring_buffer_test.go b/pkg/buffer/ring/ring_buffer_test.go index e323342b1..2efb15509 100644 --- a/pkg/buffer/ring/ring_buffer_test.go +++ b/pkg/buffer/ring/ring_buffer_test.go @@ -2,10 +2,9 @@ package ring import ( "bytes" - "math/rand" + crand "crypto/rand" "strings" "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -125,7 +124,7 @@ func TestRingBufferGrow(t *testing.T) { assert.Empty(t, head, "head should be empty") assert.Empty(t, tail, "tail should be empty") data := make([]byte, DefaultBufferSize+1) - n, err := rand.Read(data) + n, err := crand.Read(data) assert.NoError(t, err, "failed to generate random data") assert.EqualValuesf(t, DefaultBufferSize+1, n, "expect random data length is %d but got %d", DefaultBufferSize+1, n) n, err = rb.Write(data) @@ -139,7 +138,7 @@ func TestRingBufferGrow(t *testing.T) { rb = New(DefaultBufferSize) newData := make([]byte, 3*512) - n, err = rand.Read(newData) + n, err = crand.Read(newData) assert.NoError(t, err, "failed to generate random data") assert.EqualValuesf(t, 3*512, n, "expect random data length is %d but got %d", 3*512, n) n, err = rb.Write(newData) @@ -153,7 +152,7 @@ func TestRingBufferGrow(t *testing.T) { rb.Reset() data = make([]byte, bufferGrowThreshold) - n, err = rand.Read(data) + n, err = crand.Read(data) assert.NoError(t, err, "failed to generate random data") assert.EqualValuesf(t, bufferGrowThreshold, n, "expect random data length is %d but got %d", bufferGrowThreshold, n) n, err = rb.Write(data) @@ -165,7 +164,7 @@ func TestRingBufferGrow(t *testing.T) { assert.EqualValues(t, 0, rb.Available()) assert.EqualValues(t, data, rb.Bytes()) newData = make([]byte, bufferGrowThreshold/2+1) - n, err = rand.Read(newData) + n, err = crand.Read(newData) assert.NoError(t, err, "failed to generate random data") assert.EqualValuesf(t, bufferGrowThreshold/2+1, n, "expect random data length is %d but got %d", bufferGrowThreshold, n) n, err = rb.Write(newData) @@ -307,8 +306,8 @@ func TestRingBuffer_ReadFrom(t *testing.T) { rb := New(0) const dataLen = 4 * 1024 data := make([]byte, dataLen) - rand.Seed(time.Now().Unix()) - rand.Read(data) + _, err := crand.Read(data) + require.NoError(t, err) r := bytes.NewReader(data) n, err := rb.ReadFrom(r) require.NoError(t, err) @@ -329,8 +328,10 @@ func TestRingBuffer_ReadFrom(t *testing.T) { rb = New(0) const prefixLen = 2 * 1024 prefix := make([]byte, prefixLen) - rand.Read(prefix) - rand.Read(data) + _, err = crand.Read(prefix) + require.NoError(t, err) + _, err = crand.Read(data) + require.NoError(t, err) r.Reset(data) m, err = rb.Write(prefix) require.NoError(t, err) @@ -354,8 +355,10 @@ func TestRingBuffer_ReadFrom(t *testing.T) { const initLen = 5 * 1024 rb = New(initLen) - rand.Read(prefix) - rand.Read(data) + _, err = crand.Read(prefix) + require.NoError(t, err) + _, err = crand.Read(data) + require.NoError(t, err) r.Reset(data) m, err = rb.Write(prefix) require.NoError(t, err) @@ -380,8 +383,8 @@ func TestRingBuffer_WriteTo(t *testing.T) { rb := New(5 * 1024) const dataLen = 4 * 1024 data := make([]byte, dataLen) - rand.Seed(time.Now().Unix()) - rand.Read(data) + _, err := crand.Read(data) + require.NoError(t, err) n, err := rb.Write(data) require.NoError(t, err) require.EqualValuesf(t, dataLen, n, "ringbuffer should write %d bytes, but got %d", dataLen, n) @@ -394,7 +397,8 @@ func TestRingBuffer_WriteTo(t *testing.T) { require.EqualValues(t, data, buf.Bytes()) buf.Reset() - rand.Read(data) + _, err = crand.Read(data) + require.NoError(t, err) rb = New(dataLen) n, err = rb.Write(data) require.NoError(t, err) @@ -408,7 +412,8 @@ func TestRingBuffer_WriteTo(t *testing.T) { buf.Reset() rb.Reset() - rand.Read(data) + _, err = crand.Read(data) + require.NoError(t, err) n, err = rb.Write(data) require.NoError(t, err) require.EqualValuesf(t, dataLen, n, "ringbuffer should write %d bytes, but got %d", dataLen, n) @@ -419,7 +424,8 @@ func TestRingBuffer_WriteTo(t *testing.T) { require.EqualValues(t, data[:partLen], head) _, _ = rb.Discard(partLen) partData := make([]byte, partLen/2) - rand.Read(partData) + _, err = crand.Read(partData) + require.NoError(t, err) n, err = rb.Write(partData) require.NoError(t, err) require.EqualValuesf(t, partLen/2, n, "ringbuffer should write %d bytes, but got %d", dataLen, n) diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index 835ddedf0..3aaf1b6ba 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -35,8 +35,6 @@ var ( ErrUnsupportedUDPProtocol = errors.New("gnet: only udp/udp4/udp6 are supported") // ErrUnsupportedUDSProtocol occurs when trying to use an unsupported Unix protocol. ErrUnsupportedUDSProtocol = errors.New("gnet: only unix is supported") - // ErrUnsupportedPlatform occurs when running gnet on an unsupported platform. - ErrUnsupportedPlatform = errors.New("gnet: unsupported platform in gnet") // ErrUnsupportedOp occurs when calling some methods that has not been implemented yet. ErrUnsupportedOp = errors.New("gnet: unsupported operation") // ErrNegativeSize occurs when trying to pass a negative size to a buffer. diff --git a/pkg/pool/byteslice/byteslice.go b/pkg/pool/byteslice/byteslice.go index 3fd9ac89b..53397dac6 100644 --- a/pkg/pool/byteslice/byteslice.go +++ b/pkg/pool/byteslice/byteslice.go @@ -17,8 +17,6 @@ package byteslice import ( "math" "math/bits" - "reflect" - "runtime" "sync" "unsafe" ) @@ -41,7 +39,7 @@ func Put(buf []byte) { } // Get retrieves a byte slice of the length requested by the caller from pool or allocates a new one. -func (p *Pool) Get(size int) (buf []byte) { +func (p *Pool) Get(size int) []byte { if size <= 0 { return nil } @@ -49,16 +47,11 @@ func (p *Pool) Get(size int) (buf []byte) { return make([]byte, size) } idx := index(uint32(size)) - ptr, _ := p.pools[idx].Get().(unsafe.Pointer) + ptr, _ := p.pools[idx].Get().(*byte) if ptr == nil { - return make([]byte, 1<