Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: eliminate error logs when the service exits normally #500

Merged
merged 1 commit into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions acceptor_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -34,7 +38,15 @@
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)

Check warning on line 42 in acceptor_windows.go

View check run for this annotation

Codecov / codecov/patch

acceptor_windows.go#L42

Added line #L42 was not covered by tests
} 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
}

Expand All @@ -46,7 +58,13 @@
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)

Check warning on line 62 in acceptor_windows.go

View check run for this annotation

Codecov / codecov/patch

acceptor_windows.go#L62

Added line #L62 was not covered by tests
} 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())
Expand Down
12 changes: 7 additions & 5 deletions engine_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package gnet

import (
"context"
"errors"
"runtime"
"sync"
"sync/atomic"
Expand All @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down
5 changes: 1 addition & 4 deletions listener_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
}

Expand Down Expand Up @@ -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 {
Expand Down
Loading