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