Advertise routes with configured strategy. Simplify Sync apply logic

This commit is contained in:
Milos Gajdos 2020-01-16 16:42:23 +00:00
parent eda8b00f84
commit 793e6013e5
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F

View File

@ -851,7 +851,9 @@ func (n *network) processNetChan(listener tunnel.Listener) {
} }
// get a list of the best routes for each service in our routing table // get a list of the best routes for each service in our routing table
q := []router.QueryOption{router.QueryStrategy(router.AdvertiseBest)} q := []router.QueryOption{
router.QueryStrategy(n.router.Options().Advertise),
}
routes, err := n.options.Router.Table().Query(q...) routes, err := n.options.Router.Table().Query(q...)
switch err { switch err {
case nil: case nil:
@ -1048,19 +1050,24 @@ func (n *network) processNetChan(listener tunnel.Listener) {
for _, pbRoute := range pbNetSync.Routes { for _, pbRoute := range pbNetSync.Routes {
// unmarshal the routes received from remote peer // unmarshal the routes received from remote peer
route := pbUtil.ProtoToRoute(pbRoute) route := pbUtil.ProtoToRoute(pbRoute)
// lookup the best route for the given service in our routing table // continue if we are the originator of the route
if route.Router == n.options.Router.Options().Id {
log.Debugf("Network node %s skipping route addition: route already present", n.id)
continue
}
// lookup best routes for the services in the just received route
q := []router.QueryOption{ q := []router.QueryOption{
router.QueryService(route.Service), router.QueryService(route.Service),
router.QueryStrategy(router.AdvertiseBest), router.QueryStrategy(router.AdvertiseBest),
} }
// NOTE: bestRoutes is either an empty slice or one element slice
bestRoutes, err := n.options.Router.Table().Query(q...) bestRoutes, err := n.options.Router.Table().Query(q...)
if err != nil { if err != nil {
log.Debugf("Network node %s failed listing best routes for %s: %v", n.id, route.Service, err) log.Debugf("Network node %s failed listing best routes for %s: %v", n.id, route.Service, err)
continue continue
} }
// we found no route for given service
// create the route we have just received // we found no route for the given service
// create new route we have just received
if len(bestRoutes) == 0 { if len(bestRoutes) == 0 {
// add routes to the routing table // add routes to the routing table
if err := n.router.Table().Create(route); err != nil && err != router.ErrDuplicateRoute { if err := n.router.Table().Create(route); err != nil && err != router.ErrDuplicateRoute {
@ -1073,26 +1080,16 @@ func (n *network) processNetChan(listener tunnel.Listener) {
// * prefer our own routes if metric is the same // * prefer our own routes if metric is the same
// * only add new routes if the metric is better than the metric of our best route // * only add new routes if the metric is better than the metric of our best route
bestRoute := bestRoutes[0] bestRoute := bestRoutes[0]
if bestRoute.Metric == route.Metric {
if route.Router == n.options.Router.Options().Id { if bestRoute.Metric <= route.Metric {
log.Debugf("Network node %s skipping route addition: already has local route", n.id)
continue continue
} }
// NOTE: we might want to skip here, too as we already have equally good route
// add routes to the routing table
if err := n.router.Table().Create(route); err != nil && err != router.ErrDuplicateRoute {
log.Debugf("Network node %s failed to add route: %v", n.id, err)
continue
}
}
if bestRoute.Metric > route.Metric {
// TODO: should we delete our best route here?
// add route to the routing table // add route to the routing table
if err := n.router.Table().Create(route); err != nil && err != router.ErrDuplicateRoute { if err := n.router.Table().Create(route); err != nil && err != router.ErrDuplicateRoute {
log.Debugf("Network node %s failed to add route: %v", n.id, err) log.Debugf("Network node %s failed to add route: %v", n.id, err)
} }
} }
}
// update your sync timestamp // update your sync timestamp
// NOTE: this might go away as we will be doing full table advert to random peer // NOTE: this might go away as we will be doing full table advert to random peer
@ -1372,7 +1369,9 @@ func (n *network) manage() {
} }
// get a list of the best routes for each service in our routing table // get a list of the best routes for each service in our routing table
q := []router.QueryOption{router.QueryStrategy(router.AdvertiseBest)} q := []router.QueryOption{
router.QueryStrategy(n.router.Options().Advertise),
}
routes, err := n.options.Router.Table().Query(q...) routes, err := n.options.Router.Table().Query(q...)
switch err { switch err {
case nil: case nil: