From 7699eae1fbcb17408e6fe500d0ebf19493ad4b85 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 10 Sep 2023 20:23:27 +0800 Subject: [PATCH] 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 }