Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
bassosimone committed Feb 8, 2024
1 parent 7b846c4 commit 8bee4dd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 88 deletions.
84 changes: 40 additions & 44 deletions internal/webconnectivityalgo/dnswhoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ type DNSWhoamiInfoEntry struct {
Address string `json:"address"`
}

// dnsWhoamiInfoEntryWrapper wraps a [DNSWhoamiInfoEntry].
type dnsWhoamiInfoEntryWrapper struct {
T time.Time
V *DNSWhoamiInfoEntry
// dnsWhoamiInfoTimedEntry wraps a [DNSWhoamiInfoEntry].
type dnsWhoamiInfoTimedEntry struct {
Addr string
T time.Time
}

// TODO(bassosimone): this code needs refining before we can merge it inside
Expand All @@ -43,7 +43,7 @@ type dnsWhoamiInfoEntryWrapper struct {
// the [NewDNSWhoamiService] factory function.
type DNSWhoamiService struct {
// entries contains the entries.
entries map[string]*dnsWhoamiInfoEntryWrapper
entries map[string]*dnsWhoamiInfoTimedEntry

// logger is the logger.
logger model.Logger
Expand All @@ -64,7 +64,7 @@ type DNSWhoamiService struct {
// NewDNSWhoamiService constructs a new [*DNSWhoamiService].
func NewDNSWhoamiService(logger model.Logger) *DNSWhoamiService {
return &DNSWhoamiService{
entries: map[string]*dnsWhoamiInfoEntryWrapper{},
entries: map[string]*dnsWhoamiInfoTimedEntry{},
logger: logger,
mu: &sync.Mutex{},
netx: &netxlite.Netx{Underlying: nil},
Expand All @@ -73,6 +73,31 @@ func NewDNSWhoamiService(logger model.Logger) *DNSWhoamiService {
}
}

// SystemV4 returns the results of querying using the system resolver and IPv4.
func (svc *DNSWhoamiService) SystemV4(ctx context.Context) ([]DNSWhoamiInfoEntry, bool) {
spec := &dnsWhoamiResolverSpec{
name: "system:///",
factory: func(logger model.Logger, netx *netxlite.Netx) model.Resolver {
return svc.netx.NewStdlibResolver(svc.logger)
},
}
v := svc.lookup(ctx, spec)
return v, len(v) > 0
}

// UDPv4 returns the results of querying a given UDP resolver and IPv4.
func (svc *DNSWhoamiService) UDPv4(ctx context.Context, address string) ([]DNSWhoamiInfoEntry, bool) {
spec := &dnsWhoamiResolverSpec{
name: address,
factory: func(logger model.Logger, netx *netxlite.Netx) model.Resolver {
dialer := svc.netx.NewDialerWithResolver(svc.logger, svc.netx.NewStdlibResolver(svc.logger))
return svc.netx.NewParallelUDPResolver(svc.logger, dialer, address)
},
}
v := svc.lookup(ctx, spec)
return v, len(v) > 0
}

type dnsWhoamiResolverSpec struct {
name string
factory func(logger model.Logger, netx *netxlite.Netx) model.Resolver
Expand Down Expand Up @@ -104,31 +129,6 @@ func (svc *DNSWhoamiService) lookup(ctx context.Context, spec *dnsWhoamiResolver
return []DNSWhoamiInfoEntry{{Address: addrs[0]}}
}

// SystemV4 returns the results of querying using the system resolver and IPv4.
func (svc *DNSWhoamiService) SystemV4(ctx context.Context) ([]DNSWhoamiInfoEntry, bool) {
spec := &dnsWhoamiResolverSpec{
name: "system:///",
factory: func(logger model.Logger, netx *netxlite.Netx) model.Resolver {
return svc.netx.NewStdlibResolver(svc.logger)
},
}
v := svc.lookup(ctx, spec)
return v, len(v) > 0
}

// UDPv4 returns the results of querying a given UDP resolver and IPv4.
func (svc *DNSWhoamiService) UDPv4(ctx context.Context, address string) ([]DNSWhoamiInfoEntry, bool) {
spec := &dnsWhoamiResolverSpec{
name: address,
factory: func(logger model.Logger, netx *netxlite.Netx) model.Resolver {
dialer := svc.netx.NewDialerWithResolver(svc.logger, svc.netx.NewStdlibResolver(svc.logger))
return svc.netx.NewParallelUDPResolver(svc.logger, dialer, address)
},
}
v := svc.lookup(ctx, spec)
return v, len(v) > 0
}

func (svc *DNSWhoamiService) lockAndGet(now time.Time, serverAddr string) optional.Value[DNSWhoamiInfoEntry] {
// ensure there's mutual exclusion
defer svc.mu.Unlock()
Expand All @@ -148,7 +148,7 @@ func (svc *DNSWhoamiService) lockAndGet(now time.Time, serverAddr string) option

// return a copy of the value
return optional.Some(DNSWhoamiInfoEntry{
Address: entry.V.Address,
Address: entry.Addr,
})
}

Expand All @@ -158,24 +158,20 @@ func (svc *DNSWhoamiService) lockAndUpdate(now time.Time, serverAddr, whoamiAddr
svc.mu.Lock()

// insert into the table
svc.entries[serverAddr] = &dnsWhoamiInfoEntryWrapper{
T: now,
V: &DNSWhoamiInfoEntry{
Address: whoamiAddr,
},
svc.entries[serverAddr] = &dnsWhoamiInfoTimedEntry{
Addr: whoamiAddr,
T: now,
}
}

func (svc *DNSWhoamiService) cloneEntries() map[string]*dnsWhoamiInfoEntryWrapper {
func (svc *DNSWhoamiService) cloneEntries() map[string]*dnsWhoamiInfoTimedEntry {
defer svc.mu.Unlock()
svc.mu.Lock()
output := make(map[string]*dnsWhoamiInfoEntryWrapper)
output := make(map[string]*dnsWhoamiInfoTimedEntry)
for key, value := range svc.entries {
output[key] = &dnsWhoamiInfoEntryWrapper{
T: value.T,
V: &DNSWhoamiInfoEntry{
Address: value.V.Address,
},
output[key] = &dnsWhoamiInfoTimedEntry{
Addr: value.Addr,
T: value.T,
}
}
return output
Expand Down
66 changes: 22 additions & 44 deletions internal/webconnectivityalgo/dnswhoami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestDNSWhoamiService(t *testing.T) {
domain string

// internals contains the expected internals cache
internals map[string]*dnsWhoamiInfoEntryWrapper
internals map[string]*dnsWhoamiInfoTimedEntry

// callResults contains the expectations
callResults []callResults
Expand All @@ -47,18 +47,14 @@ func TestDNSWhoamiService(t *testing.T) {
}},
Good: true,
}},
internals: map[string]*dnsWhoamiInfoEntryWrapper{
internals: map[string]*dnsWhoamiInfoTimedEntry{
"system:///": {
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(time.Second),
V: &DNSWhoamiInfoEntry{
Address: netemx.DefaultClientAddress,
},
Addr: netemx.DefaultClientAddress,
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(time.Second),
},
"8.8.8.8:53": {
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(2 * time.Second),
V: &DNSWhoamiInfoEntry{
Address: netemx.DefaultClientAddress,
},
Addr: netemx.DefaultClientAddress,
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(2 * time.Second),
},
},
}, {
Expand All @@ -71,7 +67,7 @@ func TestDNSWhoamiService(t *testing.T) {
Entries: nil,
Good: false,
}},
internals: map[string]*dnsWhoamiInfoEntryWrapper{},
internals: map[string]*dnsWhoamiInfoTimedEntry{},
}}

for _, tc := range cases {
Expand Down Expand Up @@ -155,27 +151,21 @@ func TestDNSWhoamiService(t *testing.T) {
svc.netx = &netxlite.Netx{Underlying: &netxlite.NetemUnderlyingNetworkAdapter{UNet: env.ClientStack}}
svc.timeNow = ttp.timeNow

t.Log("~~~ first run ~~~~", ttp)

// run for the first time
_, _ = svc.SystemV4(context.Background())
_, _ = svc.UDPv4(context.Background(), "8.8.8.8:53")

// establish expectations for first run
//
// we expect the cache to be related to the first run
expectFirstInternals := map[string]*dnsWhoamiInfoEntryWrapper{
expectFirstInternals := map[string]*dnsWhoamiInfoTimedEntry{
"system:///": {
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(time.Second),
V: &DNSWhoamiInfoEntry{
Address: netemx.DefaultClientAddress,
},
Addr: netemx.DefaultClientAddress,
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(time.Second),
},
"8.8.8.8:53": {
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(2 * time.Second),
V: &DNSWhoamiInfoEntry{
Address: netemx.DefaultClientAddress,
},
Addr: netemx.DefaultClientAddress,
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(2 * time.Second),
},
}

Expand All @@ -184,27 +174,21 @@ func TestDNSWhoamiService(t *testing.T) {
t.Fatal(diff)
}

t.Log("~~~ second run ~~~~", ttp)

// run for the second time
_, _ = svc.SystemV4(context.Background())
_, _ = svc.UDPv4(context.Background(), "8.8.8.8:53")

// establish expectations for second run
//
// we expect the cache to be related to the first run
expectSecondInternals := map[string]*dnsWhoamiInfoEntryWrapper{
expectSecondInternals := map[string]*dnsWhoamiInfoTimedEntry{
"system:///": {
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(time.Second),
V: &DNSWhoamiInfoEntry{
Address: netemx.DefaultClientAddress,
},
Addr: netemx.DefaultClientAddress,
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(time.Second),
},
"8.8.8.8:53": {
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(2 * time.Second),
V: &DNSWhoamiInfoEntry{
Address: netemx.DefaultClientAddress,
},
Addr: netemx.DefaultClientAddress,
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(2 * time.Second),
},
}

Expand All @@ -213,27 +197,21 @@ func TestDNSWhoamiService(t *testing.T) {
t.Fatal(diff)
}

t.Log("~~~ third run ~~~~", ttp)

// run for the third time
_, _ = svc.SystemV4(context.Background())
_, _ = svc.UDPv4(context.Background(), "8.8.8.8:53")

// establish expectations for third run
//
// we expect the cache to be related to the third run
expectThirdInternals := map[string]*dnsWhoamiInfoEntryWrapper{
expectThirdInternals := map[string]*dnsWhoamiInfoTimedEntry{
"system:///": {
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(60 * time.Second),
V: &DNSWhoamiInfoEntry{
Address: netemx.DefaultClientAddress,
},
Addr: netemx.DefaultClientAddress,
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(60 * time.Second),
},
"8.8.8.8:53": {
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(62 * time.Second),
V: &DNSWhoamiInfoEntry{
Address: netemx.DefaultClientAddress,
},
Addr: netemx.DefaultClientAddress,
T: time.Date(2024, 2, 8, 9, 8, 7, 6, time.UTC).Add(62 * time.Second),
},
}

Expand Down

0 comments on commit 8bee4dd

Please sign in to comment.