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/.gitignore b/.gitignore
index acd5ee6c4..8e37a6b8e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
-# env
+# IDEs
.idea/
+.vscode/
# dependencies
/node_modules
diff --git a/README.md b/README.md
index 71469bbc9..fd9b0f4d9 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@
-
+
@@ -31,6 +31,8 @@ English | [中文](README_ZH.md)
# 🚀 Features
+## 🦖 Milestone
+
- [x] [High-performance](#-performance) event-driven looping based on a networking model of multiple threads/goroutines
- [x] Built-in goroutine pool powered by the library [ants](https://github.com/panjf2000/ants)
- [x] Lock-free during the entire runtime
@@ -43,8 +45,12 @@ English | [中文](README_ZH.md)
- [x] Running on `Linux`, `macOS`, `Windows`, and *BSD: `Darwin`/`DragonFlyBSD`/`FreeBSD`/`NetBSD`/`OpenBSD`
- [x] **Edge-triggered** I/O support
- [x] Multiple network addresses binding
+
+## 🕊 Roadmap
+
- [ ] **TLS** support
-- [ ] [io_uring](https://kernel.dk/io_uring.pdf) support
+- [ ] [io_uring](https://github.com/axboe/liburing/wiki/io_uring-and-networking-in-2023) support
+- [ ] **KCP** support
***Windows version of `gnet` should only be used in development for developing and testing, it shouldn't be used in production.***
@@ -52,7 +58,7 @@ English | [中文](README_ZH.md)
`gnet` is available as a Go module and we highly recommend that you use `gnet` via [Go Modules](https://go.dev/blog/using-go-modules), with Go 1.11 Modules enabled (Go 1.11+), you can just simply add `import "github.com/panjf2000/gnet/v2"` to the codebase and run `go mod download/go mod tidy` or `go [build|run|test]` to download the necessary dependencies automatically.
-## With v2
+## With v2
```bash
go get -u github.com/panjf2000/gnet/v2
@@ -66,11 +72,58 @@ go get -u github.com/panjf2000/gnet
# 🎡 Use cases
-The following companies/organizations use `gnet` as the underlying network service in production.
-
-
-
-If you have `gnet` integrated into projects, feel free to open a pull request refreshing this list.
+The following corporations/organizations use `gnet` as the underlying network service in production.
+
+
+
+If you're also using `gnet` in production, please help us enrich this list by opening a pull request.
# 📊 Performance
@@ -160,7 +213,7 @@ The source code of `gnet` should be distributed under the Apache-2.0 license.
Please read the [Contributing Guidelines](CONTRIBUTING.md) before opening a PR and thank you to all the developers who already made contributions to `gnet`!
-
+
# ⚓ Relevant Articles
@@ -191,20 +244,16 @@ Become a bronze sponsor with a monthly donation of $10 and get your logo on our
-# 💴 Patrons
-
-
-
# 🔑 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.
-
+
# 🔋 Sponsorship
-
This project is supported by:
-
-
+ This project is supported by:
+
+
\ No newline at end of file
diff --git a/README_ZH.md b/README_ZH.md
index 70f1c2c4d..04fae4bee 100644
--- a/README_ZH.md
+++ b/README_ZH.md
@@ -4,7 +4,7 @@
-
+
@@ -31,6 +31,8 @@
# 🚀 功能
+## 🦖 当前支持
+
- [x] 基于多线程/协程网络模型的[高性能](#-性能测试)事件驱动循环
- [x] 内置 goroutine 池,由开源库 [ants](https://github.com/panjf2000/ants) 提供支持
- [x] 整个生命周期是无锁的
@@ -43,8 +45,12 @@
- [x] 支持 `Linux`, `macOS`, `Windows` 和 *BSD 操作系统: `Darwin`/`DragonFlyBSD`/`FreeBSD`/`NetBSD`/`OpenBSD`
- [x] **Edge-triggered** I/O 支持
- [x] 多网络地址绑定
-- [ ] **TLS** 支持
-- [ ] [io_uring](https://kernel.dk/io_uring.pdf) 支持
+
+## 🕊 未来计划
+
+- [ ] 支持 **TLS**
+- [ ] 支持 [io_uring](https://github.com/axboe/liburing/wiki/io_uring-and-networking-in-2023)
+- [ ] 支持 **KCP**
***`gnet` 的 Windows 版本应该仅用于开发阶段的开发和测试,切勿用于生产环境***。
@@ -52,7 +58,7 @@
`gnet` 是一个 Go module,而且我们也强烈推荐通过 [Go Modules](https://go.dev/blog/using-go-modules) 来使用 `gnet`,在开启 Go Modules 支持(Go 1.11+)之后可以通过简单地在代码中写 `import "github.com/panjf2000/gnet/v2"` 来引入 `gnet`,然后执行 `go mod download/go mod tidy` 或者 `go [build|run|test]` 这些命令来自动下载所依赖的包。
-## 使用 v2
+## 使用 v2
```bash
go get -u github.com/panjf2000/gnet/v2
@@ -68,9 +74,56 @@ go get -u github.com/panjf2000/gnet
以下公司/组织在生产环境上使用了 `gnet` 作为底层网络服务。
-
-
-如果你的项目也在使用 `gnet`,欢迎给我提 Pull Request 来更新这份列表。
+
+
+如果你也正在生产环境上使用 `gnet`,欢迎提 Pull Request 来丰富这份列表。
# 📊 性能测试
@@ -160,7 +213,7 @@ Test duration : 15s
请在提 PR 之前仔细阅读 [Contributing Guidelines](CONTRIBUTING.md),感谢那些为 `gnet` 贡献过代码的开发者!
-
+
# ⚓ 相关文章
@@ -191,20 +244,16 @@ Test duration : 15s
-# 💴 资助者
-
-
-
# 🔑 JetBrains 开源证书支持
-`gnet` 项目一直以来都是在 JetBrains 公司旗下的 GoLand 集成开发环境中进行开发,基于 **free JetBrains Open Source license(s)** 正版免费授权,在此表达我的谢意。
+`gnet` 项目一直以来都是在 JetBrains 公司旗下的 GoLand 集成开发环境中进行开发,基于 ***free JetBrains Open Source license(s)*** 正版免费授权,在此表达我的谢意。
-
+
# 🔋 赞助商
-
本项目由以下机构赞助:
-
-
+ 本项目由以下机构赞助:
+
+
\ No newline at end of file
diff --git a/acceptor_unix.go b/acceptor_unix.go
index ae6e9a35e..7622f1a70 100644
--- a/acceptor_unix.go
+++ b/acceptor_unix.go
@@ -31,19 +31,18 @@ import (
func (el *eventloop) accept0(fd int, _ netpoll.IOEvent, _ netpoll.IOFlags) error {
for {
nfd, sa, err := socket.Accept(fd)
- if err != nil {
- switch err {
- case unix.EAGAIN: // the Accept queue has been drained out, we can return now
- return nil
- case unix.EINTR, unix.ECONNRESET, unix.ECONNABORTED:
- // ECONNRESET or ECONNABORTED could indicate that a socket
- // in the Accept queue was closed before we Accept()ed it.
- // It's a silly error, let's retry it.
- continue
- default:
- el.getLogger().Errorf("Accept() failed due to error: %v", err)
- return errors.ErrAcceptSocket
- }
+ switch err {
+ case nil:
+ case unix.EAGAIN: // the Accept queue has been drained out, we can return now
+ return nil
+ case unix.EINTR, unix.ECONNRESET, unix.ECONNABORTED:
+ // ECONNRESET or ECONNABORTED could indicate that a socket
+ // in the Accept queue was closed before we Accept()ed it.
+ // It's a silly error, let's retry it.
+ continue
+ default:
+ el.getLogger().Errorf("Accept() failed due to error: %v", err)
+ return errors.ErrAcceptSocket
}
remoteAddr := socket.SockaddrToTCPOrUnixAddr(sa)
@@ -71,17 +70,16 @@ func (el *eventloop) accept(fd int, ev netpoll.IOEvent, flags netpoll.IOFlags) e
}
nfd, sa, err := socket.Accept(fd)
- if err != nil {
- switch err {
- case unix.EINTR, unix.EAGAIN, unix.ECONNRESET, unix.ECONNABORTED:
- // ECONNRESET or ECONNABORTED could indicate that a socket
- // in the Accept queue was closed before we Accept()ed it.
- // It's a silly error, let's retry it.
- return nil
- default:
- el.getLogger().Errorf("Accept() failed due to error: %v", err)
- return errors.ErrAcceptSocket
- }
+ switch err {
+ case nil:
+ case unix.EINTR, unix.EAGAIN, unix.ECONNRESET, unix.ECONNABORTED:
+ // ECONNRESET or ECONNABORTED could indicate that a socket
+ // in the Accept queue was closed before we Accept()ed it.
+ // It's a silly error, let's retry it.
+ return nil
+ default:
+ el.getLogger().Errorf("Accept() failed due to error: %v", err)
+ return errors.ErrAcceptSocket
}
remoteAddr := socket.SockaddrToTCPOrUnixAddr(sa)
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 8b56f5494..3f270ba0e 100644
--- a/client_test.go
+++ b/client_test.go
@@ -5,9 +5,11 @@ package gnet
import (
"bytes"
+ crand "crypto/rand"
"io"
"math/rand"
"net"
+ "path/filepath"
"sync"
"sync/atomic"
"testing"
@@ -15,6 +17,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "go.uber.org/zap"
errorx "github.com/panjf2000/gnet/v2/pkg/errors"
"github.com/panjf2000/gnet/v2/pkg/logging"
@@ -437,7 +440,7 @@ func runClient(t *testing.T, network, addr string, et, reuseport, multicore, asy
clientEV := &clientEvents{tester: t, packetLen: streamLen, svr: ts}
ts.client, err = NewClient(
clientEV,
- WithLogLevel(logging.DebugLevel),
+ WithTCPNoDelay(TCPNoDelay),
WithLockOSThread(true),
WithTicker(true),
)
@@ -455,13 +458,11 @@ func runClient(t *testing.T, network, addr string, et, reuseport, multicore, asy
WithReusePort(reuseport),
WithTicker(true),
WithTCPKeepAlive(time.Minute*1),
- WithTCPNoDelay(TCPDelay),
WithLoadBalancing(lb))
assert.NoError(t, err)
}
func startGnetClient(t *testing.T, cli *Client, network, addr string, multicore, async, netDial bool) {
- rand.Seed(time.Now().UnixNano())
var (
c Conn
err error
@@ -491,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)
@@ -599,9 +600,22 @@ func testConnWakeImmediately(t *testing.T, client *Client, clientEV *clientEvent
}
func TestWakeConnImmediately(t *testing.T) {
+ currentLogger, currentFlusher := logging.GetDefaultLogger(), logging.GetDefaultFlusher()
+ t.Cleanup(func() {
+ logging.SetDefaultLoggerAndFlusher(currentLogger, currentFlusher) // restore
+ })
+
clientEV := &clientEventsForWake{tester: t}
- client, err := NewClient(clientEV, WithLogLevel(logging.DebugLevel))
+ logPath := filepath.Join(t.TempDir(), "gnet-test-wake-conn-immediately.log")
+ client, err := NewClient(clientEV,
+ WithSocketRecvBuffer(4*1024),
+ WithSocketSendBuffer(4*1024),
+ WithLogPath(logPath),
+ WithLogLevel(logging.WarnLevel),
+ WithReadBufferCap(512),
+ WithWriteBufferCap(512))
assert.NoError(t, err)
+ logging.Cleanup()
err = client.Start()
assert.NoError(t, err)
@@ -614,6 +628,11 @@ func TestWakeConnImmediately(t *testing.T) {
}
func TestClientReadOnEOF(t *testing.T) {
+ currentLogger, currentFlusher := logging.GetDefaultLogger(), logging.GetDefaultFlusher()
+ t.Cleanup(func() {
+ logging.SetDefaultLoggerAndFlusher(currentLogger, currentFlusher) // restore
+ })
+
ln, err := net.Listen("tcp", "127.0.0.1:9999")
assert.NoError(t, err)
defer ln.Close()
@@ -635,7 +654,13 @@ func TestClientReadOnEOF(t *testing.T) {
}, 1),
data: []byte("test"),
}
- cli, err := NewClient(ev)
+ cli, err := NewClient(ev,
+ WithSocketRecvBuffer(4*1024),
+ WithSocketSendBuffer(4*1024),
+ WithTCPKeepAlive(time.Minute),
+ WithLogger(zap.NewExample().Sugar()),
+ WithReadBufferCap(32*1024),
+ WithWriteBufferCap(32*1024))
assert.NoError(t, err)
defer cli.Stop() //nolint:errcheck
diff --git a/client_unix.go b/client_unix.go
index 597d73592..2d82f8e98 100644
--- a/client_unix.go
+++ b/client_unix.go
@@ -200,15 +200,16 @@ func (cli *Client) EnrollContext(c net.Conn, ctx interface{}) (Conn, error) {
)
switch c.(type) {
case *net.UnixConn:
- if sockAddr, _, _, err = socket.GetUnixSockAddr(c.RemoteAddr().Network(), c.RemoteAddr().String()); err != nil {
+ sockAddr, _, _, err = socket.GetUnixSockAddr(c.RemoteAddr().Network(), c.RemoteAddr().String())
+ if err != nil {
return nil, err
}
ua := c.LocalAddr().(*net.UnixAddr)
ua.Name = c.RemoteAddr().String() + "." + strconv.Itoa(dupFD)
gc = newTCPConn(dupFD, cli.el, sockAddr, c.LocalAddr(), c.RemoteAddr())
case *net.TCPConn:
- if cli.opts.TCPNoDelay == TCPDelay {
- if err = socket.SetNoDelay(dupFD, 0); err != nil {
+ if cli.opts.TCPNoDelay == TCPNoDelay {
+ if err = socket.SetNoDelay(dupFD, 1); err != nil {
return nil, err
}
}
@@ -217,12 +218,14 @@ func (cli *Client) EnrollContext(c net.Conn, ctx interface{}) (Conn, error) {
return nil, err
}
}
- if sockAddr, _, _, _, err = socket.GetTCPSockAddr(c.RemoteAddr().Network(), c.RemoteAddr().String()); err != nil {
+ sockAddr, _, _, _, err = socket.GetTCPSockAddr(c.RemoteAddr().Network(), c.RemoteAddr().String())
+ if err != nil {
return nil, err
}
gc = newTCPConn(dupFD, cli.el, sockAddr, c.LocalAddr(), c.RemoteAddr())
case *net.UDPConn:
- if sockAddr, _, _, _, err = socket.GetUDPSockAddr(c.RemoteAddr().Network(), c.RemoteAddr().String()); err != nil {
+ sockAddr, _, _, _, err = socket.GetUDPSockAddr(c.RemoteAddr().Network(), c.RemoteAddr().String())
+ if err != nil {
return nil, err
}
gc = newUDPConn(dupFD, cli.el, c.LocalAddr(), sockAddr, true)
diff --git a/client_windows.go b/client_windows.go
index 33e0566ff..d9cbbde4b 100644
--- a/client_windows.go
+++ b/client_windows.go
@@ -205,7 +205,7 @@ func (cli *Client) EnrollContext(nc net.Conn, ctx interface{}) (gc Conn, err err
c := newUDPConn(cli.el, nil, nc.LocalAddr(), nc.RemoteAddr())
c.SetContext(ctx)
c.rawConn = nc
- cli.el.ch <- &openConn{c: c, isDatagram: true, cb: func() { close(connOpened) }}
+ cli.el.ch <- &openConn{c: c, cb: func() { close(connOpened) }}
go func(uc net.Conn, el *eventloop) {
var buffer [0x10000]byte
for {
diff --git a/connection_unix.go b/connection_unix.go
index 53f29495b..1be7c36dd 100644
--- a/connection_unix.go
+++ b/connection_unix.go
@@ -335,13 +335,13 @@ func (c *conn) Next(n int) (buf []byte, err error) {
}
head, tail := c.inboundBuffer.Peek(n)
defer c.inboundBuffer.Discard(n) //nolint:errcheck
- if len(head) == n {
+ if len(head) >= n {
return head[:n], err
}
c.loop.cache.Reset()
c.loop.cache.Write(head)
c.loop.cache.Write(tail)
- if inBufferLen == n {
+ if inBufferLen >= n {
return c.loop.cache.Bytes(), err
}
@@ -362,13 +362,13 @@ func (c *conn) Peek(n int) (buf []byte, err error) {
return c.buffer[:n], err
}
head, tail := c.inboundBuffer.Peek(n)
- if len(head) == n {
+ if len(head) >= n {
return head[:n], err
}
c.loop.cache.Reset()
c.loop.cache.Write(head)
c.loop.cache.Write(tail)
- if inBufferLen == n {
+ if inBufferLen >= n {
return c.loop.cache.Bytes(), err
}
diff --git a/connection_windows.go b/connection_windows.go
index e46498f9d..efdfc22c9 100644
--- a/connection_windows.go
+++ b/connection_windows.go
@@ -45,7 +45,6 @@ type udpConn struct {
type openConn struct {
c *conn
cb func()
- isDatagram bool
}
type conn struct {
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 12bfc2f4a..919aee235 100644
--- a/eventloop_unix.go
+++ b/eventloop_unix.go
@@ -227,24 +227,14 @@ loop:
return nil
}
-func (el *eventloop) close(c *conn, err error) (rerr error) {
- if addr := c.localAddr; addr != nil && strings.HasPrefix(c.localAddr.Network(), "udp") {
- rerr = el.poller.Delete(c.fd)
- if _, ok := el.listeners[c.fd]; !ok {
- rerr = unix.Close(c.fd)
- el.connections.delConn(c)
- }
- if el.eventHandler.OnClose(c, err) == Shutdown {
- return errorx.ErrEngineShutdown
- }
- c.release()
- return
- }
-
+func (el *eventloop) close(c *conn, err error) error {
if !c.opened || el.connections.getConn(c.fd) == nil {
- return // ignore stale connections
+ return nil // ignore stale connections
}
+ el.connections.delConn(c)
+ action := el.eventHandler.OnClose(c, err)
+
// Send residual data in buffer back to the remote before actually closing the connection.
for !c.outboundBuffer.IsEmpty() {
iov, _ := c.outboundBuffer.Peek(0)
@@ -258,26 +248,26 @@ func (el *eventloop) close(c *conn, err error) (rerr error) {
}
}
+ c.release()
+
+ var errStr strings.Builder
err0, err1 := el.poller.Delete(c.fd), unix.Close(c.fd)
if err0 != nil {
- rerr = fmt.Errorf("failed to delete fd=%d from poller in event-loop(%d): %v", c.fd, el.idx, err0)
+ err0 = fmt.Errorf("failed to delete fd=%d from poller in event-loop(%d): %v",
+ c.fd, el.idx, os.NewSyscallError("delete", err0))
+ errStr.WriteString(err0.Error())
+ errStr.WriteString(" | ")
}
if err1 != nil {
- err1 = fmt.Errorf("failed to close fd=%d in event-loop(%d): %v", c.fd, el.idx, os.NewSyscallError("close", err1))
- if rerr != nil {
- rerr = errors.New(rerr.Error() + " & " + err1.Error())
- } else {
- rerr = err1
- }
+ err1 = fmt.Errorf("failed to close fd=%d in event-loop(%d): %v",
+ c.fd, el.idx, os.NewSyscallError("close", err1))
+ errStr.WriteString(err1.Error())
}
-
- el.connections.delConn(c)
- if el.eventHandler.OnClose(c, err) == Shutdown {
- rerr = errorx.ErrEngineShutdown
+ if errStr.Len() > 0 {
+ return errors.New(strings.TrimSuffix(errStr.String(), " | "))
}
- c.release()
- return
+ return el.handleAction(c, action)
}
func (el *eventloop) wake(c *conn) error {
@@ -304,7 +294,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.
@@ -325,19 +315,6 @@ func (el *eventloop) ticker(ctx context.Context) {
}
}
-func (el *eventloop) handleAction(c *conn, action Action) error {
- switch action {
- case None:
- return nil
- case Close:
- return el.close(c, nil)
- case Shutdown:
- return errorx.ErrEngineShutdown
- default:
- return nil
- }
-}
-
func (el *eventloop) readUDP(fd int, _ netpoll.IOEvent, _ netpoll.IOFlags) error {
n, sa, err := unix.Recvfrom(fd, el.buffer, 0)
if err != nil {
@@ -364,6 +341,19 @@ func (el *eventloop) readUDP(fd int, _ netpoll.IOEvent, _ netpoll.IOFlags) error
return nil
}
+func (el *eventloop) handleAction(c *conn, action Action) error {
+ switch action {
+ case None:
+ return nil
+ case Close:
+ return el.close(c, nil)
+ case Shutdown:
+ return errorx.ErrEngineShutdown
+ default:
+ return nil
+ }
+}
+
/*
func (el *eventloop) execCmd(itf interface{}) (err error) {
cmd := itf.(*asyncCmd)
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 ea0f87377..906d8924f 100644
--- a/eventloop_windows.go
+++ b/eventloop_windows.go
@@ -18,8 +18,8 @@ import (
"bytes"
"context"
"errors"
+ "fmt"
"runtime"
- "strings"
"sync/atomic"
"time"
@@ -97,10 +97,8 @@ func (el *eventloop) open(oc *openConn) error {
}
c := oc.c
- if !oc.isDatagram {
- el.connections[c] = struct{}{}
- el.incConn(1)
- }
+ el.connections[c] = struct{}{}
+ el.incConn(1)
out, action := el.eventHandler.OnOpen(c)
if out != nil {
@@ -157,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
@@ -188,28 +186,18 @@ func (el *eventloop) wake(c *conn) error {
}
func (el *eventloop) close(c *conn, err error) error {
- if addr := c.localAddr; addr != nil && strings.HasPrefix(addr.Network(), "udp") {
- action := el.eventHandler.OnClose(c, err)
- if c.rawConn != nil {
- if err := c.rawConn.Close(); err != nil {
- el.getLogger().Errorf("failed to close connection(%s), error:%v", c.remoteAddr.String(), err)
- }
- }
- c.release()
- return el.handleAction(c, action)
- }
-
- if _, ok := el.connections[c]; !ok {
+ if _, ok := el.connections[c]; c.rawConn == nil || !ok {
return nil // ignore stale wakes.
}
delete(el.connections, c)
el.incConn(-1)
action := el.eventHandler.OnClose(c, err)
- if err := c.rawConn.Close(); err != nil {
- el.getLogger().Errorf("failed to close connection(%s), error:%v", c.remoteAddr.String(), err)
- }
+ err = c.rawConn.Close()
c.release()
+ if err != nil {
+ return fmt.Errorf("failed to close connection=%s in event-loop(%d): %v", c.remoteAddr, el.idx, err)
+ }
return el.handleAction(c, action)
}
diff --git a/gnet.go b/gnet.go
index 3b4b577f0..d8414da0d 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 11c7df997..3e20b0c00 100644
--- a/gnet_test.go
+++ b/gnet_test.go
@@ -4,11 +4,13 @@ import (
"bufio"
"bytes"
"context"
+ crand "crypto/rand"
"encoding/binary"
"errors"
"io"
"math/rand"
"net"
+ "path/filepath"
"runtime"
"strings"
"sync/atomic"
@@ -500,9 +502,8 @@ func (s *testServer) OnClose(c Conn, err error) (action Action) {
if err != nil {
logging.Debugf("error occurred on closed, %v\n", err)
}
- if c.LocalAddr().Network() != "udp" {
- require.Equal(s.tester, c.Context(), c, "invalid context")
- }
+
+ require.Equal(s.tester, c.Context(), c, "invalid context")
atomic.AddInt32(&s.disconnected, 1)
return
@@ -642,7 +643,7 @@ func runServer(t *testing.T, addrs []string, et, reuseport, multicore, async, wr
WithReusePort(reuseport),
WithTicker(true),
WithTCPKeepAlive(time.Minute),
- WithTCPNoDelay(TCPDelay),
+ WithTCPNoDelay(TCPNoDelay),
WithLoadBalancing(lb))
} else {
err = Run(ts,
@@ -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)
@@ -883,8 +883,19 @@ func (t *testShutdownServer) OnTick() (delay time.Duration, action Action) {
}
func testShutdown(t *testing.T, network, addr string) {
+ currentLogger, currentFlusher := logging.GetDefaultLogger(), logging.GetDefaultFlusher()
+ t.Cleanup(func() {
+ logging.SetDefaultLoggerAndFlusher(currentLogger, currentFlusher) // restore
+ })
+
events := &testShutdownServer{tester: t, network: network, addr: addr, N: 100}
- err := Run(events, network+"://"+addr, WithTicker(true), WithReadBufferCap(512), WithWriteBufferCap(512))
+ logPath := filepath.Join(t.TempDir(), "gnet-test-shutdown.log")
+ err := Run(events, network+"://"+addr,
+ WithLogPath(logPath),
+ WithLogLevel(logging.WarnLevel),
+ WithTicker(true),
+ WithReadBufferCap(512),
+ WithWriteBufferCap(512))
assert.NoError(t, err)
require.Equal(t, 0, int(events.clients), "did not close all clients")
}
@@ -1195,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
@@ -1226,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.
@@ -1354,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)
@@ -1369,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
@@ -1642,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()
@@ -1678,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 02ad938c6..0ab63f761 100644
--- a/go.mod
+++ b/go.mod
@@ -1,12 +1,12 @@
module github.com/panjf2000/gnet/v2
require (
- github.com/panjf2000/ants/v2 v2.9.0
- github.com/stretchr/testify v1.8.4
+ github.com/panjf2000/ants/v2 v2.10.0
+ 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.19.0
+ golang.org/x/sync v0.8.0
+ golang.org/x/sys v0.25.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)
@@ -18,4 +18,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)
-go 1.17
+go 1.20
diff --git a/go.sum b/go.sum
index 0823447e5..6c36aa9be 100644
--- a/go.sum
+++ b/go.sum
@@ -8,8 +8,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
-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/panjf2000/ants/v2 v2.10.0 h1:zhRg1pQUtkyRiOFo2Sbqwjp0GfBNo9cUY2/Grpx1p+8=
+github.com/panjf2000/ants/v2 v2.10.0/go.mod h1:7ZxyxsqE4vvW0M7LSD8aI3cKwgFhBHbxnlN8mDqHa1I=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -22,8 +22,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
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 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
-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=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
@@ -46,15 +46,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.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
-golang.org/x/sys v0.19.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/options.go b/options.go
index 86caef7fb..ed1f8e2ec 100644
--- a/options.go
+++ b/options.go
@@ -46,21 +46,23 @@ type Options struct {
// Multicore indicates whether the engine will be effectively created with multi-cores, if so,
// then you must take care with synchronizing memory between all event callbacks, otherwise,
- // it will run the engine with single thread. The number of threads in the engine will be automatically
- // assigned to the value of logical CPUs usable by the current process.
+ // it will run the engine with single thread. The number of threads in the engine will be
+ // automatically assigned to the number of usable logical CPUs that can be leveraged by the
+ // current process.
Multicore bool
- // NumEventLoop is set up to start the given number of event-loop goroutine.
- // Note: Setting up NumEventLoop will override Multicore.
+ // NumEventLoop is set up to start the given number of event-loop goroutines.
+ // Note that a non-negative NumEventLoop will override Multicore.
NumEventLoop int
- // LB represents the load-balancing algorithm used when assigning new connections.
+ // LB represents the load-balancing algorithm used when assigning new connections
+ // to event loops.
LB LoadBalancing
- // ReuseAddr indicates whether to set up the SO_REUSEADDR socket option.
+ // ReuseAddr indicates whether to set the SO_REUSEADDR socket option.
ReuseAddr bool
- // ReusePort indicates whether to set up the SO_REUSEPORT socket option.
+ // ReusePort indicates whether to set the SO_REUSEPORT socket option.
ReusePort bool
// MulticastInterfaceIndex is the index of the interface name where the multicast UDP addresses will be bound to.
@@ -77,46 +79,51 @@ type Options struct {
ReadBufferCap int
// WriteBufferCap is the maximum number of bytes that a static outbound buffer can hold,
- // if the data exceeds this value, the overflow will be stored in the elastic linked list buffer.
+ // if the data exceeds this value, the overflow bytes will be stored in the elastic linked list buffer.
// The default value is 64KB.
//
// Note that WriteBufferCap will always be converted to the least power of two integer value greater than
// or equal to its real amount.
WriteBufferCap int
- // LockOSThread is used to determine whether each I/O event-loop is associated to an OS thread, it is useful when you
- // need some kind of mechanisms like thread local storage, or invoke certain C libraries (such as graphics lib: GLib)
- // that require thread-level manipulation via cgo, or want all I/O event-loops to actually run in parallel for a
- // potential higher performance.
+ // LockOSThread is used to determine whether each I/O event-loop should be associated to an OS thread,
+ // it is useful when you need some kind of mechanisms like thread local storage, or invoke certain C
+ // libraries (such as graphics lib: GLib) that require thread-level manipulation via cgo, or want all I/O
+ // event-loops to actually run in parallel for a potential higher performance.
LockOSThread bool
// Ticker indicates whether the ticker has been set up.
Ticker bool
- // TCPKeepAlive sets up a duration for (SO_KEEPALIVE) socket option.
+ // TCPKeepAlive enable the TCP keep-alive mechanism (SO_KEEPALIVE) and set its value
+ // on TCP_KEEPIDLE, 1/5 of its value on TCP_KEEPINTVL, and 5 on TCP_KEEPCNT.
TCPKeepAlive time.Duration
// TCPNoDelay controls whether the operating system should delay
// packet transmission in hopes of sending fewer packets (Nagle's algorithm).
+ // When this option is assigned to TCPNoDelay, TCP_NODELAY socket option will
+ // be turned on, on the contrary, if it is assigned to TCPDelay, the socket
+ // option will be turned off.
//
- // The default is true (no delay), meaning that data is sent
- // as soon as possible after a write operation.
+ // The default is TCPNoDelay, meaning that TCP_NODELAY is turned on and data
+ // will not be buffered but sent as soon as possible after a write operation.
TCPNoDelay TCPSocketOpt
- // SocketRecvBuffer sets the maximum socket receive buffer in bytes.
+ // SocketRecvBuffer sets the maximum socket receive buffer of kernel in bytes.
SocketRecvBuffer int
- // SocketSendBuffer sets the maximum socket send buffer in bytes.
+ // SocketSendBuffer sets the maximum socket send buffer of kernel in bytes.
SocketSendBuffer int
- // LogPath the local path where logs will be written, this is the easiest way to set up logging,
- // gnet instantiates a default uber-go/zap logger with this given log path, you are also allowed to employ
- // you own logger during the lifetime by implementing the following log.Logger interface.
+ // LogPath specifies a local path where logs will be written, this is the easiest
+ // way to set up logging, gnet instantiates a default uber-go/zap logger with this
+ // given log path, you are also allowed to employ your own logger during the lifetime
+ // by implementing the following logging.Logger interface.
//
- // Note that this option can be overridden by the option Logger.
+ // Note that this option can be overridden by a non-nil option Logger.
LogPath string
- // LogLevel indicates the logging level, it should be used along with LogPath.
+ // LogLevel specifies the logging level, it should be used along with LogPath.
LogLevel logging.Level
// Logger is the customized logger for logging info, if it is not set,
@@ -136,63 +143,63 @@ func WithOptions(options Options) Option {
}
}
-// WithMulticore sets up multi-cores in gnet engine.
+// WithMulticore enables multi-cores mode for gnet engine.
func WithMulticore(multicore bool) Option {
return func(opts *Options) {
opts.Multicore = multicore
}
}
-// WithLockOSThread sets up LockOSThread mode for I/O event-loops.
+// WithLockOSThread enables LockOSThread mode for I/O event-loops.
func WithLockOSThread(lockOSThread bool) Option {
return func(opts *Options) {
opts.LockOSThread = lockOSThread
}
}
-// WithReadBufferCap sets up ReadBufferCap for reading bytes.
+// WithReadBufferCap sets ReadBufferCap for reading bytes.
func WithReadBufferCap(readBufferCap int) Option {
return func(opts *Options) {
opts.ReadBufferCap = readBufferCap
}
}
-// WithWriteBufferCap sets up WriteBufferCap for pending bytes.
+// WithWriteBufferCap sets WriteBufferCap for pending bytes.
func WithWriteBufferCap(writeBufferCap int) Option {
return func(opts *Options) {
opts.WriteBufferCap = writeBufferCap
}
}
-// WithLoadBalancing sets up the load-balancing algorithm in gnet engine.
+// WithLoadBalancing picks the load-balancing algorithm for gnet engine.
func WithLoadBalancing(lb LoadBalancing) Option {
return func(opts *Options) {
opts.LB = lb
}
}
-// WithNumEventLoop sets up NumEventLoop in gnet engine.
+// WithNumEventLoop sets the number of event loops for gnet engine.
func WithNumEventLoop(numEventLoop int) Option {
return func(opts *Options) {
opts.NumEventLoop = numEventLoop
}
}
-// WithReusePort sets up SO_REUSEPORT socket option.
+// WithReusePort sets SO_REUSEPORT socket option.
func WithReusePort(reusePort bool) Option {
return func(opts *Options) {
opts.ReusePort = reusePort
}
}
-// WithReuseAddr sets up SO_REUSEADDR socket option.
+// WithReuseAddr sets SO_REUSEADDR socket option.
func WithReuseAddr(reuseAddr bool) Option {
return func(opts *Options) {
opts.ReuseAddr = reuseAddr
}
}
-// WithTCPKeepAlive sets up the SO_KEEPALIVE socket option with duration.
+// WithTCPKeepAlive enables the TCP keep-alive mechanism and sets its values.
func WithTCPKeepAlive(tcpKeepAlive time.Duration) Option {
return func(opts *Options) {
opts.TCPKeepAlive = tcpKeepAlive
@@ -206,42 +213,42 @@ func WithTCPNoDelay(tcpNoDelay TCPSocketOpt) Option {
}
}
-// WithSocketRecvBuffer sets the maximum socket receive buffer in bytes.
+// WithSocketRecvBuffer sets the maximum socket receive buffer of kernel in bytes.
func WithSocketRecvBuffer(recvBuf int) Option {
return func(opts *Options) {
opts.SocketRecvBuffer = recvBuf
}
}
-// WithSocketSendBuffer sets the maximum socket send buffer in bytes.
+// WithSocketSendBuffer sets the maximum socket send buffer of kernel in bytes.
func WithSocketSendBuffer(sendBuf int) Option {
return func(opts *Options) {
opts.SocketSendBuffer = sendBuf
}
}
-// WithTicker indicates that a ticker is set.
+// WithTicker indicates whether a ticker is currently set.
func WithTicker(ticker bool) Option {
return func(opts *Options) {
opts.Ticker = ticker
}
}
-// WithLogPath is an option to set up the local path of log file.
+// WithLogPath specifies a local path for logging file.
func WithLogPath(fileName string) Option {
return func(opts *Options) {
opts.LogPath = fileName
}
}
-// WithLogLevel is an option to set up the logging level.
+// WithLogLevel specifies the logging level for the local logging file.
func WithLogLevel(lvl logging.Level) Option {
return func(opts *Options) {
opts.LogLevel = lvl
}
}
-// WithLogger sets up a customized logger.
+// WithLogger specifies a customized logger.
func WithLogger(logger logging.Logger) Option {
return func(opts *Options) {
opts.Logger = logger
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<