ipnlocal, magicsock: add more description to storing last suggested exit

node related functions
Updates tailscale/corp#19681

Signed-off-by: Claire Wang <claire@tailscale.com>
pull/11998/head
Claire Wang 6 months ago
parent 5708fc0639
commit 7a467a37e1

@ -6424,7 +6424,7 @@ func (b *LocalBackend) SuggestExitNode() (response apitype.ExitNodeSuggestionRes
lastSuggestedExitNode := b.lastSuggestedExitNode lastSuggestedExitNode := b.lastSuggestedExitNode
b.mu.Unlock() b.mu.Unlock()
if lastReport == nil || netMap == nil { if lastReport == nil || netMap == nil {
last, err := suggestLastExitNode(lastSuggestedExitNode) last, err := lastSuggestedExitNode.asAPIType()
if err != nil { if err != nil {
return response, ErrCannotSuggestExitNode return response, ErrCannotSuggestExitNode
} }
@ -6434,7 +6434,7 @@ func (b *LocalBackend) SuggestExitNode() (response apitype.ExitNodeSuggestionRes
r := rand.New(rand.NewSource(seed)) r := rand.New(rand.NewSource(seed))
res, err := suggestExitNode(lastReport, netMap, r) res, err := suggestExitNode(lastReport, netMap, r)
if err != nil { if err != nil {
last, err := suggestLastExitNode(lastSuggestedExitNode) last, err := lastSuggestedExitNode.asAPIType()
if err != nil { if err != nil {
return response, ErrCannotSuggestExitNode return response, ErrCannotSuggestExitNode
} }
@ -6447,12 +6447,13 @@ func (b *LocalBackend) SuggestExitNode() (response apitype.ExitNodeSuggestionRes
return res, err return res, err
} }
// suggestLastExitNode formats a response with the last suggested exit node's ID and name. // asAPIType formats a response with the last suggested exit node's ID and name.
// Returns error if there is no id or name.
// Used as a fallback before returning a nil response and error. // Used as a fallback before returning a nil response and error.
func suggestLastExitNode(lastSuggestedExitNode lastSuggestedExitNode) (res apitype.ExitNodeSuggestionResponse, err error) { func (n lastSuggestedExitNode) asAPIType() (res apitype.ExitNodeSuggestionResponse, _ error) {
if lastSuggestedExitNode.id != "" && lastSuggestedExitNode.name != "" { if n.id != "" && n.name != "" {
res.ID = lastSuggestedExitNode.id res.ID = n.id
res.Name = lastSuggestedExitNode.name res.Name = n.name
return res, nil return res, nil
} }
return res, ErrUnableToSuggestLastExitNode return res, ErrUnableToSuggestLastExitNode

@ -3438,7 +3438,7 @@ func TestMinLatencyDERPregion(t *testing.T) {
} }
} }
func TestSuggestLastExitNode(t *testing.T) { func TestLastSuggestedExitNodeAsAPIType(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
lastSuggestedExitNode lastSuggestedExitNode lastSuggestedExitNode lastSuggestedExitNode
@ -3460,7 +3460,7 @@ func TestSuggestLastExitNode(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := suggestLastExitNode(tt.lastSuggestedExitNode) got, err := tt.lastSuggestedExitNode.asAPIType()
if got != tt.wantRes || err != tt.wantErr { if got != tt.wantRes || err != tt.wantErr {
t.Errorf("got %v error %v, want %v error %v", got, err, tt.wantRes, tt.wantErr) t.Errorf("got %v error %v, want %v error %v", got, err, tt.wantRes, tt.wantErr)
} }
@ -3472,7 +3472,7 @@ func TestLocalBackendSuggestExitNode(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
lastSuggestedExitNode lastSuggestedExitNode lastSuggestedExitNode lastSuggestedExitNode
report netcheck.Report report *netcheck.Report
netMap netmap.NetworkMap netMap netmap.NetworkMap
wantID tailcfg.StableNodeID wantID tailcfg.StableNodeID
wantName string wantName string
@ -3482,7 +3482,7 @@ func TestLocalBackendSuggestExitNode(t *testing.T) {
{ {
name: "nil netmap, returns last suggested exit node", name: "nil netmap, returns last suggested exit node",
lastSuggestedExitNode: lastSuggestedExitNode{name: "test", id: "test"}, lastSuggestedExitNode: lastSuggestedExitNode{name: "test", id: "test"},
report: netcheck.Report{ report: &netcheck.Report{
RegionLatency: map[int]time.Duration{ RegionLatency: map[int]time.Duration{
1: 0, 1: 0,
2: -1, 2: -1,
@ -3518,7 +3518,7 @@ func TestLocalBackendSuggestExitNode(t *testing.T) {
{ {
name: "found better derp node, last suggested exit node updates", name: "found better derp node, last suggested exit node updates",
lastSuggestedExitNode: lastSuggestedExitNode{name: "test", id: "test"}, lastSuggestedExitNode: lastSuggestedExitNode{name: "test", id: "test"},
report: netcheck.Report{ report: &netcheck.Report{
RegionLatency: map[int]time.Duration{ RegionLatency: map[int]time.Duration{
1: 10, 1: 10,
2: 10, 2: 10,
@ -3574,7 +3574,7 @@ func TestLocalBackendSuggestExitNode(t *testing.T) {
{ {
name: "found better mullvad node, last suggested exit node updates", name: "found better mullvad node, last suggested exit node updates",
lastSuggestedExitNode: lastSuggestedExitNode{name: "San Jose", id: "3"}, lastSuggestedExitNode: lastSuggestedExitNode{name: "San Jose", id: "3"},
report: netcheck.Report{ report: &netcheck.Report{
RegionLatency: map[int]time.Duration{ RegionLatency: map[int]time.Duration{
1: 0, 1: 0,
2: 0, 2: 0,
@ -3645,7 +3645,7 @@ func TestLocalBackendSuggestExitNode(t *testing.T) {
{ {
name: "ErrNoPreferredDERP, use last suggested exit node", name: "ErrNoPreferredDERP, use last suggested exit node",
lastSuggestedExitNode: lastSuggestedExitNode{name: "test", id: "test"}, lastSuggestedExitNode: lastSuggestedExitNode{name: "test", id: "test"},
report: netcheck.Report{ report: &netcheck.Report{
RegionLatency: map[int]time.Duration{ RegionLatency: map[int]time.Duration{
1: 10, 1: 10,
2: 10, 2: 10,
@ -3701,7 +3701,7 @@ func TestLocalBackendSuggestExitNode(t *testing.T) {
{ {
name: "ErrNoPreferredDERP, use last suggested exit node", name: "ErrNoPreferredDERP, use last suggested exit node",
lastSuggestedExitNode: lastSuggestedExitNode{name: "test", id: "test"}, lastSuggestedExitNode: lastSuggestedExitNode{name: "test", id: "test"},
report: netcheck.Report{ report: &netcheck.Report{
RegionLatency: map[int]time.Duration{ RegionLatency: map[int]time.Duration{
1: 10, 1: 10,
2: 10, 2: 10,
@ -3756,7 +3756,7 @@ func TestLocalBackendSuggestExitNode(t *testing.T) {
}, },
{ {
name: "unable to use last suggested exit node", name: "unable to use last suggested exit node",
report: netcheck.Report{ report: &netcheck.Report{
RegionLatency: map[int]time.Duration{ RegionLatency: map[int]time.Duration{
1: 10, 1: 10,
2: 10, 2: 10,
@ -3772,7 +3772,7 @@ func TestLocalBackendSuggestExitNode(t *testing.T) {
lb := newTestLocalBackend(t) lb := newTestLocalBackend(t)
lb.lastSuggestedExitNode = tt.lastSuggestedExitNode lb.lastSuggestedExitNode = tt.lastSuggestedExitNode
lb.netMap = &tt.netMap lb.netMap = &tt.netMap
lb.sys.MagicSock.Get().SetLastNetcheckReport(context.Background(), tt.report) lb.sys.MagicSock.Get().SetLastNetcheckReportForTest(context.Background(), tt.report)
got, err := lb.SuggestExitNode() got, err := lb.SuggestExitNode()
if got.ID != tt.wantID { if got.ID != tt.wantID {
t.Errorf("ID=%v, want=%v", got.ID, tt.wantID) t.Errorf("ID=%v, want=%v", got.ID, tt.wantID)

@ -3089,8 +3089,8 @@ func (c *Conn) GetLastNetcheckReport(ctx context.Context) *netcheck.Report {
return lastReport return lastReport
} }
// SetLastNetcheckReport sets local backend's last netcheck report. // SetLastNetcheckReportForTest sets the magicsock conn's last netcheck report.
// Used for testing purposes. // Used for testing purposes.
func (c *Conn) SetLastNetcheckReport(ctx context.Context, report netcheck.Report) { func (c *Conn) SetLastNetcheckReportForTest(ctx context.Context, report *netcheck.Report) {
c.lastNetCheckReport.Store(&report) c.lastNetCheckReport.Store(report)
} }

Loading…
Cancel
Save