Skip to content

Commit

Permalink
chore: use errors.Is for customized errors
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Sep 26, 2024
1 parent debb51b commit 0ad8b95
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 68 deletions.
9 changes: 5 additions & 4 deletions engine_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package gnet

import (
"context"
"errors"
"runtime"
"strings"
"sync"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand Down
24 changes: 8 additions & 16 deletions internal/netpoll/poller_epoll_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package netpoll

import (
"errors"
"os"
"runtime"
"sync/atomic"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -147,25 +145,19 @@ 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)
}
for i := 0; i < MaxAsyncTasksAtOneTime; i++ {
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)
}
Expand Down
24 changes: 8 additions & 16 deletions internal/netpoll/poller_epoll_ultimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package netpoll

import (
"errors"
"os"
"runtime"
"sync/atomic"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -149,25 +147,19 @@ 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)
}
for i := 0; i < MaxAsyncTasksAtOneTime; i++ {
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)
}
Expand Down
24 changes: 8 additions & 16 deletions internal/netpoll/poller_kqueue_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
package netpoll

import (
"errors"
"os"
"runtime"
"sync/atomic"

"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"
)

Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -132,25 +130,19 @@ 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)
}
for i := 0; i < MaxAsyncTasksAtOneTime; i++ {
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)
}
Expand Down
24 changes: 8 additions & 16 deletions internal/netpoll/poller_kqueue_ultimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package netpoll

import (
"errors"
"os"
"runtime"
"sync/atomic"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -134,25 +132,19 @@ 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)
}
for i := 0; i < MaxAsyncTasksAtOneTime; i++ {
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)
}
Expand Down

0 comments on commit 0ad8b95

Please sign in to comment.