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

fix: skip already seen routes after a match #196

Merged
merged 1 commit into from
Jun 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
13 changes: 12 additions & 1 deletion layer4/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (routes RouteList) Provision(ctx caddy.Context) error {
func (routes RouteList) Compile(logger *zap.Logger, matchingTimeout time.Duration, next NextHandler) Handler {
return HandlerFunc(func(cx *Connection) error {
deadline := time.Now().Add(matchingTimeout)
lastMatchedRouteIdx := -1
router:
// timeout matching to protect against malicious or very slow clients
err := cx.Conn.SetReadDeadline(deadline)
Expand All @@ -127,7 +128,16 @@ func (routes RouteList) Compile(logger *zap.Logger, matchingTimeout time.Duratio
}
}

for _, route := range routes {
for i, route := range routes {
// After a match continue with the routes after the matched one, instead of starting at the beginning.
// This is done for backwards compatibility with configs written before the "Non blocking matchers & matching timeout" rewrite.
// See https://github.com/mholt/caddy-l4/pull/192 and https://github.com/mholt/caddy-l4/pull/192#issuecomment-2143681952.
if i <= lastMatchedRouteIdx {
continue
}
// Only skip once after a match, so it behaves like we continued after the match.
lastMatchedRouteIdx = -1

// A route must match at least one of the matcher sets
matched, err := route.matcherSets.AnyMatch(cx)
if errors.Is(err, ErrConsumedAllPrefetchedBytes) {
Expand Down Expand Up @@ -168,6 +178,7 @@ func (routes RouteList) Compile(logger *zap.Logger, matchingTimeout time.Duratio
if isTerminal {
return nil
} else {
lastMatchedRouteIdx = i
goto router
}
}
Expand Down
Loading