cleanupStaleRoutes method

RoutingTable<T> cleanupStaleRoutes(
  1. RoutingTable<T> routingTable,
  2. DateTime currentTime
)

Removes stale routes and performs garbage collection

routingTable - Current routing table currentTime - Current time for comparison

Returns cleaned routing table with stale routes removed

Implementation

RoutingTable<T> cleanupStaleRoutes(
  RoutingTable<T> routingTable,
  DateTime currentTime,
) {
  final validRoutes = <T, RouteEntry<T>>{};
  final cutoff = currentTime.subtract(garbageCollectionTimeout);

  for (final entry in routingTable.routes.entries) {
    final dest = entry.key;
    final route = entry.value;

    // Keep directly connected routes and recent routes
    if (route.isDirectlyConnected || route.lastUpdate.isAfter(cutoff)) {
      validRoutes[dest] = route;
    }
  }

  return RoutingTable<T>(
    sourceNode: routingTable.sourceNode,
    routes: Map.unmodifiable(validRoutes),
    lastUpdate: currentTime,
  );
}