mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-28 12:36:26 -08:00
ipn/ipnlocal: send the status Notify when any field is set
Replace the notifyPending flag in applyControlClientStatusLocked with a check on the Notify itself. ipn.Notify is incomparable, so a new Notify.IsZero method does the check through reflect, and a test sets each field in turn to prove IsZero sees it. RefreshExitNode drops its send flag the same way. A netmap whose self node is invalid and that changes nothing else no longer produces an empty Notify. Updates #22206 Signed-off-by: Brendan Creane <[email protected]>
This commit is contained in:
3 files changed
+57
-17
No files matched your search
@@ -6,6 +6,7 @@ package ipn
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -593,6 +594,11 @@ type PeerState struct {
|
||||
PeerWireGuardStateAt time.Time
|
||||
}
|
||||
|
||||
// IsZero reports whether no field of n is set.
|
||||
func (n Notify) IsZero() bool {
|
||||
return reflect.ValueOf(n).IsZero()
|
||||
}
|
||||
|
||||
func (n Notify) String() string {
|
||||
var sb strings.Builder
|
||||
sb.WriteString("Notify{")
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"go/types"
|
||||
"maps"
|
||||
"math/bits"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -15,11 +16,54 @@ import (
|
||||
|
||||
"golang.org/x/tools/go/packages"
|
||||
|
||||
"tailscale.com/drive"
|
||||
"tailscale.com/health"
|
||||
"tailscale.com/types/empty"
|
||||
"tailscale.com/types/views"
|
||||
"tailscale.com/util/mak"
|
||||
)
|
||||
|
||||
// TestNotifyIsZero checks that IsZero is true for an empty Notify and false
|
||||
// once any single field is set, so a new field cannot be missed.
|
||||
func TestNotifyIsZero(t *testing.T) {
|
||||
if !(Notify{}).IsZero() {
|
||||
t.Error("empty Notify is not zero")
|
||||
}
|
||||
rt := reflect.TypeFor[Notify]()
|
||||
for i := range rt.NumField() {
|
||||
f := rt.Field(i)
|
||||
if f.Name == "_" {
|
||||
continue
|
||||
}
|
||||
var n Notify
|
||||
fv := reflect.ValueOf(&n).Elem().Field(i)
|
||||
switch f.Type.Kind() {
|
||||
case reflect.Pointer:
|
||||
fv.Set(reflect.New(f.Type.Elem()))
|
||||
case reflect.Slice:
|
||||
fv.Set(reflect.MakeSlice(f.Type, 1, 1))
|
||||
case reflect.Map:
|
||||
fv.Set(reflect.MakeMap(f.Type))
|
||||
case reflect.String:
|
||||
fv.SetString("x")
|
||||
case reflect.Bool:
|
||||
fv.SetBool(true)
|
||||
case reflect.Struct:
|
||||
switch f.Name {
|
||||
case "DriveShares":
|
||||
n.DriveShares = views.SliceOfViews[*drive.Share, drive.ShareView]([]*drive.Share{{}})
|
||||
default:
|
||||
t.Fatalf("field %s: add a case for struct type %v", f.Name, f.Type)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("field %s: add a case for kind %v", f.Name, f.Type.Kind())
|
||||
}
|
||||
if n.IsZero() {
|
||||
t.Errorf("Notify with %s set reports zero", f.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotifyString(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
|
||||
+7
-17
@@ -2014,11 +2014,11 @@ func (b *LocalBackend) applyControlClientStatusLocked(c controlclient.Client, st
|
||||
}
|
||||
|
||||
// notify collects the login, prefs, netmap, and exit node changes from
|
||||
// this status. The deferred send runs on every return.
|
||||
// this status. The deferred call sends it on every return, if any
|
||||
// field was set.
|
||||
var notify ipn.Notify
|
||||
notifyPending := false // true once notify must be sent
|
||||
defer func() {
|
||||
if notifyPending {
|
||||
if !notify.IsZero() {
|
||||
b.sendLocked(notify)
|
||||
}
|
||||
}()
|
||||
@@ -2048,7 +2048,6 @@ func (b *LocalBackend) applyControlClientStatusLocked(c controlclient.Client, st
|
||||
}
|
||||
b.authReconfigLocked()
|
||||
notify.LoginFinished = &empty.Message{}
|
||||
notifyPending = true
|
||||
}
|
||||
|
||||
prefsChanged := false
|
||||
@@ -2145,7 +2144,7 @@ func (b *LocalBackend) applyControlClientStatusLocked(c controlclient.Client, st
|
||||
// The Notify built so far is dropped. The replacing path has
|
||||
// already sent its own prefs.
|
||||
b.logf("Ignoring SetControlClientStatus from old client")
|
||||
notifyPending = false
|
||||
notify = ipn.Notify{}
|
||||
return false
|
||||
}
|
||||
// b.cc is nil for a cached netmap applied by startLocked.
|
||||
@@ -2190,7 +2189,6 @@ func (b *LocalBackend) applyControlClientStatusLocked(c controlclient.Client, st
|
||||
notify.ErrMessage = &msg
|
||||
notify.Prefs = new(b.pm.CurrentPrefs())
|
||||
b.addSuggestedExitNodeLocked(¬ify)
|
||||
notifyPending = true
|
||||
return false
|
||||
}
|
||||
if oldNetMap != nil {
|
||||
@@ -2236,7 +2234,6 @@ func (b *LocalBackend) applyControlClientStatusLocked(c controlclient.Client, st
|
||||
if st.NetMap.SelfNode.Valid() {
|
||||
notify.SelfChange = st.NetMap.SelfNode.AsStruct()
|
||||
}
|
||||
notifyPending = true
|
||||
if b.hasPeerChangeWatcherLocked() {
|
||||
notify.UserProfiles = st.NetMap.UserProfiles
|
||||
notify.PeersChanged = make([]*tailcfg.Node, 0, len(st.NetMap.Peers))
|
||||
@@ -2270,11 +2267,8 @@ func (b *LocalBackend) applyControlClientStatusLocked(c controlclient.Client, st
|
||||
// by an edit made while b.mu was released for tkaSyncIfNeeded.
|
||||
if prefsChanged {
|
||||
notify.Prefs = new(b.pm.CurrentPrefs())
|
||||
notifyPending = true
|
||||
}
|
||||
if b.addSuggestedExitNodeLocked(¬ify) {
|
||||
notifyPending = true
|
||||
}
|
||||
b.addSuggestedExitNodeLocked(¬ify)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -7655,15 +7649,11 @@ func (b *LocalBackend) RefreshExitNode() {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
var notify ipn.Notify
|
||||
send := false
|
||||
if b.refreshExitNodeLocked() {
|
||||
notify.Prefs = new(b.pm.CurrentPrefs())
|
||||
send = true
|
||||
}
|
||||
if b.addSuggestedExitNodeLocked(¬ify) {
|
||||
send = true
|
||||
}
|
||||
if send {
|
||||
b.addSuggestedExitNodeLocked(¬ify)
|
||||
if !notify.IsZero() {
|
||||
b.sendLocked(notify)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user