Skip to content

Commit

Permalink
opt: eliminate error logs when the service exits normally (#500)
Browse files Browse the repository at this point in the history
Fixes #497
  • Loading branch information
panjf2000 authored Sep 10, 2023
1 parent 3f217f9 commit f7830a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
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 @@ 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
}

Expand All @@ -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())
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

0 comments on commit f7830a4

Please sign in to comment.