From b41adddd470dc822c2c8e30230363f8a709df165 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 13 Aug 2023 17:08:14 +0800 Subject: [PATCH 1/5] chore: bump up modules (#493) * chore: bump up modules * chore: only run tests against the minimum supported version and latest stable version of Go --- .github/workflows/test.yml | 2 +- .github/workflows/test_gc_opt.yml | 2 +- .github/workflows/test_poll_opt.yml | 2 +- .github/workflows/test_poll_opt_gc_opt.yml | 2 +- go.mod | 6 +++--- go.sum | 12 ++++++------ 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 776c3f0f4..97531f944 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -55,7 +55,7 @@ jobs: strategy: fail-fast: false matrix: - go: ['1.17', '1.18', '1.19', '1.20'] + go: ['1.17', '1.21'] os: - ubuntu-latest - macos-latest diff --git a/.github/workflows/test_gc_opt.yml b/.github/workflows/test_gc_opt.yml index c18da9b61..318736650 100644 --- a/.github/workflows/test_gc_opt.yml +++ b/.github/workflows/test_gc_opt.yml @@ -55,7 +55,7 @@ jobs: strategy: fail-fast: false matrix: - go: ['1.17', '1.18', '1.19', '1.20'] + go: ['1.17', '1.21'] os: - ubuntu-latest - macos-latest diff --git a/.github/workflows/test_poll_opt.yml b/.github/workflows/test_poll_opt.yml index 8f1337d77..53f908da1 100644 --- a/.github/workflows/test_poll_opt.yml +++ b/.github/workflows/test_poll_opt.yml @@ -54,7 +54,7 @@ jobs: strategy: fail-fast: false matrix: - go: ['1.17', '1.18', '1.19', '1.20'] + go: ['1.17', '1.21'] os: [ubuntu-latest, macos-latest] name: Go ${{ matrix.go }} @ ${{ matrix.os }} runs-on: ${{ matrix.os }} diff --git a/.github/workflows/test_poll_opt_gc_opt.yml b/.github/workflows/test_poll_opt_gc_opt.yml index 5af9f93aa..e4d8f2565 100644 --- a/.github/workflows/test_poll_opt_gc_opt.yml +++ b/.github/workflows/test_poll_opt_gc_opt.yml @@ -54,7 +54,7 @@ jobs: strategy: fail-fast: false matrix: - go: ['1.17', '1.18', '1.19', '1.20'] + go: ['1.17', '1.21'] os: [ubuntu-latest, macos-latest] name: Go ${{ matrix.go }} @ ${{ matrix.os }} runs-on: ${{ matrix.os }} diff --git a/go.mod b/go.mod index f47d80576..819ecbc73 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,11 @@ module github.com/panjf2000/gnet/v2 require ( github.com/panjf2000/ants/v2 v2.8.1 - github.com/stretchr/testify v1.8.2 + github.com/stretchr/testify v1.8.4 github.com/valyala/bytebufferpool v1.0.0 go.uber.org/zap v1.21.0 - golang.org/x/sync v0.2.0 - golang.org/x/sys v0.8.0 + golang.org/x/sync v0.3.0 + golang.org/x/sys v0.11.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) diff --git a/go.sum b/go.sum index f10e6e250..ba271dfd5 100644 --- a/go.sum +++ b/go.sum @@ -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.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -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/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.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= -golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-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.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 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= From 89672dccd5fcbac8a789773c122b6dd4e64c9472 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Fri, 18 Aug 2023 16:49:15 +0800 Subject: [PATCH 2/5] chore: add a TODO about removing the callback in Conn.AsyncWrite with UDP (#494) Also eliminate the defer for callback in Conn.AsyncWrite with UDP --- connection_unix.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/connection_unix.go b/connection_unix.go index 23555c906..053729273 100644 --- a/connection_unix.go +++ b/connection_unix.go @@ -422,12 +422,15 @@ func (c *conn) SetKeepAlivePeriod(d time.Duration) error { func (c *conn) AsyncWrite(buf []byte, callback AsyncCallback) error { if c.isDatagram { - defer func() { - if callback != nil { - _ = callback(nil, nil) - } - }() - return c.sendTo(buf) + err := c.sendTo(buf) + // TODO: it will not go asynchronously with UDP, so calling a callback is needless, + // we may remove this branch in the future, please don't rely on the callback + // to do something important under UDP, if you're working with UDP, just call Conn.Write + // to send back your data. + if callback != nil { + _ = callback(nil, nil) + } + return err } return c.loop.poller.Trigger(c.asyncWrite, &asyncWriteHook{callback, buf}) } From 3f217f9b7e9667cc66ab433410f431879be8d3f5 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 10 Sep 2023 18:31:37 +0800 Subject: [PATCH 3/5] chore: do some chores on tests and comments (#499) * test: restore the default logger after tests * chore: add a supplement comment about UDP on Conn.AsyncWrite --- gnet.go | 6 ++++++ gnet_test.go | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/gnet.go b/gnet.go index 047b4ef98..fd3098913 100644 --- a/gnet.go +++ b/gnet.go @@ -226,6 +226,12 @@ type Writer interface { // AsyncWrite writes bytes to peer asynchronously, it's goroutine-safe, // you don't have to invoke it within any method in EventHandler, // usually you would call it in an individual goroutine. + // + // Note that it will go synchronously with UDP, so it is needless to call + // this asynchronous method, we may disable this method for UDP and just + // return ErrUnsupportedOp in the future, therefore, please don't rely on + // this method to do something important under UDP, if you're working with UDP, + // just call Conn.Write to send back your data. AsyncWrite(buf []byte, callback AsyncCallback) (err error) // AsyncWritev writes multiple byte slices to peer asynchronously, diff --git a/gnet_test.go b/gnet_test.go index abf5ee569..927dbad95 100644 --- a/gnet_test.go +++ b/gnet_test.go @@ -552,6 +552,11 @@ func (t *testWakeConnServer) OnTick() (delay time.Duration, action Action) { } func testWakeConn(t *testing.T, network, addr string) { + currentLogger, currentFlusher := logging.GetDefaultLogger(), logging.GetDefaultFlusher() + t.Cleanup(func() { + logging.SetDefaultLoggerAndFlusher(currentLogger, currentFlusher) // restore + }) + svr := &testWakeConnServer{tester: t, network: network, addr: addr, conn: make(chan Conn, 1)} logger := zap.NewExample() err := Run(svr, network+"://"+addr, @@ -1145,6 +1150,11 @@ func (t *testMultiInstLoggerRaceServer) OnBoot(_ Engine) (action Action) { } func TestMultiInstLoggerRace(t *testing.T) { + currentLogger, currentFlusher := logging.GetDefaultLogger(), logging.GetDefaultFlusher() + t.Cleanup(func() { + logging.SetDefaultLoggerAndFlusher(currentLogger, currentFlusher) // restore + }) + logger1, _ := zap.NewDevelopment() events1 := new(testMultiInstLoggerRaceServer) g := errgroup.Group{} From f7830a40048644ea7596d800757f273fb8aaef15 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 10 Sep 2023 20:05:27 +0800 Subject: [PATCH 4/5] opt: eliminate error logs when the service exits normally (#500) Fixes #497 --- acceptor_windows.go | 22 ++++++++++++++++++++-- engine_windows.go | 12 +++++++----- listener_windows.go | 5 +---- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/acceptor_windows.go b/acceptor_windows.go index 6b6d01861..13d551f82 100644 --- a/acceptor_windows.go +++ b/acceptor_windows.go @@ -15,8 +15,12 @@ package gnet import ( + "errors" "net" "runtime" + "sync/atomic" + + errorx "github.com/panjf2000/gnet/v2/pkg/errors" ) func (eng *engine) listen() (err error) { @@ -34,7 +38,15 @@ func (eng *engine) listen() (err error) { n, addr, e := eng.ln.pc.ReadFrom(buffer[:]) if e != nil { err = e - eng.opts.Logger.Errorf("failed to receive data from UDP fd due to error:%v", err) + 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: 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) + } return } @@ -46,7 +58,13 @@ func (eng *engine) listen() (err error) { tc, e := eng.ln.ln.Accept() if e != nil { err = e - eng.opts.Logger.Errorf("Accept() fails due to error: %v", err) + 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: ditto. + // err = errors.Join(err, errorx.ErrEngineShutdown) + } return } el := eng.eventLoops.next(tc.RemoteAddr()) diff --git a/engine_windows.go b/engine_windows.go index f2142c6c8..d0d3cb304 100644 --- a/engine_windows.go +++ b/engine_windows.go @@ -16,6 +16,7 @@ package gnet import ( "context" + "errors" "runtime" "sync" "sync/atomic" @@ -33,8 +34,9 @@ type engine struct { ctx context.Context cancel context.CancelFunc } - inShutdown int32 // whether the engine is in shutdown - workerPool struct { + inShutdown int32 // whether the engine is in shutdown + beingShutdown int32 // whether the engine is being shutdown + workerPool struct { *errgroup.Group shutdownCtx context.Context @@ -50,10 +52,11 @@ func (eng *engine) isInShutdown() bool { // shutdown signals the engine to shut down. func (eng *engine) shutdown(err error) { - if err != nil && err != errorx.ErrEngineShutdown { + if err != nil && !errors.Is(err, errorx.ErrEngineShutdown) { eng.opts.Logger.Errorf("engine is being shutdown with error: %v", err) } eng.workerPool.shutdown() + atomic.StoreInt32(&eng.beingShutdown, 1) } func (eng *engine) closeEventLoops() { @@ -91,7 +94,6 @@ func (eng *engine) start(numEventLoop int) error { func (eng *engine) stop(engine Engine) error { <-eng.workerPool.shutdownCtx.Done() - eng.opts.Logger.Infof("engine is being shutdown...") eng.eventHandler.OnShutdown(engine) if eng.ticker.cancel != nil { @@ -100,7 +102,7 @@ func (eng *engine) stop(engine Engine) error { eng.closeEventLoops() - if err := eng.workerPool.Wait(); err != nil { + if err := eng.workerPool.Wait(); err != nil && !errors.Is(err, errorx.ErrEngineShutdown) { eng.opts.Logger.Errorf("engine shutdown error: %v", err) } diff --git a/listener_windows.go b/listener_windows.go index 2d554c361..1e92cdf46 100644 --- a/listener_windows.go +++ b/listener_windows.go @@ -90,9 +90,6 @@ func (l *listener) close() { return } logging.Error(os.NewSyscallError("close", l.ln.Close())) - if l.network == "unix" { - logging.Error(os.RemoveAll(l.address)) - } }) } @@ -124,7 +121,7 @@ func initListener(network, addr string, options *Options) (l *listener, err erro } l.addr = l.pc.LocalAddr() case "unix": - logging.Error(os.Remove(addr)) + _ = os.Remove(addr) fallthrough case "tcp", "tcp4", "tcp6": if l.ln, err = lc.Listen(context.Background(), network, addr); err != nil { From 7699eae1fbcb17408e6fe500d0ebf19493ad4b85 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 10 Sep 2023 20:23:27 +0800 Subject: [PATCH 5/5] chore: consolidate the import declarations of the internal error package (#501) --- client_test.go | 10 +++++----- client_unix.go | 6 +++--- connection_unix.go | 12 ++++++------ eventloop_unix.go | 18 +++++++++--------- gnet_test.go | 14 +++++++------- pkg/buffer/elastic/elastic_ring_list_buffer.go | 4 ++-- 6 files changed, 32 insertions(+), 32 deletions(-) diff --git a/client_test.go b/client_test.go index 57b0b87c3..02a7637ab 100644 --- a/client_test.go +++ b/client_test.go @@ -14,7 +14,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - gerr "github.com/panjf2000/gnet/v2/pkg/errors" + errorx "github.com/panjf2000/gnet/v2/pkg/errors" "github.com/panjf2000/gnet/v2/pkg/logging" bbPool "github.com/panjf2000/gnet/v2/pkg/pool/bytebuffer" goPool "github.com/panjf2000/gnet/v2/pkg/pool/goroutine" @@ -30,8 +30,8 @@ type clientEvents struct { func (ev *clientEvents) OnBoot(e Engine) Action { fd, err := e.Dup() - require.ErrorIsf(ev.tester, err, gerr.ErrEmptyEngine, "expected error: %v, but got: %v", - gerr.ErrUnsupportedOp, err) + require.ErrorIsf(ev.tester, err, errorx.ErrEmptyEngine, "expected error: %v, but got: %v", + errorx.ErrUnsupportedOp, err) assert.EqualValuesf(ev.tester, fd, -1, "expected -1, but got: %d", fd) return None } @@ -80,8 +80,8 @@ func (ev *clientEvents) OnTick() (delay time.Duration, action Action) { func (ev *clientEvents) OnShutdown(e Engine) { fd, err := e.Dup() - require.ErrorIsf(ev.tester, err, gerr.ErrEmptyEngine, "expected error: %v, but got: %v", - gerr.ErrUnsupportedOp, err) + require.ErrorIsf(ev.tester, err, errorx.ErrEmptyEngine, "expected error: %v, but got: %v", + errorx.ErrUnsupportedOp, err) assert.EqualValuesf(ev.tester, fd, -1, "expected -1, but got: %d", fd) } diff --git a/client_unix.go b/client_unix.go index 61d2a710e..70bf37c7c 100644 --- a/client_unix.go +++ b/client_unix.go @@ -32,7 +32,7 @@ import ( "github.com/panjf2000/gnet/v2/internal/netpoll" "github.com/panjf2000/gnet/v2/internal/socket" "github.com/panjf2000/gnet/v2/pkg/buffer/ring" - gerrors "github.com/panjf2000/gnet/v2/pkg/errors" + errorx "github.com/panjf2000/gnet/v2/pkg/errors" "github.com/panjf2000/gnet/v2/pkg/logging" ) @@ -126,7 +126,7 @@ func (cli *Client) Start() error { // Stop stops the client event-loop. func (cli *Client) Stop() (err error) { - logging.Error(cli.el.poller.UrgentTrigger(func(_ interface{}) error { return gerrors.ErrEngineShutdown }, nil)) + logging.Error(cli.el.poller.UrgentTrigger(func(_ interface{}) error { return errorx.ErrEngineShutdown }, nil)) // Stop the ticker. if cli.opts.Ticker { cli.el.engine.ticker.cancel() @@ -215,7 +215,7 @@ func (cli *Client) Enroll(c net.Conn) (Conn, error) { } gc = newUDPConn(dupFD, cli.el, c.LocalAddr(), sockAddr, true) default: - return nil, gerrors.ErrUnsupportedProtocol + return nil, errorx.ErrUnsupportedProtocol } err = cli.el.poller.UrgentTrigger(cli.el.register, gc) if err != nil { diff --git a/connection_unix.go b/connection_unix.go index 053729273..9f5c6f403 100644 --- a/connection_unix.go +++ b/connection_unix.go @@ -31,7 +31,7 @@ import ( "github.com/panjf2000/gnet/v2/internal/netpoll" "github.com/panjf2000/gnet/v2/internal/socket" "github.com/panjf2000/gnet/v2/pkg/buffer/elastic" - gerrors "github.com/panjf2000/gnet/v2/pkg/errors" + errorx "github.com/panjf2000/gnet/v2/pkg/errors" "github.com/panjf2000/gnet/v2/pkg/logging" bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice" ) @@ -355,7 +355,7 @@ func (c *conn) Write(p []byte) (int, error) { func (c *conn) Writev(bs [][]byte) (int, error) { if c.isDatagram { - return 0, gerrors.ErrUnsupportedOp + return 0, errorx.ErrUnsupportedOp } return c.writev(bs) } @@ -437,7 +437,7 @@ func (c *conn) AsyncWrite(buf []byte, callback AsyncCallback) error { func (c *conn) AsyncWritev(bs [][]byte, callback AsyncCallback) error { if c.isDatagram { - return gerrors.ErrUnsupportedOp + return errorx.ErrUnsupportedOp } return c.loop.poller.Trigger(c.asyncWritev, &asyncWritevHook{callback, bs}) } @@ -470,13 +470,13 @@ func (c *conn) Close() error { } func (*conn) SetDeadline(_ time.Time) error { - return gerrors.ErrUnsupportedOp + return errorx.ErrUnsupportedOp } func (*conn) SetReadDeadline(_ time.Time) error { - return gerrors.ErrUnsupportedOp + return errorx.ErrUnsupportedOp } func (*conn) SetWriteDeadline(_ time.Time) error { - return gerrors.ErrUnsupportedOp + return errorx.ErrUnsupportedOp } diff --git a/eventloop_unix.go b/eventloop_unix.go index e10b12fbc..67cc1aabb 100644 --- a/eventloop_unix.go +++ b/eventloop_unix.go @@ -30,7 +30,7 @@ import ( "github.com/panjf2000/gnet/v2/internal/io" "github.com/panjf2000/gnet/v2/internal/netpoll" - gerrors "github.com/panjf2000/gnet/v2/pkg/errors" + errorx "github.com/panjf2000/gnet/v2/pkg/errors" "github.com/panjf2000/gnet/v2/pkg/logging" ) @@ -115,7 +115,7 @@ func (el *eventloop) read(c *conn) error { case Close: return el.close(c, nil) case Shutdown: - return gerrors.ErrEngineShutdown + return errorx.ErrEngineShutdown } _, _ = c.inboundBuffer.Write(c.buffer) c.buffer = c.buffer[:0] @@ -165,7 +165,7 @@ func (el *eventloop) close(c *conn, err error) (rerr error) { el.connections.delConn(c) } if el.eventHandler.OnClose(c, err) == Shutdown { - return gerrors.ErrEngineShutdown + return errorx.ErrEngineShutdown } c.release() return @@ -206,7 +206,7 @@ func (el *eventloop) close(c *conn, err error) (rerr error) { el.connections.delConn(c) if el.eventHandler.OnClose(c, err) == Shutdown { - rerr = gerrors.ErrEngineShutdown + rerr = errorx.ErrEngineShutdown } c.release() @@ -242,7 +242,7 @@ func (el *eventloop) ticker(ctx context.Context) { switch action { case None: case Shutdown: - err := el.poller.UrgentTrigger(func(_ interface{}) error { return gerrors.ErrEngineShutdown }, nil) + err := el.poller.UrgentTrigger(func(_ interface{}) error { return errorx.ErrEngineShutdown }, nil) el.getLogger().Debugf("stopping ticker in event-loop(%d) from OnTick(), UrgentTrigger:%v", el.idx, err) } if timer == nil { @@ -266,7 +266,7 @@ func (el *eventloop) handleAction(c *conn, action Action) error { case Close: return el.close(c, nil) case Shutdown: - return gerrors.ErrEngineShutdown + return errorx.ErrEngineShutdown default: return nil } @@ -293,7 +293,7 @@ func (el *eventloop) readUDP(fd int, _ netpoll.IOEvent) error { c.release() } if action == Shutdown { - return gerrors.ErrEngineShutdown + return errorx.ErrEngineShutdown } return nil } @@ -303,7 +303,7 @@ func (el *eventloop) execCmd(itf interface{}) (err error) { cmd := itf.(*asyncCmd) c := el.connections.getConnByGFD(cmd.fd) if c == nil || c.gfd != cmd.fd { - return gerrors.ErrInvalidConn + return errorx.ErrInvalidConn } defer func() { @@ -322,7 +322,7 @@ func (el *eventloop) execCmd(itf interface{}) (err error) { case asyncCmdWritev: _, err = c.Writev(cmd.arg.([][]byte)) default: - return gerrors.ErrUnsupportedOp + return errorx.ErrUnsupportedOp } return } diff --git a/gnet_test.go b/gnet_test.go index 927dbad95..23446a965 100644 --- a/gnet_test.go +++ b/gnet_test.go @@ -19,7 +19,7 @@ import ( "go.uber.org/zap" "golang.org/x/sync/errgroup" - gerr "github.com/panjf2000/gnet/v2/pkg/errors" + errorx "github.com/panjf2000/gnet/v2/pkg/errors" "github.com/panjf2000/gnet/v2/pkg/logging" bbPool "github.com/panjf2000/gnet/v2/pkg/pool/bytebuffer" goPool "github.com/panjf2000/gnet/v2/pkg/pool/goroutine" @@ -814,8 +814,8 @@ func testShutdownActionOnOpen(t *testing.T, network, addr string) { err := Run(events, network+"://"+addr, WithTicker(true)) assert.NoError(t, err) _, err = events.eng.Dup() - assert.ErrorIsf(t, err, gerr.ErrEngineInShutdown, "expected error: %v, but got: %v", - gerr.ErrEngineInShutdown, err) + assert.ErrorIsf(t, err, errorx.ErrEngineInShutdown, "expected error: %v, but got: %v", + errorx.ErrEngineInShutdown, err) } func TestUDPShutdown(t *testing.T) { @@ -887,7 +887,7 @@ func (t *testCloseConnectionServer) OnTraffic(c Conn) (action Action) { go func() { time.Sleep(time.Second) _ = c.CloseWithCallback(func(c Conn, err error) error { - assert.ErrorIsf(t.tester, err, gerr.ErrEngineShutdown, "should be engine shutdown error") + assert.ErrorIsf(t.tester, err, errorx.ErrEngineShutdown, "should be engine shutdown error") return nil }) }() @@ -923,7 +923,7 @@ func testCloseConnection(t *testing.T, network, addr string) { func TestServerOptionsCheck(t *testing.T) { err := Run(&BuiltinEventEngine{}, "tcp://:3500", WithNumEventLoop(10001), WithLockOSThread(true)) - assert.EqualError(t, err, gerr.ErrTooManyEventLoopThreads.Error(), "error returned with LockOSThread option") + assert.EqualError(t, err, errorx.ErrTooManyEventLoopThreads.Error(), "error returned with LockOSThread option") } func TestStopServer(t *testing.T) { @@ -1066,7 +1066,7 @@ func testEngineStop(t *testing.T, network, addr string) { require.Greater(t, events2.exchngCount, int64(0)) require.Equal(t, int64(2+1+5+1), events1.exchngCount+events2.exchngCount) // stop an already stopped engine - require.Equal(t, gerr.ErrEngineInShutdown, events1.eng.Stop(context.Background())) + require.Equal(t, errorx.ErrEngineInShutdown, events1.eng.Stop(context.Background())) } // Test should not panic when we wake-up server_closed conn. @@ -1170,7 +1170,7 @@ func TestMultiInstLoggerRace(t *testing.T) { return err }) - assert.ErrorIs(t, g.Wait(), gerr.ErrUnsupportedProtocol) + assert.ErrorIs(t, g.Wait(), errorx.ErrUnsupportedProtocol) } var errIncompletePacket = errors.New("incomplete packet") diff --git a/pkg/buffer/elastic/elastic_ring_list_buffer.go b/pkg/buffer/elastic/elastic_ring_list_buffer.go index 8bc521982..d16fd3803 100644 --- a/pkg/buffer/elastic/elastic_ring_list_buffer.go +++ b/pkg/buffer/elastic/elastic_ring_list_buffer.go @@ -19,7 +19,7 @@ import ( "math" "github.com/panjf2000/gnet/v2/pkg/buffer/linkedlist" - gerrors "github.com/panjf2000/gnet/v2/pkg/errors" + errorx "github.com/panjf2000/gnet/v2/pkg/errors" ) // Buffer combines ring-buffer and list-buffer. @@ -35,7 +35,7 @@ type Buffer struct { // New instantiates an elastic.Buffer and returns it. func New(maxStaticBytes int) (*Buffer, error) { if maxStaticBytes <= 0 { - return nil, gerrors.ErrNegativeSize + return nil, errorx.ErrNegativeSize } return &Buffer{maxStaticBytes: maxStaticBytes}, nil }