Update router querying method (#834)

* Add address to router query options. Drop Query interface for QueryOptions

* Cleanup isMatch function

* Update network proto
This commit is contained in:
Asim Aslam 2019-10-09 17:13:52 +01:00 committed by GitHub
parent 107b7419b7
commit fe94237448
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 249 additions and 122 deletions

View File

@ -43,9 +43,9 @@ type routerKey struct{}
func (r *routerSelector) getRoutes(service string) ([]router.Route, error) {
if !r.remote {
// lookup router for routes for the service
return r.r.Lookup(router.NewQuery(
return r.r.Lookup(
router.QueryService(service),
))
)
}
// lookup the remote router

View File

@ -443,8 +443,8 @@ func (n *network) announce(client transport.Client) {
}
// pruneRoutes prunes routes return by given query
func (n *network) pruneRoutes(q router.Query) error {
routes, err := n.router.Table().Query(q)
func (n *network) pruneRoutes(q ...router.QueryOption) error {
routes, err := n.router.Table().Query(q...)
if err != nil && err != router.ErrRouteNotFound {
return err
}
@ -461,18 +461,18 @@ func (n *network) pruneRoutes(q router.Query) error {
// pruneNodeRoutes prunes routes that were either originated by or routable via given node
func (n *network) prunePeerRoutes(peer *node) error {
// lookup all routes originated by router
q := router.NewQuery(
q := []router.QueryOption{
router.QueryRouter(peer.id),
)
if err := n.pruneRoutes(q); err != nil {
}
if err := n.pruneRoutes(q...); err != nil {
return err
}
// lookup all routes routable via gw
q = router.NewQuery(
q = []router.QueryOption{
router.QueryGateway(peer.id),
)
if err := n.pruneRoutes(q); err != nil {
}
if err := n.pruneRoutes(q...); err != nil {
return err
}

View File

@ -7,6 +7,7 @@ import (
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/network"
pbNet "github.com/micro/go-micro/network/proto"
"github.com/micro/go-micro/router"
pbRtr "github.com/micro/go-micro/router/proto"
)
@ -100,7 +101,29 @@ func (n *Network) Graph(ctx context.Context, req *pbNet.GraphRequest, resp *pbNe
// Routes returns a list of routing table routes
func (n *Network) Routes(ctx context.Context, req *pbNet.RoutesRequest, resp *pbNet.RoutesResponse) error {
routes, err := n.Network.Options().Router.Table().List()
// build query
var qOpts []router.QueryOption
if q := req.Query; q != nil {
if len(q.Service) > 0 {
qOpts = append(qOpts, router.QueryService(q.Service))
}
if len(q.Address) > 0 {
qOpts = append(qOpts, router.QueryAddress(q.Address))
}
if len(q.Gateway) > 0 {
qOpts = append(qOpts, router.QueryGateway(q.Gateway))
}
if len(q.Router) > 0 {
qOpts = append(qOpts, router.QueryRouter(q.Router))
}
if len(q.Network) > 0 {
qOpts = append(qOpts, router.QueryNetwork(q.Network))
}
}
routes, err := n.Network.Options().Router.Table().Query(qOpts...)
if err != nil {
return errors.InternalServerError("go.micro.network", "failed to list routes: %s", err)
}

View File

@ -23,6 +23,78 @@ var _ = math.Inf
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Query is passed in a LookupRequest
type Query struct {
Service string `protobuf:"bytes,1,opt,name=service,proto3" json:"service,omitempty"`
Address string `protobuf:"bytes,2,opt,name=address,proto3" json:"address,omitempty"`
Gateway string `protobuf:"bytes,3,opt,name=gateway,proto3" json:"gateway,omitempty"`
Router string `protobuf:"bytes,4,opt,name=router,proto3" json:"router,omitempty"`
Network string `protobuf:"bytes,5,opt,name=network,proto3" json:"network,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Query) Reset() { *m = Query{} }
func (m *Query) String() string { return proto.CompactTextString(m) }
func (*Query) ProtoMessage() {}
func (*Query) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{0}
}
func (m *Query) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Query.Unmarshal(m, b)
}
func (m *Query) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Query.Marshal(b, m, deterministic)
}
func (m *Query) XXX_Merge(src proto.Message) {
xxx_messageInfo_Query.Merge(m, src)
}
func (m *Query) XXX_Size() int {
return xxx_messageInfo_Query.Size(m)
}
func (m *Query) XXX_DiscardUnknown() {
xxx_messageInfo_Query.DiscardUnknown(m)
}
var xxx_messageInfo_Query proto.InternalMessageInfo
func (m *Query) GetService() string {
if m != nil {
return m.Service
}
return ""
}
func (m *Query) GetAddress() string {
if m != nil {
return m.Address
}
return ""
}
func (m *Query) GetGateway() string {
if m != nil {
return m.Gateway
}
return ""
}
func (m *Query) GetRouter() string {
if m != nil {
return m.Router
}
return ""
}
func (m *Query) GetNetwork() string {
if m != nil {
return m.Network
}
return ""
}
// PeerRequest requests list of peers
type NodesRequest struct {
// node topology depth
@ -36,7 +108,7 @@ func (m *NodesRequest) Reset() { *m = NodesRequest{} }
func (m *NodesRequest) String() string { return proto.CompactTextString(m) }
func (*NodesRequest) ProtoMessage() {}
func (*NodesRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{0}
return fileDescriptor_0b7953b26a7c4730, []int{1}
}
func (m *NodesRequest) XXX_Unmarshal(b []byte) error {
@ -77,7 +149,7 @@ func (m *NodesResponse) Reset() { *m = NodesResponse{} }
func (m *NodesResponse) String() string { return proto.CompactTextString(m) }
func (*NodesResponse) ProtoMessage() {}
func (*NodesResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{1}
return fileDescriptor_0b7953b26a7c4730, []int{2}
}
func (m *NodesResponse) XXX_Unmarshal(b []byte) error {
@ -117,7 +189,7 @@ func (m *GraphRequest) Reset() { *m = GraphRequest{} }
func (m *GraphRequest) String() string { return proto.CompactTextString(m) }
func (*GraphRequest) ProtoMessage() {}
func (*GraphRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{2}
return fileDescriptor_0b7953b26a7c4730, []int{3}
}
func (m *GraphRequest) XXX_Unmarshal(b []byte) error {
@ -156,7 +228,7 @@ func (m *GraphResponse) Reset() { *m = GraphResponse{} }
func (m *GraphResponse) String() string { return proto.CompactTextString(m) }
func (*GraphResponse) ProtoMessage() {}
func (*GraphResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{3}
return fileDescriptor_0b7953b26a7c4730, []int{4}
}
func (m *GraphResponse) XXX_Unmarshal(b []byte) error {
@ -185,6 +257,8 @@ func (m *GraphResponse) GetRoot() *Peer {
}
type RoutesRequest struct {
// filter based on
Query *Query `protobuf:"bytes,1,opt,name=query,proto3" json:"query,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@ -194,7 +268,7 @@ func (m *RoutesRequest) Reset() { *m = RoutesRequest{} }
func (m *RoutesRequest) String() string { return proto.CompactTextString(m) }
func (*RoutesRequest) ProtoMessage() {}
func (*RoutesRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{4}
return fileDescriptor_0b7953b26a7c4730, []int{5}
}
func (m *RoutesRequest) XXX_Unmarshal(b []byte) error {
@ -215,6 +289,13 @@ func (m *RoutesRequest) XXX_DiscardUnknown() {
var xxx_messageInfo_RoutesRequest proto.InternalMessageInfo
func (m *RoutesRequest) GetQuery() *Query {
if m != nil {
return m.Query
}
return nil
}
type RoutesResponse struct {
Routes []*proto1.Route `protobuf:"bytes,1,rep,name=routes,proto3" json:"routes,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
@ -226,7 +307,7 @@ func (m *RoutesResponse) Reset() { *m = RoutesResponse{} }
func (m *RoutesResponse) String() string { return proto.CompactTextString(m) }
func (*RoutesResponse) ProtoMessage() {}
func (*RoutesResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{5}
return fileDescriptor_0b7953b26a7c4730, []int{6}
}
func (m *RoutesResponse) XXX_Unmarshal(b []byte) error {
@ -264,7 +345,7 @@ func (m *ServicesRequest) Reset() { *m = ServicesRequest{} }
func (m *ServicesRequest) String() string { return proto.CompactTextString(m) }
func (*ServicesRequest) ProtoMessage() {}
func (*ServicesRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{6}
return fileDescriptor_0b7953b26a7c4730, []int{7}
}
func (m *ServicesRequest) XXX_Unmarshal(b []byte) error {
@ -296,7 +377,7 @@ func (m *ServicesResponse) Reset() { *m = ServicesResponse{} }
func (m *ServicesResponse) String() string { return proto.CompactTextString(m) }
func (*ServicesResponse) ProtoMessage() {}
func (*ServicesResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{7}
return fileDescriptor_0b7953b26a7c4730, []int{8}
}
func (m *ServicesResponse) XXX_Unmarshal(b []byte) error {
@ -339,7 +420,7 @@ func (m *Node) Reset() { *m = Node{} }
func (m *Node) String() string { return proto.CompactTextString(m) }
func (*Node) ProtoMessage() {}
func (*Node) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{8}
return fileDescriptor_0b7953b26a7c4730, []int{9}
}
func (m *Node) XXX_Unmarshal(b []byte) error {
@ -387,7 +468,7 @@ func (m *Connect) Reset() { *m = Connect{} }
func (m *Connect) String() string { return proto.CompactTextString(m) }
func (*Connect) ProtoMessage() {}
func (*Connect) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{9}
return fileDescriptor_0b7953b26a7c4730, []int{10}
}
func (m *Connect) XXX_Unmarshal(b []byte) error {
@ -428,7 +509,7 @@ func (m *Close) Reset() { *m = Close{} }
func (m *Close) String() string { return proto.CompactTextString(m) }
func (*Close) ProtoMessage() {}
func (*Close) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{10}
return fileDescriptor_0b7953b26a7c4730, []int{11}
}
func (m *Close) XXX_Unmarshal(b []byte) error {
@ -471,7 +552,7 @@ func (m *Peer) Reset() { *m = Peer{} }
func (m *Peer) String() string { return proto.CompactTextString(m) }
func (*Peer) ProtoMessage() {}
func (*Peer) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{11}
return fileDescriptor_0b7953b26a7c4730, []int{12}
}
func (m *Peer) XXX_Unmarshal(b []byte) error {
@ -507,6 +588,7 @@ func (m *Peer) GetPeers() []*Peer {
}
func init() {
proto.RegisterType((*Query)(nil), "go.micro.network.Query")
proto.RegisterType((*NodesRequest)(nil), "go.micro.network.NodesRequest")
proto.RegisterType((*NodesResponse)(nil), "go.micro.network.NodesResponse")
proto.RegisterType((*GraphRequest)(nil), "go.micro.network.GraphRequest")
@ -526,33 +608,38 @@ func init() {
}
var fileDescriptor_0b7953b26a7c4730 = []byte{
// 416 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x93, 0x5d, 0x6f, 0xda, 0x30,
0x14, 0x86, 0x21, 0x10, 0x3e, 0xce, 0x16, 0x60, 0xd6, 0x34, 0x45, 0xb9, 0xd8, 0x98, 0xb5, 0x0b,
0x34, 0x6d, 0x66, 0x02, 0x71, 0x35, 0x4d, 0x9a, 0xc4, 0x45, 0xa5, 0x4a, 0x45, 0x95, 0xf9, 0x03,
0x85, 0xc4, 0x82, 0xa8, 0x25, 0x4e, 0x1d, 0xd3, 0xfe, 0xc2, 0xfe, 0xaf, 0xca, 0x1f, 0xe1, 0x33,
0x41, 0xed, 0x1d, 0xe7, 0xf0, 0xf8, 0x3d, 0x3e, 0xaf, 0xdf, 0xc0, 0x64, 0x15, 0xcb, 0xf5, 0x76,
0x49, 0x42, 0xbe, 0x19, 0x6e, 0xe2, 0x50, 0xf0, 0xe1, 0x8a, 0xff, 0x36, 0x3f, 0x12, 0x26, 0x9f,
0xb9, 0xb8, 0x1f, 0xa6, 0x82, 0xcb, 0x5d, 0x45, 0x74, 0x85, 0x7a, 0x2b, 0x4e, 0x34, 0x45, 0x6c,
0x3f, 0x18, 0x97, 0x0b, 0x09, 0xbe, 0x95, 0x4c, 0x58, 0x1d, 0x53, 0x18, 0x19, 0xfc, 0x03, 0x3e,
0xce, 0x78, 0xc4, 0x32, 0xca, 0x1e, 0xb7, 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4f, 0x19,
0x13, 0xea, 0xde, 0xb5, 0x0b, 0x8f, 0x62, 0xa0, 0xd1, 0x8b, 0x03, 0xcd, 0x99, 0xe9, 0xa3, 0x6b,
0x70, 0xf5, 0xf3, 0xa2, 0xaf, 0xe7, 0x67, 0x0e, 0xd3, 0x11, 0x7c, 0x2b, 0xfd, 0xdf, 0x38, 0x8e,
0x2b, 0x4a, 0x4b, 0xe7, 0xb1, 0x48, 0xeb, 0x30, 0xce, 0x45, 0x5a, 0x47, 0x41, 0xc6, 0x15, 0x74,
0x03, 0x0d, 0x13, 0x14, 0x54, 0x00, 0x1f, 0x65, 0x2a, 0xe8, 0x97, 0x03, 0x3b, 0xb9, 0x39, 0xb4,
0xf2, 0x88, 0xa0, 0xef, 0xe7, 0xfc, 0x49, 0xa2, 0x02, 0x7c, 0x09, 0xc9, 0x45, 0x97, 0x0d, 0xfd,
0xb1, 0x8e, 0x5f, 0x03, 0x00, 0x00, 0xff, 0xff, 0x3a, 0x63, 0x7a, 0x7b, 0x2c, 0x04, 0x00, 0x00,
// 482 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0x4d, 0x6f, 0xd3, 0x40,
0x10, 0x6d, 0x3e, 0x9c, 0xb4, 0x03, 0x29, 0x65, 0x85, 0x8a, 0xe5, 0x03, 0x84, 0x15, 0x87, 0x0a,
0x51, 0x07, 0x35, 0xea, 0x09, 0x81, 0x90, 0x7a, 0x40, 0x42, 0xa2, 0x82, 0xed, 0x1f, 0x20, 0x8d,
0x47, 0x89, 0x05, 0xf5, 0xba, 0xbb, 0x1b, 0xaa, 0xfe, 0x02, 0x7e, 0x19, 0xff, 0x0b, 0xed, 0xce,
0xd8, 0xb4, 0x89, 0x1d, 0xc1, 0xcd, 0x6f, 0xe7, 0xcd, 0x9b, 0xdd, 0x37, 0x4f, 0x86, 0xd3, 0x45,
0xee, 0x96, 0xab, 0xcb, 0x74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x6b, 0x89, 0x4a, 0xe4, 0x36, 0x4a, 0x25, 0x7a, 0x39,
0x08, 0x7f, 0x99, 0xe9, 0x9f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x07, 0x84, 0x80, 0x26, 0xe5, 0x04,
0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.

View File

@ -16,6 +16,15 @@ service Network {
rpc Services(ServicesRequest) returns (ServicesResponse) {};
}
// Query is passed in a LookupRequest
message Query {
string service = 1;
string address = 2;
string gateway = 3;
string router = 4;
string network = 5;
}
// PeerRequest requests list of peers
message NodesRequest {
// node topology depth
@ -38,6 +47,8 @@ message GraphResponse {
}
message RoutesRequest {
// filter based on
Query query = 1;
}
message RoutesResponse {

View File

@ -128,7 +128,7 @@ func (p *Proxy) getRoute(service string) ([]router.Route, error) {
p.Unlock()
// lookup the routes in the router
results, err := p.Router.Lookup(router.NewQuery(router.QueryService(service)))
results, err := p.Router.Lookup(router.QueryService(service))
if err != nil {
// check the status of the router
if status := p.Router.Status(); status.Code == router.Error {

View File

@ -722,8 +722,8 @@ func (r *router) Solicit() error {
}
// Lookup routes in the routing table
func (r *router) Lookup(q Query) ([]Route, error) {
return r.table.Query(q)
func (r *router) Lookup(q ...QueryOption) ([]Route, error) {
return r.table.Query(q...)
}
// Watch routes

View File

@ -17,11 +17,7 @@ type Router struct {
// Lookup looks up routes in the routing table and returns them
func (r *Router) Lookup(ctx context.Context, req *pb.LookupRequest, resp *pb.LookupResponse) error {
query := router.NewQuery(
router.QueryService(req.Query.Service),
)
routes, err := r.Router.Lookup(query)
routes, err := r.Router.Lookup(router.QueryService(req.Query.Service))
if err != nil {
return errors.InternalServerError("go.micro.router", "failed to lookup routes: %v", err)
}

View File

@ -90,11 +90,7 @@ func (t *Table) List(ctx context.Context, req *pb.Request, resp *pb.ListResponse
}
func (t *Table) Query(ctx context.Context, req *pb.QueryRequest, resp *pb.QueryResponse) error {
query := router.NewQuery(
router.QueryService(req.Query.Service),
)
routes, err := t.Router.Table().Query(query)
routes, err := t.Router.Table().Query(router.QueryService(req.Query.Service))
if err != nil {
return errors.InternalServerError("go.micro.router", "failed to lookup routes: %s", err)
}

View File

@ -7,6 +7,8 @@ type QueryOption func(*QueryOptions)
type QueryOptions struct {
// Service is destination service name
Service string
// Address of the service
Address string
// Gateway is route gateway
Gateway string
// Network is network address
@ -22,6 +24,13 @@ func QueryService(s string) QueryOption {
}
}
// QueryAddress sets service to query
func QueryAddress(a string) QueryOption {
return func(o *QueryOptions) {
o.Address = a
}
}
// QueryGateway sets gateway address to query
func QueryGateway(g string) QueryOption {
return func(o *QueryOptions) {
@ -43,22 +52,12 @@ func QueryRouter(r string) QueryOption {
}
}
// Query is routing table query
type Query interface {
// Options returns query options
Options() QueryOptions
}
// query is a basic implementation of Query
type query struct {
opts QueryOptions
}
// NewQuery creates new query and returns it
func NewQuery(opts ...QueryOption) Query {
func NewQuery(opts ...QueryOption) QueryOptions {
// default options
qopts := QueryOptions{
Service: "*",
Address: "*",
Gateway: "*",
Network: "*",
Router: "*",
@ -68,12 +67,5 @@ func NewQuery(opts ...QueryOption) Query {
o(&qopts)
}
return &query{
opts: qopts,
}
}
// Options returns query options
func (q *query) Options() QueryOptions {
return q.opts
return qopts
}

View File

@ -31,7 +31,7 @@ type Router interface {
// Solicit advertises the whole routing table to the network
Solicit() error
// Lookup queries routes in the routing table
Lookup(Query) ([]Route, error)
Lookup(...QueryOption) ([]Route, error)
// Watch returns a watcher which tracks updates to the routing table
Watch(opts ...WatchOption) (Watcher, error)
// Start starts the router
@ -55,7 +55,7 @@ type Table interface {
// List all routes in the table
List() ([]Route, error)
// Query routes in the routing table
Query(Query) ([]Route, error)
Query(...QueryOption) ([]Route, error)
}
// Option used by the router

View File

@ -321,13 +321,15 @@ func (s *svc) Stop() error {
}
// Lookup looks up routes in the routing table and returns them
func (s *svc) Lookup(q router.Query) ([]router.Route, error) {
func (s *svc) Lookup(q ...router.QueryOption) ([]router.Route, error) {
// call the router
query := router.NewQuery(q...)
resp, err := s.router.Lookup(context.Background(), &pb.LookupRequest{
Query: &pb.Query{
Service: q.Options().Service,
Gateway: q.Options().Gateway,
Network: q.Options().Network,
Service: query.Service,
Gateway: query.Gateway,
Network: query.Network,
},
}, s.callOpts...)

View File

@ -90,13 +90,15 @@ func (t *table) List() ([]router.Route, error) {
}
// Lookup looks up routes in the routing table and returns them
func (t *table) Query(q router.Query) ([]router.Route, error) {
func (t *table) Query(q ...router.QueryOption) ([]router.Route, error) {
query := router.NewQuery(q...)
// call the router
resp, err := t.table.Query(context.Background(), &pb.QueryRequest{
Query: &pb.Query{
Service: q.Options().Service,
Gateway: q.Options().Gateway,
Network: q.Options().Network,
Service: query.Service,
Gateway: query.Gateway,
Network: query.Network,
},
}, t.callOpts...)

View File

@ -135,22 +135,44 @@ func (t *table) List() ([]Route, error) {
}
// isMatch checks if the route matches given query options
func isMatch(route Route, gateway, network, router string) bool {
if gateway == "*" || gateway == route.Gateway {
if network == "*" || network == route.Network {
if router == "*" || router == route.Router {
return true
}
func isMatch(route Route, address, gateway, network, router string) bool {
// matches the values provided
match := func(a, b string) bool {
if a == "*" || a == b {
return true
}
return false
}
// a simple struct to hold our values
type compare struct {
a string
b string
}
// compare the following values
values := []compare{
{gateway, route.Gateway},
{network, route.Network},
{router, route.Router},
{address, route.Address},
}
for _, v := range values {
// attempt to match each value
if !match(v.a, v.b) {
return false
}
}
return false
return true
}
// findRoutes finds all the routes for given network and router and returns them
func findRoutes(routes map[uint64]Route, gateway, network, router string) []Route {
func findRoutes(routes map[uint64]Route, address, gateway, network, router string) []Route {
var results []Route
for _, route := range routes {
if isMatch(route, gateway, network, router) {
if isMatch(route, address, gateway, network, router) {
results = append(results, route)
}
}
@ -158,21 +180,24 @@ func findRoutes(routes map[uint64]Route, gateway, network, router string) []Rout
}
// Lookup queries routing table and returns all routes that match the lookup query
func (t *table) Query(q Query) ([]Route, error) {
func (t *table) Query(q ...QueryOption) ([]Route, error) {
t.RLock()
defer t.RUnlock()
if q.Options().Service != "*" {
if _, ok := t.routes[q.Options().Service]; !ok {
// create new query options
opts := NewQuery(q...)
if opts.Service != "*" {
if _, ok := t.routes[opts.Service]; !ok {
return nil, ErrRouteNotFound
}
return findRoutes(t.routes[q.Options().Service], q.Options().Gateway, q.Options().Network, q.Options().Router), nil
return findRoutes(t.routes[opts.Service], opts.Address, opts.Gateway, opts.Network, opts.Router), nil
}
var results []Route
// search through all destinations
for _, routes := range t.routes {
results = append(results, findRoutes(routes, q.Options().Gateway, q.Options().Network, q.Options().Router)...)
results = append(results, findRoutes(routes, opts.Address, opts.Gateway, opts.Network, opts.Router)...)
}
return results, nil

View File

@ -123,18 +123,15 @@ func TestQuery(t *testing.T) {
}
// return all routes
query := NewQuery()
routes, err := table.Query(query)
routes, err := table.Query()
if err != nil {
t.Errorf("error looking up routes: %s", err)
}
// query routes particular network
network := "net1"
query = NewQuery(QueryNetwork(network))
routes, err = table.Query(query)
routes, err = table.Query(QueryNetwork(network))
if err != nil {
t.Errorf("error looking up routes: %s", err)
}
@ -151,9 +148,8 @@ func TestQuery(t *testing.T) {
// query routes for particular gateway
gateway := "gw1"
query = NewQuery(QueryGateway(gateway))
routes, err = table.Query(query)
routes, err = table.Query(QueryGateway(gateway))
if err != nil {
t.Errorf("error looking up routes: %s", err)
}
@ -168,9 +164,8 @@ func TestQuery(t *testing.T) {
// query routes for particular router
router := "rtr1"
query = NewQuery(QueryRouter(router))
routes, err = table.Query(query)
routes, err = table.Query(QueryRouter(router))
if err != nil {
t.Errorf("error looking up routes: %s", err)
}
@ -184,13 +179,13 @@ func TestQuery(t *testing.T) {
}
// query particular gateway and network
query = NewQuery(
query := []QueryOption{
QueryGateway(gateway),
QueryNetwork(network),
QueryRouter(router),
)
}
routes, err = table.Query(query)
routes, err = table.Query(query...)
if err != nil {
t.Errorf("error looking up routes: %s", err)
}
@ -212,9 +207,7 @@ func TestQuery(t *testing.T) {
}
// non-existen route query
query = NewQuery(QueryService("foobar"))
routes, err = table.Query(query)
routes, err = table.Query(QueryService("foobar"))
if err != ErrRouteNotFound {
t.Errorf("error looking up routes. Expected: %s, found: %s", ErrRouteNotFound, err)
}