Make Optimal strategy default. Collapse routing tables based on strategy

This commit is contained in:
Milos Gajdos 2019-10-09 17:23:02 +01:00
parent 96e564e402
commit 837597fe6f
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
3 changed files with 62 additions and 8 deletions

View File

@ -682,18 +682,59 @@ func (r *router) flushRouteEvents(evType EventType) ([]*Event, error) {
return nil, fmt.Errorf("failed listing routes: %s", err) return nil, fmt.Errorf("failed listing routes: %s", err)
} }
// TODO: flush the routes based on strategy here r.RLock()
// - collapse the routes per service-metric-locality (prefer your routes) advertStrategy := r.options.Advertise
r.RUnlock()
if advertStrategy == All {
// build a list of events to advertise
events := make([]*Event, len(routes))
for i, route := range routes {
event := &Event{
Type: evType,
Timestamp: time.Now(),
Route: route,
}
events[i] = event
}
return events, nil
}
// routeMap stores optimal routes per service
optimalRoutes := make(map[string]Route)
// go through all routes found in the routing table and collapse them to optimal routes
for _, route := range routes {
optimal, ok := optimalRoutes[route.Service]
if !ok {
optimalRoutes[route.Service] = route
continue
}
// if the current optimal route metric is higher than routing table route, replace it
if optimal.Metric > route.Metric {
optimalRoutes[route.Service] = route
continue
}
// if the metrics are the same, prefer advertising your own route
if optimal.Metric == route.Metric {
if route.Router == r.options.Id {
optimalRoutes[route.Service] = route
continue
}
}
}
// build a list of events to advertise // build a list of events to advertise
events := make([]*Event, len(routes)) events := make([]*Event, len(optimalRoutes))
for i, route := range routes { i := 0
for _, route := range optimalRoutes {
event := &Event{ event := &Event{
Type: evType, Type: evType,
Timestamp: time.Now(), Timestamp: time.Now(),
Route: route, Route: route,
} }
events[i] = event events[i] = event
i++
} }
return events, nil return events, nil

View File

@ -76,9 +76,10 @@ func Advertise(a Strategy) Option {
// DefaultOptions returns router default options // DefaultOptions returns router default options
func DefaultOptions() Options { func DefaultOptions() Options {
return Options{ return Options{
Id: uuid.New().String(), Id: uuid.New().String(),
Address: DefaultAddress, Address: DefaultAddress,
Network: DefaultNetwork, Network: DefaultNetwork,
Registry: registry.DefaultRegistry, Registry: registry.DefaultRegistry,
Advertise: Optimal,
} }
} }

View File

@ -149,6 +149,18 @@ const (
Optimal Optimal
) )
// String returns human readable Strategy
func (s Strategy) String() string {
switch s {
case All:
return "all"
case Optimal:
return "optimal"
default:
return "unknown"
}
}
// NewRouter creates new Router and returns it // NewRouter creates new Router and returns it
func NewRouter(opts ...Option) Router { func NewRouter(opts ...Option) Router {
return newRouter(opts...) return newRouter(opts...)