Router table.Read replaces List/Query (#1966)

* Table.REad insted of list and query

* fmt
This commit is contained in:
Asim Aslam
2020-08-23 13:10:48 +01:00
committed by GitHub
parent fc54503232
commit 82e8298b73
10 changed files with 44 additions and 680 deletions

View File

@@ -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
}
}

View File

@@ -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 {

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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

View File

@@ -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