2020-07-27 15:22:00 +03:00
|
|
|
package registry
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2019-07-10 19:46:22 +03:00
|
|
|
"time"
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/logger"
|
|
|
|
"github.com/micro/go-micro/v3/router"
|
2019-08-28 01:08:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// table is an in-memory routing table
|
2019-07-29 14:40:13 +03:00
|
|
|
type table struct {
|
2019-07-29 14:44:28 +03:00
|
|
|
sync.RWMutex
|
2019-07-26 01:19:05 +03:00
|
|
|
// routes stores service routes
|
2020-08-07 22:53:38 +03:00
|
|
|
routes map[string]map[uint64]*route
|
2019-07-26 01:19:05 +03:00
|
|
|
// watchers stores table watchers
|
|
|
|
watchers map[string]*tableWatcher
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
type route struct {
|
|
|
|
route router.Route
|
|
|
|
updated time.Time
|
|
|
|
}
|
|
|
|
|
2019-07-29 14:40:13 +03:00
|
|
|
// newtable creates a new routing table and returns it
|
2020-08-21 11:23:01 +03:00
|
|
|
func newTable() *table {
|
2019-07-29 14:40:13 +03:00
|
|
|
return &table{
|
2020-08-07 22:53:38 +03:00
|
|
|
routes: make(map[string]map[uint64]*route),
|
|
|
|
watchers: make(map[string]*tableWatcher),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pruneRoutes will prune routes older than the time specified
|
|
|
|
func (t *table) pruneRoutes(olderThan time.Duration) {
|
|
|
|
var routes []router.Route
|
|
|
|
|
|
|
|
t.Lock()
|
|
|
|
|
|
|
|
// search for all the routes
|
|
|
|
for _, routeList := range t.routes {
|
|
|
|
for _, r := range routeList {
|
|
|
|
// if any route is older than
|
|
|
|
if time.Since(r.updated).Seconds() > olderThan.Seconds() {
|
|
|
|
routes = append(routes, r.route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Unlock()
|
|
|
|
|
|
|
|
// delete the routes we've found
|
|
|
|
for _, route := range routes {
|
|
|
|
t.Delete(route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteService removes the entire service
|
|
|
|
func (t *table) deleteService(service, network string) {
|
|
|
|
t.Lock()
|
2020-08-09 21:39:21 +03:00
|
|
|
defer t.Unlock()
|
2020-08-07 22:53:38 +03:00
|
|
|
|
|
|
|
routes, ok := t.routes[service]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the routes for the service
|
|
|
|
for hash, rt := range routes {
|
|
|
|
// TODO: check if this causes a problem
|
|
|
|
// with * in the network if that is a thing
|
|
|
|
// or blank strings
|
|
|
|
if rt.route.Network != network {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(routes, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the map for the service if its empty
|
|
|
|
if len(routes) == 0 {
|
|
|
|
delete(t.routes, service)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// save the routes
|
|
|
|
t.routes[service] = routes
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// sendEvent sends events to all subscribed watchers
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) sendEvent(e *router.Event) {
|
2019-08-28 01:08:35 +03:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
2020-01-23 14:44:06 +03:00
|
|
|
if len(e.Id) == 0 {
|
|
|
|
e.Id = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
for _, w := range t.watchers {
|
|
|
|
select {
|
|
|
|
case w.resChan <- e:
|
|
|
|
case <-w.done:
|
2020-01-17 18:23:10 +03:00
|
|
|
// don't block forever
|
|
|
|
case <-time.After(time.Second):
|
2019-08-28 01:08:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 19:46:22 +03:00
|
|
|
// Create creates new route in the routing table
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) Create(r router.Route) error {
|
2019-07-09 17:45:42 +03:00
|
|
|
service := r.Service
|
2019-07-08 18:16:50 +03:00
|
|
|
sum := r.Hash()
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// check if there are any routes in the table for the route destination
|
2019-07-26 01:19:05 +03:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
2020-08-07 22:53:38 +03:00
|
|
|
t.routes[service] = make(map[uint64]*route)
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// add new route to the table for the route destination
|
2020-08-07 22:53:38 +03:00
|
|
|
if _, ok := t.routes[service][sum]; ok {
|
|
|
|
return router.ErrDuplicateRoute
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the route
|
|
|
|
t.routes[service][sum] = &route{r, time.Now()}
|
|
|
|
|
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router emitting %s for route: %s", router.Create, r.Address)
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
// send a route created event
|
|
|
|
go t.sendEvent(&router.Event{Type: router.Create, Timestamp: time.Now(), Route: r})
|
|
|
|
|
|
|
|
return nil
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2019-06-17 21:51:13 +03:00
|
|
|
// Delete deletes the route from the routing table
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) Delete(r router.Route) error {
|
2019-07-09 17:45:42 +03:00
|
|
|
service := r.Service
|
2019-07-08 18:16:50 +03:00
|
|
|
sum := r.Hash()
|
|
|
|
|
2019-06-13 14:09:49 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-26 01:19:05 +03:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
2020-07-27 15:22:00 +03:00
|
|
|
return router.ErrRouteNotFound
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2019-09-26 13:56:30 +03:00
|
|
|
if _, ok := t.routes[service][sum]; !ok {
|
2020-07-27 15:22:00 +03:00
|
|
|
return router.ErrRouteNotFound
|
2019-08-29 18:25:21 +03:00
|
|
|
}
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
// delete the route from the service
|
2019-09-26 13:56:30 +03:00
|
|
|
delete(t.routes[service], sum)
|
2020-08-07 22:53:38 +03:00
|
|
|
|
|
|
|
// delete the whole map if there are no routes left
|
2020-07-01 17:20:30 +03:00
|
|
|
if len(t.routes[service]) == 0 {
|
|
|
|
delete(t.routes, service)
|
|
|
|
}
|
2020-08-07 22:53:38 +03:00
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
2020-07-27 15:22:00 +03:00
|
|
|
logger.Debugf("Router emitting %s for route: %s", router.Delete, r.Address)
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
go t.sendEvent(&router.Event{Type: router.Delete, Timestamp: time.Now(), Route: r})
|
2019-09-26 13:56:30 +03:00
|
|
|
|
2019-06-13 14:09:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// Update updates routing table with the new route
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) Update(r router.Route) error {
|
2019-07-09 17:45:42 +03:00
|
|
|
service := r.Service
|
2019-07-08 18:16:50 +03:00
|
|
|
sum := r.Hash()
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// check if the route destination has any routes in the table
|
2019-07-26 01:19:05 +03:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
2020-08-07 22:53:38 +03:00
|
|
|
t.routes[service] = make(map[uint64]*route)
|
2019-08-29 18:21:30 +03:00
|
|
|
}
|
2019-07-10 23:28:32 +03:00
|
|
|
|
2019-09-26 14:45:10 +03:00
|
|
|
if _, ok := t.routes[service][sum]; !ok {
|
2020-08-07 22:53:38 +03:00
|
|
|
// update the route
|
|
|
|
t.routes[service][sum] = &route{r, time.Now()}
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
2020-07-27 15:22:00 +03:00
|
|
|
logger.Debugf("Router emitting %s for route: %s", router.Update, r.Address)
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
go t.sendEvent(&router.Event{Type: router.Update, Timestamp: time.Now(), Route: r})
|
2019-09-26 14:45:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// just update the route, but dont emit Update event
|
2020-08-07 22:53:38 +03:00
|
|
|
t.routes[service][sum] = &route{r, time.Now()}
|
2019-09-13 20:46:24 +03:00
|
|
|
|
2019-07-10 23:28:32 +03:00
|
|
|
return nil
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2020-08-23 15:10:48 +03:00
|
|
|
// 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)
|
2019-06-19 20:01:48 +03:00
|
|
|
}
|
|
|
|
|
2020-08-21 11:23:01 +03:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
2020-06-30 11:53:49 +03:00
|
|
|
|
2020-08-21 11:23:01 +03:00
|
|
|
var routes []router.Route
|
2020-08-07 22:53:38 +03:00
|
|
|
|
2020-08-23 15:10:48 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise get all routes
|
|
|
|
for _, serviceRoutes := range t.routes {
|
|
|
|
for _, rt := range serviceRoutes {
|
|
|
|
routes = append(routes, rt.route)
|
|
|
|
}
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
2020-08-07 22:53:38 +03:00
|
|
|
|
2020-08-21 11:23:01 +03:00
|
|
|
return routes, nil
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Watch returns routing table entry watcher
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) Watch(opts ...router.WatchOption) (router.Watcher, error) {
|
2019-06-13 14:09:49 +03:00
|
|
|
// by default watch everything
|
2020-07-27 15:22:00 +03:00
|
|
|
wopts := router.WatchOptions{
|
2019-07-09 17:45:42 +03:00
|
|
|
Service: "*",
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&wopts)
|
|
|
|
}
|
|
|
|
|
2019-07-26 01:19:05 +03:00
|
|
|
w := &tableWatcher{
|
2019-08-02 16:59:08 +03:00
|
|
|
id: uuid.New().String(),
|
2019-06-13 14:09:49 +03:00
|
|
|
opts: wopts,
|
2020-07-27 15:22:00 +03:00
|
|
|
resChan: make(chan *router.Event, 10),
|
2019-06-13 14:09:49 +03:00
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
2019-08-02 16:59:08 +03:00
|
|
|
// when the watcher is stopped delete it
|
|
|
|
go func() {
|
|
|
|
<-w.done
|
|
|
|
t.Lock()
|
|
|
|
delete(t.watchers, w.id)
|
|
|
|
t.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// save the watcher
|
2019-06-13 14:09:49 +03:00
|
|
|
t.Lock()
|
2019-08-02 16:59:08 +03:00
|
|
|
t.watchers[w.id] = w
|
2019-06-13 14:09:49 +03:00
|
|
|
t.Unlock()
|
|
|
|
|
2019-07-26 01:19:05 +03:00
|
|
|
return w, nil
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|