Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions cmd/tailcat/perf.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,25 @@ func probePath(ctx context.Context, cl *tailcat.Client) (pathInfo, error) {
// discovery along.
func waitForDirectPath(ctx context.Context, cl *tailcat.Client, timeout time.Duration) (pathInfo, error) {
deadline := time.Now().Add(timeout)
var last *pathInfo
for {
t0 := time.Now()
pingCtx, cancel := context.WithDeadline(ctx, deadline)
p, err := probePath(pingCtx, cl)
cancel()
if err != nil {
if ctx.Err() == nil && errors.Is(err, context.DeadlineExceeded) {
if last != nil {
return *last, nil
}
return pathInfo{}, fmt.Errorf("no reply to pings after %v", timeout)
}
return pathInfo{}, err
}
if p.Direct || time.Until(deadline) < time.Second/2 {
return p, nil
}
last = &p
select {
case <-ctx.Done():
return pathInfo{}, ctx.Err()
Expand Down
28 changes: 28 additions & 0 deletions cmd/tailcat/perf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"encoding/json"
"regexp"
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -97,6 +98,33 @@ func TestPerf(t *testing.T) {
t.Errorf("no RTT samples in %+v", got.Result)
}
})

// A path that stays relayed must still come back from the wait for
// a direct path, whatever --timeout is. Whole seconds used to fail:
// the last once-a-second ping saw about 1s left, slept past the
// deadline, and reported "no reply to pings".
t.Run("relayed", func(t *testing.T) {
relayedPerf := func(args ...string) ([]byte, error) {
all := append([]string{"--key=new", "--derpmap-url=" + e.derpMapURL, "perf", "--timeout=2s", "--time=1s", "--interval=0"}, args...)
all = append(all, addr)
cmd := e.cmd(all...)
cmd.Env = append(slices.Clone(cmd.Env), "TS_DEBUG_ALWAYS_USE_DERP=1")
return cmd.CombinedOutput()
}

out, err := relayedPerf("--via-derp")
if err != nil {
t.Fatalf("perf --via-derp: %v\n%s", err, out)
}
if !regexp.MustCompile(`(?m)^# path: relayed via DERP\(`).Match(out) {
t.Errorf("output missing relayed path:\n%s", out)
}

out, err = relayedPerf()
if err == nil || !strings.Contains(string(out), "no direct path to the server") {
t.Errorf("perf without --via-derp: err=%v, want a no-direct-path refusal:\n%s", err, out)
}
})
}

// TestPerfServeRejectsPortClash checks that a port list can't proxy
Expand Down