Skip to content

Commit

Permalink
ipnlocal, magicsock: add more description to storing last suggested exit
Browse files Browse the repository at this point in the history
node related functions
Updates tailscale/corp#19681

Signed-off-by: Claire Wang <claire@tailscale.com>
  • Loading branch information
clairew committed May 3, 2024
1 parent 4062936 commit 6806e72
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
9 changes: 5 additions & 4 deletions ipn/ipnlocal/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -6361,7 +6361,7 @@ func (b *LocalBackend) SuggestExitNode() (response apitype.ExitNodeSuggestionRes
lastSuggestedExitNode := b.lastSuggestedExitNode
b.mu.Unlock()
if lastReport == nil || netMap == nil {
last, err := suggestLastExitNode(lastSuggestedExitNode)
last, err := formatLastSuggestedExitNode(lastSuggestedExitNode)
if err != nil {
return response, ErrCannotSuggestExitNode
}
Expand All @@ -6371,7 +6371,7 @@ func (b *LocalBackend) SuggestExitNode() (response apitype.ExitNodeSuggestionRes
r := rand.New(rand.NewSource(seed))
res, err := suggestExitNode(lastReport, netMap, r)
if err != nil {
last, err := suggestLastExitNode(lastSuggestedExitNode)
last, err := formatLastSuggestedExitNode(lastSuggestedExitNode)
if err != nil {
return response, ErrCannotSuggestExitNode
}
Expand All @@ -6384,9 +6384,10 @@ func (b *LocalBackend) SuggestExitNode() (response apitype.ExitNodeSuggestionRes
return res, err
}

// suggestLastExitNode formats a response with the last suggested exit node's ID and name.
// formatLastSuggestedExitNode 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.
func suggestLastExitNode(lastSuggestedExitNode lastSuggestedExitNode) (res apitype.ExitNodeSuggestionResponse, err error) {
func formatLastSuggestedExitNode(lastSuggestedExitNode lastSuggestedExitNode) (res apitype.ExitNodeSuggestionResponse, err error) {
if lastSuggestedExitNode.id != "" && lastSuggestedExitNode.name != "" {
res.ID = lastSuggestedExitNode.id
res.Name = lastSuggestedExitNode.name
Expand Down
6 changes: 3 additions & 3 deletions ipn/ipnlocal/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3438,7 +3438,7 @@ func TestMinLatencyDERPregion(t *testing.T) {
}
}

func TestSuggestLastExitNode(t *testing.T) {
func TestFormatLastSuggestedExitNode(t *testing.T) {
tests := []struct {
name string
lastSuggestedExitNode lastSuggestedExitNode
Expand All @@ -3460,7 +3460,7 @@ func TestSuggestLastExitNode(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := suggestLastExitNode(tt.lastSuggestedExitNode)
got, err := formatLastSuggestedExitNode(tt.lastSuggestedExitNode)
if got != tt.wantRes || err != tt.wantErr {
t.Errorf("got %v error %v, want %v error %v", got, err, tt.wantRes, tt.wantErr)
}
Expand Down Expand Up @@ -3772,7 +3772,7 @@ func TestLocalBackendSuggestExitNode(t *testing.T) {
lb := newTestLocalBackend(t)
lb.lastSuggestedExitNode = tt.lastSuggestedExitNode
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()
if got.ID != tt.wantID {
t.Errorf("ID=%v, want=%v", got.ID, tt.wantID)
Expand Down
6 changes: 3 additions & 3 deletions wgengine/magicsock/magicsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3089,8 +3089,8 @@ func (c *Conn) GetLastNetcheckReport(ctx context.Context) *netcheck.Report {
return lastReport
}

// SetLastNetcheckReport sets local backend's last netcheck report.
// SetLastNetcheckReportForTest sets local backend's last netcheck report.
// Used for testing purposes.
func (c *Conn) SetLastNetcheckReport(ctx context.Context, report netcheck.Report) {
c.lastNetCheckReport.Store(&report)
func (c *Conn) SetLastNetcheckReportForTest(ctx context.Context, report *netcheck.Report) {
c.lastNetCheckReport.Store(report)
}

0 comments on commit 6806e72

Please sign in to comment.