mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
Add router advertisement Strategy option to router.
This commit is contained in:
parent
fe94237448
commit
96e564e402
@ -319,23 +319,13 @@ func (r *router) advertiseTable() error {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
// list routing table routes to announce
|
||||
routes, err := r.table.List()
|
||||
// do full table flush
|
||||
events, err := r.flushRouteEvents(Update)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed listing routes: %s", err)
|
||||
}
|
||||
// collect all the added routes before we attempt to add default gateway
|
||||
events := make([]*Event, len(routes))
|
||||
for i, route := range routes {
|
||||
event := &Event{
|
||||
Type: Update,
|
||||
Timestamp: time.Now(),
|
||||
Route: route,
|
||||
}
|
||||
events[i] = event
|
||||
return fmt.Errorf("failed flushing routes: %s", err)
|
||||
}
|
||||
|
||||
// advertise all routes as Update events to subscribers
|
||||
// advertise routes to subscribers
|
||||
if len(events) > 0 {
|
||||
log.Debugf("Router flushing table with %d events: %s", len(events), r.options.Id)
|
||||
r.advertWg.Add(1)
|
||||
@ -692,6 +682,9 @@ 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)
|
||||
|
||||
// build a list of events to advertise
|
||||
events := make([]*Event, len(routes))
|
||||
for i, route := range routes {
|
||||
|
@ -18,6 +18,8 @@ type Options struct {
|
||||
Network string
|
||||
// Registry is the local registry
|
||||
Registry registry.Registry
|
||||
// Advertise is the advertising strategy
|
||||
Advertise Strategy
|
||||
// Client for calling router
|
||||
Client client.Client
|
||||
}
|
||||
@ -64,6 +66,13 @@ func Registry(r registry.Registry) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// Strategy sets route advertising strategy
|
||||
func Advertise(a Strategy) Option {
|
||||
return func(o *Options) {
|
||||
o.Advertise = a
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultOptions returns router default options
|
||||
func DefaultOptions() Options {
|
||||
return Options{
|
||||
|
@ -139,6 +139,16 @@ type Advert struct {
|
||||
Events []*Event
|
||||
}
|
||||
|
||||
// Strategy is route advertisement strategy
|
||||
type Strategy int
|
||||
|
||||
const (
|
||||
// All advertises all routes to the network
|
||||
All Strategy = iota
|
||||
// Optimal advertises optimal routes to the network
|
||||
Optimal
|
||||
)
|
||||
|
||||
// NewRouter creates new Router and returns it
|
||||
func NewRouter(opts ...Option) Router {
|
||||
return newRouter(opts...)
|
||||
|
Loading…
Reference in New Issue
Block a user