Skip to content

Commit

Permalink
opt: eliminate error logs when the service exits normally
Browse files Browse the repository at this point in the history
Fixes #497
  • Loading branch information
panjf2000 committed Sep 10, 2023
1 parent 89672dc commit 17f839b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 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)

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

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
11 changes: 7 additions & 4 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 @@ -100,7 +103,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

0 comments on commit 17f839b

Please sign in to comment.