From 08da7c128367db9884fadd914a34e1f88b5d7414 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Thu, 6 Jun 2019 16:37:40 +0100 Subject: [PATCH] First commit: Outline of Router interface --- router/default.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++ router/options.go | 31 ++++++++++++++++++++++++++ router/route.go | 7 ++++++ router/router.go | 50 +++++++++++++++++++++++++++++++++++++++++ router/service.go | 7 ++++++ 5 files changed, 152 insertions(+) create mode 100644 router/default.go create mode 100644 router/options.go create mode 100644 router/route.go create mode 100644 router/router.go create mode 100644 router/service.go diff --git a/router/default.go b/router/default.go new file mode 100644 index 00000000..4f5b1a77 --- /dev/null +++ b/router/default.go @@ -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 "" +} diff --git a/router/options.go b/router/options.go new file mode 100644 index 00000000..5cfbcd13 --- /dev/null +++ b/router/options.go @@ -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 + } +} diff --git a/router/route.go b/router/route.go new file mode 100644 index 00000000..d9b80ce1 --- /dev/null +++ b/router/route.go @@ -0,0 +1,7 @@ +package router + +// Route is micro network route +type Route struct { + // Hop is the next route hop + Hop Router +} diff --git a/router/router.go b/router/router.go new file mode 100644 index 00000000..9142ea81 --- /dev/null +++ b/router/router.go @@ -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...) +} diff --git a/router/service.go b/router/service.go new file mode 100644 index 00000000..3d944c4a --- /dev/null +++ b/router/service.go @@ -0,0 +1,7 @@ +package router + +// Service is a service to route to +type Service struct { + // Name is service name + Name string +}