Skip to content

Commit

Permalink
clean up entries
Browse files Browse the repository at this point in the history
  • Loading branch information
lukedirtwalker committed Dec 24, 2024
1 parent 8bbf361 commit de53436
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
15 changes: 14 additions & 1 deletion pkg/daemon/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/lint:go.bzl", "go_library")
load("//tools/lint:go.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
Expand Down Expand Up @@ -33,3 +33,16 @@ go_library(
"@org_golang_google_protobuf//types/known/timestamppb:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["topology_test.go"],
deps = [
":go_default_library",
"//pkg/addr:go_default_library",
"//pkg/daemon/mock_daemon:go_default_library",
"//pkg/snet:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
],
)
19 changes: 8 additions & 11 deletions pkg/daemon/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package daemon
import (
"context"
"net/netip"
"sync"
"sync/atomic"
"time"

"github.com/scionproto/scion/pkg/log"
Expand Down Expand Up @@ -59,14 +59,12 @@ func LoadTopology(ctx context.Context, conn Connector) (snet.Topology, error) {
type ReloadingTopology struct {
conn Connector
baseTopology snet.Topology
interfaces sync.Map
interfaces atomic.Pointer[map[uint16]netip.AddrPort]
}

// NewReloadingTopology creates a new ReloadingTopology that reloads the
// interface information periodically. The Run method must be called for
// interface information to be populated. NOTE: The reloading topology does not
// clean up old interface information, so if you have a lot of interface churn,
// you may want to use a different implementation.
// interface information to be populated.
func NewReloadingTopology(ctx context.Context, conn Connector) (*ReloadingTopology, error) {
ia, err := conn.LocalIA(ctx)
if err != nil {
Expand Down Expand Up @@ -95,11 +93,12 @@ func (t *ReloadingTopology) Topology() snet.Topology {
LocalIA: base.LocalIA,
PortRange: base.PortRange,
Interface: func(ifID uint16) (netip.AddrPort, bool) {
a, ok := t.interfaces.Load(ifID)
if !ok {
m := t.interfaces.Load()
if m == nil {
return netip.AddrPort{}, false
}
return a.(netip.AddrPort), true
a, ok := (*m)[ifID]
return a, ok
},
}
}
Expand Down Expand Up @@ -131,8 +130,6 @@ func (t *ReloadingTopology) loadInterfaces(ctx context.Context) error {
if err != nil {
return err
}
for ifID, addr := range intfs {
t.interfaces.Store(ifID, addr)
}
t.interfaces.Store(&intfs)
return nil
}
2 changes: 2 additions & 0 deletions pkg/daemon/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func TestReloadingTopology(t *testing.T) {
<-done
wantTopo.interfaces = interfacesLater
wantTopo.checkTopology(t, loader.Topology())
_, ok := loader.Topology().Interface(1)
assert.False(t, ok)
}

type testTopology struct {
Expand Down

0 comments on commit de53436

Please sign in to comment.