Router table.Read replaces List/Query (#1966)
* Table.REad insted of list and query * fmt
This commit is contained in:
@@ -77,3 +77,16 @@ func DefaultOptions() Options {
|
||||
Context: context.Background(),
|
||||
}
|
||||
}
|
||||
|
||||
type ReadOptions struct {
|
||||
Service string
|
||||
}
|
||||
|
||||
type ReadOption func(o *ReadOptions)
|
||||
|
||||
// ReadService sets the service to read from the table
|
||||
func ReadService(s string) ReadOption {
|
||||
return func(o *ReadOptions) {
|
||||
o.Service = s
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,7 +266,7 @@ func (r *rtr) Lookup(service string, opts ...router.LookupOption) ([]router.Rout
|
||||
q := router.NewLookup(opts...)
|
||||
|
||||
// if we find the routes filter and return them
|
||||
routes, err := r.table.Query(service)
|
||||
routes, err := r.table.Read(router.ReadService(service))
|
||||
if err == nil {
|
||||
routes = router.Filter(routes, q)
|
||||
if len(routes) == 0 {
|
||||
|
||||
@@ -198,35 +198,35 @@ func (t *table) Update(r router.Route) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// List returns a list of all routes in the table
|
||||
func (t *table) List() ([]router.Route, error) {
|
||||
// Read entries from the table
|
||||
func (t *table) Read(opts ...router.ReadOption) ([]router.Route, error) {
|
||||
var options router.ReadOptions
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
t.RLock()
|
||||
defer t.RUnlock()
|
||||
|
||||
var routes []router.Route
|
||||
for _, rmap := range t.routes {
|
||||
for _, route := range rmap {
|
||||
routes = append(routes, route.route)
|
||||
|
||||
// get the routes based on options passed
|
||||
if len(options.Service) > 0 {
|
||||
routeMap, ok := t.routes[options.Service]
|
||||
if !ok {
|
||||
return nil, router.ErrRouteNotFound
|
||||
}
|
||||
for _, rt := range routeMap {
|
||||
routes = append(routes, rt.route)
|
||||
}
|
||||
return routes, nil
|
||||
}
|
||||
|
||||
return routes, nil
|
||||
}
|
||||
|
||||
// Lookup queries routing table and returns all routes that match the lookup query
|
||||
func (t *table) Query(service string) ([]router.Route, error) {
|
||||
t.RLock()
|
||||
defer t.RUnlock()
|
||||
|
||||
routeMap, ok := t.routes[service]
|
||||
if !ok {
|
||||
return nil, router.ErrRouteNotFound
|
||||
}
|
||||
|
||||
var routes []router.Route
|
||||
|
||||
for _, rt := range routeMap {
|
||||
routes = append(routes, rt.route)
|
||||
// otherwise get all routes
|
||||
for _, serviceRoutes := range t.routes {
|
||||
for _, rt := range serviceRoutes {
|
||||
routes = append(routes, rt.route)
|
||||
}
|
||||
}
|
||||
|
||||
return routes, nil
|
||||
|
||||
@@ -99,7 +99,7 @@ func TestList(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
routes, err := table.List()
|
||||
routes, err := table.Read()
|
||||
if err != nil {
|
||||
t.Fatalf("error listing routes: %s", err)
|
||||
}
|
||||
@@ -116,7 +116,7 @@ func TestQuery(t *testing.T) {
|
||||
t.Fatalf("error adding route: %s", err)
|
||||
}
|
||||
|
||||
rt, err := table.Query(route.Service)
|
||||
rt, err := table.Read(router.ReadService(route.Service))
|
||||
if err != nil {
|
||||
t.Fatal("Expected a route got err", err)
|
||||
}
|
||||
|
||||
@@ -45,10 +45,8 @@ type Table interface {
|
||||
Delete(Route) error
|
||||
// Update route in the routing table
|
||||
Update(Route) error
|
||||
// List all routes in the table
|
||||
List() ([]Route, error)
|
||||
// Query routes in the routing table
|
||||
Query(service string) ([]Route, error)
|
||||
// Read is for querying the table
|
||||
Read(...ReadOption) ([]Route, error)
|
||||
}
|
||||
|
||||
// Option used by the router
|
||||
|
||||
@@ -44,7 +44,7 @@ func (s *static) Table() router.Table {
|
||||
func (s *static) Lookup(service string, opts ...router.LookupOption) ([]router.Route, error) {
|
||||
options := router.NewLookup(opts...)
|
||||
|
||||
_, _ , err := net.SplitHostPort(service)
|
||||
_, _, err := net.SplitHostPort(service)
|
||||
if err == nil {
|
||||
// use the address
|
||||
options.Address = service
|
||||
|
||||
Reference in New Issue
Block a user