First commit: Outline of Router interface

This commit is contained in:
Milos Gajdos 2019-06-06 16:37:40 +01:00
parent 6587ae07be
commit 08da7c1283
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
5 changed files with 152 additions and 0 deletions

57
router/default.go Normal file
View File

@ -0,0 +1,57 @@
package router
import (
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/registry/gossip"
)
type router struct {
opts Options
goss registry.Registry
}
func newRouter(opts ...Option) Router {
// TODO: for now default options
goss := gossip.NewRegistry()
r := &router{
goss: goss,
}
for _, o := range opts {
o(&r.opts)
}
return r
}
func (r *router) Init(opts ...Option) error {
for _, o := range opts {
o(&r.opts)
}
return nil
}
func (r *router) Options() Options {
return r.opts
}
func (r *router) AddRoute(route *Route, opts ...RouteOption) error {
return nil
}
func (r *router) RemoveRoute(route *Route) error {
return nil
}
func (r *router) GetRoute(s *Service) ([]*Route, error) {
return nil, nil
}
func (r *router) List() ([]*Route, error) {
return nil, nil
}
func (r *router) String() string {
return ""
}

31
router/options.go Normal file
View File

@ -0,0 +1,31 @@
package router
import (
"context"
"time"
"github.com/micro/go-micro/registry"
)
// Options allows to set Router options
type Options struct {
// Registry is route source registry i.e. local registry
Registry registry.Registry
// Context stores arbitrary options
Context context.Context
}
// RouteOption allows to soecify routing table options
type RouteOption struct {
// TTL defines route entry lifetime
TTL time.Duration
// COntext allows to specify other arbitrary options
Context context.Context
}
// Registry is local registry
func Registry(r registry.Registry) Option {
return func(o *Options) {
o.Registry = r
}
}

7
router/route.go Normal file
View File

@ -0,0 +1,7 @@
package router
// Route is micro network route
type Route struct {
// Hop is the next route hop
Hop Router
}

50
router/router.go Normal file
View File

@ -0,0 +1,50 @@
// Package router provides an interface for micro network routers
package router
import (
"errors"
"github.com/micro/go-micro/registry"
)
// Router is micro network router
type Router interface {
// Initi initializes Router with options
Init(...Option) error
// Options returns Router options
Options() Options
// AddRoute adds new service route
AddRoute(*Route, ...RouteOption) error
// RemoveRoute removes service route
RemoveRoute(*Route) error
// GetRoute returns list of routes for service
GetRoute(*Service) ([]*Route, error)
// List returns all routes
List() ([]*Route, error)
// String implemens fmt.Stringer interface
String() string
}
// Option used by the Router
type Option func(*Options)
var (
DefaultRouter = NewRouter()
// Not found error when Get is called
ErrNotFound = errors.New("route not found")
)
// NewRouter creates new Router and returns it
func NewRouter(opts ...Option) Router {
// set Registry to DefaultRegistry
opt := Options{
Registry: registry.DefaultRegistry,
}
for _, o := range opts {
o(&opt)
}
return newRouter(opts...)
}

7
router/service.go Normal file
View File

@ -0,0 +1,7 @@
package router
// Service is a service to route to
type Service struct {
// Name is service name
Name string
}