summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorKevin Liang <kevinliang@tailscale.com>2024-03-22 19:13:14 +0000
committerKevin Liang <kevinliang@tailscale.com>2024-03-22 19:13:14 +0000
commit735838a0f10204f9ae60884836c0f088ea980392 (patch)
tree5bf88190a9858f5253ac2db36aeaddbbc8467694
parent2dfe7593ede77ab59a3e24d7c263b8350c70cc98 (diff)
downloadtailscale-fran/appc-domain-delte-prototype.tar.xz
tailscale-fran/appc-domain-delte-prototype.zip
Rate limiting the update of lastseen date for route.fran/appc-domain-delte-prototype
-rw-r--r--appc/appconnector.go44
-rw-r--r--ipn/ipnlocal/profiles.go5
-rw-r--r--ipn/prefs.go19
3 files changed, 53 insertions, 15 deletions
diff --git a/appc/appconnector.go b/appc/appconnector.go
index 6e37f0501..72bdf3a58 100644
--- a/appc/appconnector.go
+++ b/appc/appconnector.go
@@ -15,6 +15,7 @@ import (
"slices"
"strings"
"sync"
+ "time"
xmaps "golang.org/x/exp/maps"
"golang.org/x/net/dns/dnsmessage"
@@ -69,6 +70,9 @@ type AppConnector struct {
// queue provides ordering for update operations
queue execqueue.ExecQueue
+
+ discoveredToUpdateDate map[string][]netip.Prefix
+ discoveredLastUpdate time.Time
}
// NewAppConnector creates a new AppConnector.
@@ -335,12 +339,17 @@ func (e *AppConnector) ObserveDNSResponse(res []byte) {
// advertise each address we have learned for the routed domain, that
// was not already known.
var toAdvertise []netip.Prefix
- var toUpdateDate []netip.Prefix
+ // var toUpdateDate []netip.Prefix
for _, addr := range addrs {
if !e.isAddrKnownLocked(domain, addr) {
toAdvertise = append(toAdvertise, netip.PrefixFrom(addr, addr.BitLen()))
} else {
- toUpdateDate = append(toUpdateDate, netip.PrefixFrom(addr, addr.BitLen()))
+ if e.discoveredToUpdateDate == nil {
+ e.discoveredToUpdateDate = make(map[string][]netip.Prefix)
+ }
+ e.discoveredToUpdateDate[domain] = append(e.discoveredToUpdateDate[domain], netip.PrefixFrom(addr, addr.BitLen()))
+ slices.SortFunc(e.discoveredToUpdateDate[domain], func(i, j netip.Prefix) int { return i.Addr().Compare(j.Addr()) })
+ slices.Compact(e.discoveredToUpdateDate[domain])
}
}
@@ -348,15 +357,34 @@ func (e *AppConnector) ObserveDNSResponse(res []byte) {
// update the pm.currentroutes, then advertise those routes.
// we might also able to do this in local backend's advertiseRoute method, doing the update one domain by one domain.
// currentRouteInfo := nil
- routesToUpdate := append(toAdvertise, toUpdateDate...)
+
+ updateStore := false
routeInfo := e.routeAdvertiser.ReadRouteInfoFromStore()
- if routeInfo.Discovered == nil {
- routeInfo.Discovered = make(map[string]*ipn.DatedRoute)
+ if len(toAdvertise) != 0 {
+ routeInfo.AddRoutesInDiscoveredForDomain(domain, toAdvertise)
+ updateStore = true
}
- routeInfo.UpdateRoutesInDiscoveredForDomain(domain, routesToUpdate)
+ now := time.Now()
+ if len(e.discoveredToUpdateDate) > 0 && now.Sub(e.discoveredLastUpdate) > 5*time.Minute {
+ // fmt.Println("We did a date update to the existing routes") // Kevin debug
+ // fmt.Println("The time now is: ", now, " The last update was; ", e.discoveredLastUpdate) // Kevin debug
+ routeInfo.UpdateDatesForRoutesInDiscovered(e.discoveredToUpdateDate)
+ e.discoveredToUpdateDate = make(map[string][]netip.Prefix)
+ e.discoveredLastUpdate = now
+ updateStore = true
+ }
+ // fmt.Println("The time I updated the date was: ", now) // Kevin debug
+ // fmt.Println("Next time we update the date will be: ", now.Add(1*time.Minute)) // Kevin debug
+ // fmt.Println("The last update was; ", e.discoveredLastUpdate)// Kevin debug
+ // fmt.Println("discoveredToUpdate: ", e.discoveredToUpdateDate)// Kevin debug
outDated := routeInfo.OutDatedRoutesInDiscoveredForDomain(domain)
- e.routeAdvertiser.UpdateRoutesInfoToStore(routeInfo)
- // fmt.Println("Route infos after dns update: ", e.routeAdvertiser.ReadRouteInfoFromStore())
+ if len(outDated) != 0 {
+ updateStore = true
+ }
+ if updateStore {
+ e.routeAdvertiser.UpdateRoutesInfoToStore(routeInfo)
+ }
+ // fmt.Println("Route infos after dns update: ", e.routeAdvertiser.ReadRouteInfoFromStore()) // Kevin debug
e.scheduleAdvertisement(domain, toAdvertise...)
e.scheduleUndvertisement(domain, outDated...)
}
diff --git a/ipn/ipnlocal/profiles.go b/ipn/ipnlocal/profiles.go
index 1ba48cdce..be38909a1 100644
--- a/ipn/ipnlocal/profiles.go
+++ b/ipn/ipnlocal/profiles.go
@@ -53,7 +53,10 @@ func (pm *profileManager) CurrentRoutes() *ipn.RouteInfo {
func (pm *profileManager) SetCurrentRoutes(in *ipn.RouteInfo) error {
pm.currentRoutes = in
- return pm.WriteRoutesForCurrentProfile()
+ if err := pm.WriteRoutesForCurrentProfile(); err != nil {
+ return err
+ }
+ return nil
}
func (pm *profileManager) WriteState(id ipn.StateKey, val []byte) error {
diff --git a/ipn/prefs.go b/ipn/prefs.go
index 056cf2afa..8c67841d2 100644
--- a/ipn/prefs.go
+++ b/ipn/prefs.go
@@ -960,11 +960,10 @@ type RouteInfo struct {
Discovered map[string]*DatedRoute
}
-func (r RouteInfo) UpdateRoutesInDiscoveredForDomain(domain string, addrs []netip.Prefix) {
+func (r RouteInfo) AddRoutesInDiscoveredForDomain(domain string, addrs []netip.Prefix) {
dr, hasKey := r.Discovered[domain]
- fmt.Println("FRAN URIDFD", hasKey, dr, dr.Routes)
if !hasKey || dr == nil || dr.Routes == nil {
- newDatedRoutes := &DatedRoute{make(map[netip.Prefix]time.Time), time.Now().Truncate(1 * time.Hour)}
+ newDatedRoutes := &DatedRoute{make(map[netip.Prefix]time.Time), time.Now()}
newDatedRoutes.addAddrsToDatedRoute(addrs)
r.Discovered[domain] = newDatedRoutes
return
@@ -977,11 +976,19 @@ func (r RouteInfo) UpdateRoutesInDiscoveredForDomain(domain string, addrs []neti
return
}
+func (r RouteInfo) UpdateDatesForRoutesInDiscovered(toUpdate map[string][]netip.Prefix) {
+ for domain, addrs := range toUpdate {
+ for _, addr := range addrs {
+ r.Discovered[domain].Routes[addr] = time.Now()
+ }
+ }
+}
+
func (r RouteInfo) OutDatedRoutesInDiscoveredForDomain(domain string) []netip.Prefix {
dr, hasKey := r.Discovered[domain]
var outdate []netip.Prefix
now := time.Now()
- if !hasKey || dr == nil || dr.Routes == nil || now.Sub(dr.LastCleanUp) < 360 {
+ if !hasKey || dr == nil || dr.Routes == nil || now.Sub(dr.LastCleanUp) < 360*time.Hour {
return nil
}
for addr, date := range dr.Routes {
@@ -992,7 +999,7 @@ func (r RouteInfo) OutDatedRoutesInDiscoveredForDomain(domain string) []netip.Pr
}
}
r.Discovered[domain] = dr
- dr.LastCleanUp = time.Now().Truncate(1 * time.Hour)
+ dr.LastCleanUp = time.Now()
return outdate
}
@@ -1018,7 +1025,7 @@ func (d *DatedRoute) String() string {
}
func (d *DatedRoute) addAddrsToDatedRoute(addrs []netip.Prefix) {
- time := time.Now().Truncate(1 * time.Hour)
+ time := time.Now()
for _, addr := range addrs {
d.Routes[addr] = time
}