Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop the loop when listener is closed #261

Merged
merged 5 commits into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 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 @@ -65,6 +66,7 @@ func (lw *ListenerWrapper) WrapListener(l net.Listener) net.Listener {
Listener: l,
logger: lw.logger,
compiledRoute: lw.compiledRoute,
done: make(chan struct{}),
connChan: connChan,
wg: new(sync.WaitGroup),
}
Expand Down Expand Up @@ -114,25 +116,30 @@ type listener struct {
logger *zap.Logger
compiledRoute Handler

closed atomic.Bool
mholt marked this conversation as resolved.
Show resolved Hide resolved
done chan struct{}
// closed when there is a non-recoverable error and all handle goroutines are done
connChan chan net.Conn
err error

// count running handles
wg *sync.WaitGroup
}

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

// loop accept connection from underlying listener and pipe the connection if there are any
func (l *listener) loop() {
for {
conn, err := l.Listener.Accept()
var nerr net.Error
if errors.As(err, &nerr) && nerr.Temporary() {
if errors.As(err, &nerr) && nerr.Temporary() && !l.closed.Load() {
l.logger.Error("temporary error accepting connection", zap.Error(err))
continue
}
if err != nil {
l.err = err
break
}

Expand All @@ -145,6 +152,7 @@ func (l *listener) loop() {
l.wg.Wait()
close(l.connChan)
}()
close(l.done)
for conn := range l.connChan {
_ = conn.Close()
}
Expand Down Expand Up @@ -185,11 +193,15 @@ func (l *listener) handle(conn net.Conn) {
}

func (l *listener) Accept() (net.Conn, error) {
for conn := range l.connChan {
return conn, nil
select {
case conn, ok := <-l.connChan:
if ok {
return conn, nil
}
return nil, net.ErrClosed
case <-l.done:
return nil, net.ErrClosed
}
return nil, l.err

}

func (l *listener) pipeConnection(conn *Connection) error {
Expand Down
Loading