Skip to content

Commit

Permalink
stop the loop when listener is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
WeidiDeng committed Oct 28, 2024
1 parent 16b3b20 commit e06861f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions layer4/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"runtime"
"sync"
"sync/atomic"
"time"

"github.com/caddyserver/caddy/v2"
Expand Down Expand Up @@ -114,6 +115,7 @@ type listener struct {
logger *zap.Logger
compiledRoute Handler

closed atomic.Bool
// closed when there is a non-recoverable error and all handle goroutines are done
connChan chan net.Conn
err error
Expand All @@ -122,10 +124,21 @@ type listener struct {
wg *sync.WaitGroup
}

func (l *listener) Close() error {
err := l.Listener.Close()
l.closed.Store(true)
return err
}

// loop accept connection from underlying listener and pipe the connection if there are any
func (l *listener) loop() {
for {
conn, err := l.Listener.Accept()
// listener closed
if l.closed.Load() {
break
}

var nerr net.Error
if errors.As(err, &nerr) && nerr.Temporary() {
l.logger.Error("temporary error accepting connection", zap.Error(err))
Expand Down

0 comments on commit e06861f

Please sign in to comment.