Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pkgs/mcpx/internal/cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func NewClientAt(paths daemon.Paths, configPath, endpoint string) *Client {
// no address to fall back to, and the dialer says so when it cannot
// join (no TS_AUTHKEY) instead of retrying the name as plain TCP.
if host, port, isTSNet, perr := daemon.ParseTSNetEndpoint(c.endpoint); isTSNet {
dialer := daemon.NewTSNetDialer("mcpx-client")
dialer := daemon.NewTSNetDialer("mcpx-client", set.Duration("daemon.tsnetUpTimeout"))
dial := dialer.DialContext
if perr != nil {
dial = func(context.Context, string, string) (net.Conn, error) { return nil, perr }
Expand Down
7 changes: 4 additions & 3 deletions pkgs/mcpx/internal/daemon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,10 @@ func (s *Server) listenTSNet() error {
return nil
}
tsLn, err := tsnetTransport{
hostname: hostname,
stateDir: s.tsnetStateDir(),
authKey: envTSNetAuthKey(),
hostname: hostname,
stateDir: s.tsnetStateDir(),
authKey: envTSNetAuthKey(),
upTimeout: s.set.Duration("daemon.tsnetUpTimeout"),
}.Listen()
if err != nil {
return err
Expand Down
43 changes: 28 additions & 15 deletions pkgs/mcpx/internal/daemon/transport_tsnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"sync"
"time"

"tailscale.com/ipn/store/mem"
"tailscale.com/tsnet"
Expand Down Expand Up @@ -42,16 +43,24 @@ type tsnetTransport struct {
hostname string
stateDir string
authKey string
// upTimeout is daemon.tsnetUpTimeout; zero means the built-in default.
upTimeout time.Duration
}

func (t tsnetTransport) Name() string { return "tsnet" }

// tsnetUpTimeout bounds how long a node may take to authenticate and
// reach Running before its listener is believed. tsnet's Start is
// asynchronous: without this wait a daemon with an expired or revoked
// auth key would report startup success while its identity route never
// joins the tailnet.
var tsnetUpTimeout = defaults.TSNetUpTimeout
// tsnetUpBudget is how long a node may take to authenticate and reach
// Running before its listener is believed: daemon.tsnetUpTimeout, or the
// built-in default when it is unset. tsnet's Start is asynchronous:
// without this wait a daemon with an expired or revoked auth key would
// report startup success while its identity route never joins the
// tailnet.
func tsnetUpBudget(configured time.Duration) time.Duration {
if configured > 0 {
return configured
}
return defaults.TSNetUpTimeout
}

func (t tsnetTransport) Listen() (net.Listener, error) {
node, err := newTSNetNode(t.hostname, t.stateDir, t.authKey, false)
Expand All @@ -62,12 +71,13 @@ func (t tsnetTransport) Listen() (net.Listener, error) {
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), tsnetUpTimeout)
budget := tsnetUpBudget(t.upTimeout)
ctx, cancel := context.WithTimeout(context.Background(), budget)
defer cancel()
if _, err := node.server.Up(ctx); err != nil {
_ = ln.Close()
_ = node.Close()
return nil, fmt.Errorf("tsnet node %s did not join the tailnet within %s (auth key expired or revoked?): %w", t.hostname, tsnetUpTimeout, err)
return nil, fmt.Errorf("tsnet node %s did not join the tailnet within %s (auth key expired or revoked?): %w", t.hostname, budget, err)
}
return ln, nil
}
Expand Down Expand Up @@ -218,8 +228,9 @@ func ParseTSNetEndpoint(endpoint string) (host string, port int, ok bool, err er
// process. Without an auth key the dial fails with the reason — a
// tsnet endpoint is never silently retried as a plain TCP address.
type TSNetDialer struct {
hostname string
authKey string
hostname string
authKey string
upTimeout time.Duration

once sync.Once
node *tsnetNode
Expand All @@ -240,19 +251,21 @@ type TSNetDialer struct {
// wait is a no-op; a failed join is reported once, with the reason.
func (d *TSNetDialer) ensureJoined() error {
d.joinOnce.Do(func() {
ctx, cancel := context.WithTimeout(context.Background(), tsnetUpTimeout)
budget := tsnetUpBudget(d.upTimeout)
ctx, cancel := context.WithTimeout(context.Background(), budget)
defer cancel()
if _, err := d.node.server.Up(ctx); err != nil {
d.joinErr = fmt.Errorf("tsnet client did not join the tailnet within %s: %w", tsnetUpTimeout, err)
d.joinErr = fmt.Errorf("tsnet client did not join the tailnet within %s: %w", budget, err)
}
})
return d.joinErr
}

// NewTSNetDialer builds a dialer for one client hostname (the name this
// client appears under on the tailnet).
func NewTSNetDialer(hostname string) *TSNetDialer {
return &TSNetDialer{hostname: hostname, authKey: envTSNetAuthKey()}
// client appears under on the tailnet). upTimeout is the join budget
// (daemon.tsnetUpTimeout); zero means the built-in default.
func NewTSNetDialer(hostname string, upTimeout time.Duration) *TSNetDialer {
return &TSNetDialer{hostname: hostname, authKey: envTSNetAuthKey(), upTimeout: upTimeout}
}

func envTSNetAuthKey() string {
Expand Down
25 changes: 25 additions & 0 deletions pkgs/mcpx/internal/daemon/tsnet_up_budget_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package daemon

import (
"testing"
"time"

"github.com/developing-today/code/pkgs/mcpx/internal/defaults"
)

// daemon.tsnetUpTimeout was declared and never read: both the daemon's
// listener and the client's dialer waited the built-in default whatever
// the setting said. The budget now comes from the setting, and only an
// unset one falls back.
func TestTSNetUpBudgetHonoursTheSetting(t *testing.T) {
if got := tsnetUpBudget(0); got != defaults.TSNetUpTimeout {
t.Fatalf("an unset budget should be the default %s, got %s", defaults.TSNetUpTimeout, got)
}
if got := tsnetUpBudget(90 * time.Second); got != 90*time.Second {
t.Fatalf("a configured budget should be used as is, got %s", got)
}
d := NewTSNetDialer("mcpx-client", 45*time.Second)
if d.upTimeout != 45*time.Second {
t.Fatalf("the dialer should carry the configured budget, got %s", d.upTimeout)
}
}
1 change: 0 additions & 1 deletion pkgs/mcpx/internal/settings/consumed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ var unreadAllowed = map[string]string{
// names nothing that exists, and config.ResolveChainFrom consumes
// the file-layer values to govern the chain itself in two passes
// (issue #1842) -- so it no longer belongs on this list.
"daemon.tsnetUpTimeout": "read by transport_tsnet.go without an accessor",
"paths.configFile": "a bootstrap setting: it names the file the settings are " +
"read from, so config.SearchPathFrom reads MCPX_CONFIG before any Set " +
"exists, and the global --config reaches App.ConfigPath the same way.",
Expand Down