Skip to content

Commit

Permalink
fix: use nil to signal connection was hijacked by caddy http
Browse files Browse the repository at this point in the history
Otherwise an error would be logged in routes.Compile
  • Loading branch information
ydylla committed Aug 6, 2024
1 parent d3998f5 commit 2966697
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions layer4/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,13 @@ func (l *listener) loop() {
}
}

// errHijacked is used when a handler takes over the connection, it's lifetime is not managed by handle
var errHijacked = errors.New("hijacked connection")

func (l *listener) handle(conn net.Conn) {
var err error
defer func() {
l.wg.Done()
if err != errHijacked {
// If there is no err the connection was successfully hijacked
// by the caddy http app which is now responsible for closing it
if err != nil {
conn.Close()
}
}()
Expand All @@ -171,7 +170,7 @@ func (l *listener) handle(conn net.Conn) {
start := time.Now()
err = l.compiledRoute.Handle(cx)
duration := time.Since(start)
if err != nil && err != errHijacked {
if err != nil {
logFunc := l.logger.Error
if errors.Is(err, ErrMatchingTimeout) {
logFunc = l.logger.Warn
Expand Down Expand Up @@ -210,7 +209,7 @@ func (l *listener) pipeConnection(conn *Connection) error {
} else {
l.connChan <- conn
}
return errHijacked
return nil
}

// tlsConnection implements ConnectionState interface to use it with h2
Expand Down

0 comments on commit 2966697

Please sign in to comment.