Skip to content

Commit

Permalink
read data only when matchers need some to determine match status
Browse files Browse the repository at this point in the history
  • Loading branch information
WeidiDeng committed Aug 12, 2024
1 parent 66db9b9 commit 8259243
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions layer4/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ func (routes RouteList) Compile(logger *zap.Logger, matchingTimeout time.Duratio
return err
}
for {
// only read more because there is no buffered data or matchers require more.
// only read more because matchers require more (no matcher in the simplest case).
// can happen if this routes list is embedded in another
if len(cx.buf) == 0 || matcherNeedMore {
if matcherNeedMore {
err = cx.prefetch()
if err != nil {
logFunc := logger.Error
Expand Down
27 changes: 26 additions & 1 deletion layer4/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package layer4

import (
"context"
"encoding/json"
"errors"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"io"
"net"
"testing"
"time"
Expand All @@ -13,11 +16,33 @@ import (
"go.uber.org/zap/zaptest/observer"
)

type testIoMatcher struct {
}

func (testIoMatcher) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "layer4.matchers.testIoMatcher",
New: func() caddy.Module { return new(testIoMatcher) },
}
}

func (m *testIoMatcher) Match(cx *Connection) (bool, error) {
buf := make([]byte, 1)
n, err := io.ReadFull(cx, buf)
return n > 0, err
}

func TestMatchingTimeoutWorks(t *testing.T) {
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()

routes := RouteList{&Route{}}
caddy.RegisterModule(testIoMatcher{})

routes := RouteList{&Route{
MatcherSetsRaw: caddyhttp.RawMatcherSets{
caddy.ModuleMap{"testIoMatcher": json.RawMessage("{}")}, // any io using matcher
},
}}

err := routes.Provision(ctx)
if err != nil {
Expand Down

0 comments on commit 8259243

Please sign in to comment.