feature/relayserver: init server at config time instead of request time (#17484)

The lazy init led to confusion and a belief that was something was
wrong. It's reasonable to expect the daemon to listen on the port at the
time it's configured.

Updates tailscale/corp#33094

Signed-off-by: Jordan Whited <jordan@tailscale.com>
This commit is contained in:
Jordan Whited 2025-10-09 11:45:03 -07:00 committed by GitHub
parent 0f4dec928e
commit e2233b7942
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 12 additions and 13 deletions

View File

@ -367,7 +367,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
💣 tailscale.com/net/tshttpproxy from tailscale.com/feature/useproxy
tailscale.com/net/tstun from tailscale.com/cmd/tailscaled+
tailscale.com/net/udprelay from tailscale.com/feature/relayserver
tailscale.com/net/udprelay/endpoint from tailscale.com/feature/relayserver+
tailscale.com/net/udprelay/endpoint from tailscale.com/net/udprelay+
tailscale.com/net/udprelay/status from tailscale.com/client/local+
tailscale.com/omit from tailscale.com/ipn/conffile
tailscale.com/paths from tailscale.com/client/local+

View File

@ -21,10 +21,8 @@ import (
"tailscale.com/ipn/ipnext"
"tailscale.com/ipn/localapi"
"tailscale.com/net/udprelay"
"tailscale.com/net/udprelay/endpoint"
"tailscale.com/net/udprelay/status"
"tailscale.com/tailcfg"
"tailscale.com/types/key"
"tailscale.com/types/logger"
"tailscale.com/types/ptr"
"tailscale.com/util/eventbus"
@ -91,13 +89,6 @@ type extension struct {
hasNodeAttrDisableRelayServer bool // tailcfg.NodeAttrDisableRelayServer
}
// relayServer is the interface of [udprelay.Server].
type relayServer interface {
AllocateEndpoint(discoA key.DiscoPublic, discoB key.DiscoPublic) (endpoint.ServerEndpoint, error)
Close() error
GetSessions() []status.ServerSession
}
// Name implements [ipnext.Extension].
func (e *extension) Name() string {
return featureName
@ -182,7 +173,11 @@ func (e *extension) consumeEventbusTopics(ec *eventbus.Client, port int) func(*e
debugSessionsCh := e.debugSessionsCh
return func(ec *eventbus.Client) {
var rs relayServer // lazily initialized
rs, err := udprelay.NewServer(e.logf, port, overrideAddrs())
if err != nil {
e.logf("error initializing server: %v", err)
}
defer func() {
if rs != nil {
rs.Close()
@ -194,7 +189,6 @@ func (e *extension) consumeEventbusTopics(ec *eventbus.Client, port int) func(*e
return
case respCh := <-debugSessionsCh:
if rs == nil {
// Don't initialize the server simply for a debug request.
respCh <- nil
continue
}
@ -202,7 +196,8 @@ func (e *extension) consumeEventbusTopics(ec *eventbus.Client, port int) func(*e
respCh <- sessions
case req := <-reqSub.Events():
if rs == nil {
var err error
// The server may have previously failed to initialize if
// the configured port was in use, try again.
rs, err = udprelay.NewServer(e.logf, port, overrideAddrs())
if err != nil {
e.logf("error initializing server: %v", err)

View File

@ -8,6 +8,7 @@ import (
"tailscale.com/ipn"
"tailscale.com/tsd"
"tailscale.com/types/logger"
"tailscale.com/types/ptr"
"tailscale.com/util/eventbus"
)
@ -96,6 +97,7 @@ func Test_extension_profileStateChanged(t *testing.T) {
sys := tsd.NewSystem()
bus := sys.Bus.Get()
e := &extension{
logf: logger.Discard,
port: tt.fields.port,
bus: bus,
}
@ -154,6 +156,7 @@ func Test_extension_handleBusLifetimeLocked(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &extension{
logf: logger.Discard,
bus: eventbus.New(),
shutdown: tt.shutdown,
port: tt.port,

View File

@ -536,6 +536,7 @@ func (s *Server) listenOn(port int) error {
s.uc6 = bc
s.uc6Port = uint16(portUint)
}
s.logf("listening on %s:%d", network, portUint)
}
return nil
}