Added network ID option. Added mutex to routing table.
This commit is contained in:
		| @@ -1,27 +1,32 @@ | |||||||
| package router | package router | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"github.com/google/uuid" | ||||||
| 	"github.com/micro/go-micro/registry" | 	"github.com/micro/go-micro/registry" | ||||||
| 	"github.com/micro/go-micro/registry/gossip" | 	"github.com/micro/go-micro/registry/gossip" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type router struct { | type router struct { | ||||||
| 	opts Options | 	opts  Options | ||||||
| 	goss registry.Registry | 	goss  registry.Registry | ||||||
| 	t    Table | 	table Table | ||||||
|  | 	id    uuid.UUID | ||||||
| } | } | ||||||
|  |  | ||||||
| func newRouter(opts ...Option) Router { | func newRouter(opts ...Option) Router { | ||||||
| 	// TODO: figure out how to supply gossip registry options | 	// TODO: figure out how to supply gossip registry options | ||||||
| 	r := &router{ | 	r := &router{ | ||||||
| 		goss: gossip.NewRegistry(), | 		goss:  gossip.NewRegistry(), | ||||||
| 		t:    NewTable(), | 		table: NewTable(), | ||||||
|  | 		id:    uuid.New(), | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	for _, o := range opts { | 	for _, o := range opts { | ||||||
| 		o(&r.opts) | 		o(&r.opts) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// TODO: need to start some gossip.Registry watch here | ||||||
|  |  | ||||||
| 	return r | 	return r | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -63,10 +68,15 @@ func (r *router) Lookup(q Query) ([]*Entry, error) { | |||||||
|  |  | ||||||
| // Table returns routing table | // Table returns routing table | ||||||
| func (r *router) Table() Table { | func (r *router) Table() Table { | ||||||
| 	return nil | 	return r.table | ||||||
| } | } | ||||||
|  |  | ||||||
| // Address returns router's network address | // Network returns router's micro network | ||||||
|  | func (r *router) Network() string { | ||||||
|  | 	return r.opts.Network | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Address returns router's bind address | ||||||
| func (r *router) Address() string { | func (r *router) Address() string { | ||||||
| 	return r.opts.Address | 	return r.opts.Address | ||||||
| } | } | ||||||
|   | |||||||
| @@ -8,6 +8,8 @@ import ( | |||||||
| type Options struct { | type Options struct { | ||||||
| 	// Address is router address | 	// Address is router address | ||||||
| 	Address string | 	Address string | ||||||
|  | 	// Network defines micro network address | ||||||
|  | 	Network string | ||||||
| 	// RIB is Routing Information Base | 	// RIB is Routing Information Base | ||||||
| 	RIB RIB | 	RIB RIB | ||||||
| 	// Table is routing table | 	// Table is routing table | ||||||
| @@ -16,13 +18,6 @@ type Options struct { | |||||||
| 	Context context.Context | 	Context context.Context | ||||||
| } | } | ||||||
|  |  | ||||||
| // RIBase allows to configure RIB |  | ||||||
| func RIBase(r RIB) Option { |  | ||||||
| 	return func(o *Options) { |  | ||||||
| 		o.RIB = r |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Address allows to set router address | // Address allows to set router address | ||||||
| func Address(a string) Option { | func Address(a string) Option { | ||||||
| 	return func(o *Options) { | 	return func(o *Options) { | ||||||
| @@ -30,6 +25,20 @@ func Address(a string) Option { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // Network allows to set router network | ||||||
|  | func Network(n string) Option { | ||||||
|  | 	return func(o *Options) { | ||||||
|  | 		o.Network = n | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // RIBase allows to configure RIB | ||||||
|  | func RIBase(r RIB) Option { | ||||||
|  | 	return func(o *Options) { | ||||||
|  | 		o.RIB = r | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| // RoutingTable allows to specify custom routing table | // RoutingTable allows to specify custom routing table | ||||||
| func RoutingTable(t Table) Option { | func RoutingTable(t Table) Option { | ||||||
| 	return func(o *Options) { | 	return func(o *Options) { | ||||||
|   | |||||||
| @@ -24,6 +24,8 @@ type Router interface { | |||||||
| 	Table() Table | 	Table() Table | ||||||
| 	// Address is Router adddress | 	// Address is Router adddress | ||||||
| 	Address() string | 	Address() string | ||||||
|  | 	// Network defines network router is in | ||||||
|  | 	Network() string | ||||||
| 	// String implemens fmt.Stringer interface | 	// String implemens fmt.Stringer interface | ||||||
| 	String() string | 	String() string | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,6 +1,9 @@ | |||||||
| package router | package router | ||||||
|  |  | ||||||
| import "errors" | import ( | ||||||
|  | 	"errors" | ||||||
|  | 	"sync" | ||||||
|  | ) | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| 	// DefaultRouter returns default micro router | 	// DefaultRouter returns default micro router | ||||||
| @@ -29,8 +32,8 @@ type Table interface { | |||||||
|  |  | ||||||
| // Entry is micro network routing table entry | // Entry is micro network routing table entry | ||||||
| type Entry struct { | type Entry struct { | ||||||
| 	// DestAddr is destination address | 	// Addr is destination address | ||||||
| 	DestAddr string | 	Addr string | ||||||
| 	// NetID is micro network ID | 	// NetID is micro network ID | ||||||
| 	NetID string | 	NetID string | ||||||
| 	// Hop is the next route hop | 	// Hop is the next route hop | ||||||
| @@ -44,6 +47,7 @@ type Entry struct { | |||||||
| type table struct { | type table struct { | ||||||
| 	// m stores routing table map | 	// m stores routing table map | ||||||
| 	m map[string][]Entry | 	m map[string][]Entry | ||||||
|  | 	sync.RWMutex | ||||||
| } | } | ||||||
|  |  | ||||||
| // NewTable creates new routing table and returns it | // NewTable creates new routing table and returns it | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user