Make Optimal strategy default. Collapse routing tables based on strategy
This commit is contained in:
parent
96e564e402
commit
837597fe6f
@ -682,9 +682,11 @@ func (r *router) flushRouteEvents(evType EventType) ([]*Event, error) {
|
||||
return nil, fmt.Errorf("failed listing routes: %s", err)
|
||||
}
|
||||
|
||||
// TODO: flush the routes based on strategy here
|
||||
// - collapse the routes per service-metric-locality (prefer your routes)
|
||||
r.RLock()
|
||||
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 {
|
||||
@ -695,6 +697,45 @@ func (r *router) flushRouteEvents(evType EventType) ([]*Event, error) {
|
||||
}
|
||||
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
|
||||
events := make([]*Event, len(optimalRoutes))
|
||||
i := 0
|
||||
for _, route := range optimalRoutes {
|
||||
event := &Event{
|
||||
Type: evType,
|
||||
Timestamp: time.Now(),
|
||||
Route: route,
|
||||
}
|
||||
events[i] = event
|
||||
i++
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
@ -80,5 +80,6 @@ func DefaultOptions() Options {
|
||||
Address: DefaultAddress,
|
||||
Network: DefaultNetwork,
|
||||
Registry: registry.DefaultRegistry,
|
||||
Advertise: Optimal,
|
||||
}
|
||||
}
|
||||
|
@ -149,6 +149,18 @@ const (
|
||||
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
|
||||
func NewRouter(opts ...Option) Router {
|
||||
return newRouter(opts...)
|
||||
|
Loading…
x
Reference in New Issue
Block a user