Skip to content

Commit

Permalink
dont print err upon signal term
Browse files Browse the repository at this point in the history
  • Loading branch information
flowerinthenight committed Nov 22, 2024
1 parent d93d949 commit 4699caa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ func main() {
go func() {
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, syscall.SIGINT, syscall.SIGTERM)
log.Printf("signal: %v", <-sigch)
<-sigch
cancel()
}()

Expand Down
13 changes: 12 additions & 1 deletion hedge.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,25 @@ func (op *Op) Run(ctx context.Context, done ...chan error) error {
return err
}

var exitedTCP atomic.Int32
doneTCP := make(chan error, 1)

// This connection will be closed upon context termination.
tl, err := net.ListenTCP("tcp", addr)
if err != nil {
return err
}

defer tl.Close()
op.logger.Printf("tcp: listen on %v", op.hostPort)

go func() {
defer func() { doneTCP <- nil }()
for {
conn, err := tl.Accept()
if exitedTCP.Load() == 1 {
return
}

if err != nil {
op.logger.Printf("Accept failed: %v", err)
return
Expand Down Expand Up @@ -499,6 +507,9 @@ func (op *Op) Run(ctx context.Context, done ...chan error) error {

<-ctx.Done() // wait for termination

exitedTCP.Store(1) // don't print err in tl.Accept
tl.Close() // will cause tl.Accept to fail

gs.GracefulStop() // stop grpc server
if op.ensureOn.Load() == 1 {
op.ensureCancel() // stop semaphore checker;
Expand Down

0 comments on commit 4699caa

Please sign in to comment.