router: update interface (#1732)

* router: replace Start and Stop with Close

* router: update default network to micro

* router: update tests
This commit is contained in:
ben-toogood 2020-06-24 11:09:16 +01:00 committed by GitHub
parent 695cc9d526
commit c940961574
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 33 additions and 42 deletions

View File

@ -184,7 +184,7 @@ func (r *routerSelector) Reset(service string) {
func (r *routerSelector) Close() error { func (r *routerSelector) Close() error {
// stop the router advertisements // stop the router advertisements
return r.r.Stop() return r.r.Close()
} }
func (r *routerSelector) String() string { func (r *routerSelector) String() string {

2
go.mod
View File

@ -68,7 +68,7 @@ require (
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1 google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1
google.golang.org/grpc v1.26.0 google.golang.org/grpc v1.26.0
google.golang.org/protobuf v1.22.0 google.golang.org/protobuf v1.22.0 // indirect
gopkg.in/telegram-bot-api.v4 v4.6.4 gopkg.in/telegram-bot-api.v4 v4.6.4
sigs.k8s.io/yaml v1.1.0 // indirect sigs.k8s.io/yaml v1.1.0 // indirect
) )

1
go.sum
View File

@ -223,6 +223,7 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51
github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg= github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg=
github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ= github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=

View File

@ -1746,11 +1746,6 @@ func (n *network) Connect() error {
// create closed channel // create closed channel
n.closed = make(chan bool) n.closed = make(chan bool)
// start the router
if err := n.options.Router.Start(); err != nil {
return err
}
// start advertising routes // start advertising routes
advertChan, err := n.options.Router.Advertise() advertChan, err := n.options.Router.Advertise()
if err != nil { if err != nil {
@ -1785,8 +1780,8 @@ func (n *network) close() error {
return err return err
} }
// stop the router // close the router
if err := n.router.Stop(); err != nil { if err := n.router.Close(); err != nil {
return err return err
} }

View File

@ -45,15 +45,25 @@ func newRouter(opts ...Option) Router {
o(&options) o(&options)
} }
return &router{ // construct the router
r := &router{
options: options, options: options,
table: newTable(), table: newTable(),
subscribers: make(map[string]chan *Advert), subscribers: make(map[string]chan *Advert),
} }
// start the router and return
r.start()
return r
} }
// Init initializes router with given options // Init initializes router with given options
func (r *router) Init(opts ...Option) error { func (r *router) Init(opts ...Option) error {
// stop the router before we initialize
if err := r.Close(); err != nil {
return err
}
r.Lock() r.Lock()
defer r.Unlock() defer r.Unlock()
@ -61,6 +71,11 @@ func (r *router) Init(opts ...Option) error {
o(&r.options) o(&r.options)
} }
// restart the router
if err := r.start(); err != nil {
return err
}
return nil return nil
} }
@ -399,11 +414,8 @@ func (r *router) drain() {
} }
} }
// Start starts the router // start the router. Should be called under lock.
func (r *router) Start() error { func (r *router) start() error {
r.Lock()
defer r.Unlock()
if r.running { if r.running {
return nil return nil
} }
@ -615,8 +627,8 @@ func (r *router) Watch(opts ...WatchOption) (Watcher, error) {
return r.table.Watch(opts...) return r.table.Watch(opts...)
} }
// Stop stops the router // Close the router
func (r *router) Stop() error { func (r *router) Close() error {
r.Lock() r.Lock()
defer r.Unlock() defer r.Unlock()

View File

@ -15,19 +15,15 @@ func routerTestSetup() Router {
return newRouter(Registry(r)) return newRouter(Registry(r))
} }
func TestRouterStartStop(t *testing.T) { func TestRouterClose(t *testing.T) {
r := routerTestSetup() r := routerTestSetup()
if err := r.Start(); err != nil {
t.Errorf("failed to start router: %v", err)
}
_, err := r.Advertise() _, err := r.Advertise()
if err != nil { if err != nil {
t.Errorf("failed to start advertising: %v", err) t.Errorf("failed to start advertising: %v", err)
} }
if err := r.Stop(); err != nil { if err := r.Close(); err != nil {
t.Errorf("failed to stop router: %v", err) t.Errorf("failed to stop router: %v", err)
} }
if len(os.Getenv("IN_TRAVIS_CI")) == 0 { if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
@ -41,10 +37,6 @@ func TestRouterAdvertise(t *testing.T) {
// lower the advertise interval // lower the advertise interval
AdvertiseEventsTick = 500 * time.Millisecond AdvertiseEventsTick = 500 * time.Millisecond
if err := r.Start(); err != nil {
t.Errorf("failed to start router: %v", err)
}
ch, err := r.Advertise() ch, err := r.Advertise()
if err != nil { if err != nil {
t.Errorf("failed to start advertising: %v", err) t.Errorf("failed to start advertising: %v", err)
@ -134,7 +126,7 @@ func TestRouterAdvertise(t *testing.T) {
wg.Wait() wg.Wait()
if err := r.Stop(); err != nil { if err := r.Close(); err != nil {
t.Errorf("failed to stop router: %v", err) t.Errorf("failed to stop router: %v", err)
} }
} }

View File

@ -11,7 +11,7 @@ var (
// DefaultName is default router service name // DefaultName is default router service name
DefaultName = "go.micro.router" DefaultName = "go.micro.router"
// DefaultNetwork is default micro network // DefaultNetwork is default micro network
DefaultNetwork = "go.micro" DefaultNetwork = "micro"
// DefaultRouter is default network router // DefaultRouter is default network router
DefaultRouter = NewRouter() DefaultRouter = NewRouter()
) )
@ -32,10 +32,8 @@ type Router interface {
Lookup(...QueryOption) ([]Route, error) Lookup(...QueryOption) ([]Route, error)
// Watch returns a watcher which tracks updates to the routing table // Watch returns a watcher which tracks updates to the routing table
Watch(opts ...WatchOption) (Watcher, error) Watch(opts ...WatchOption) (Watcher, error)
// Start starts the router // Close the router
Start() error Close() error
// Stop stops the router
Stop() error
// Returns the router implementation // Returns the router implementation
String() string String() string
} }

View File

@ -85,13 +85,6 @@ func (s *svc) Table() router.Table {
return s.table return s.table
} }
// Start starts the service
func (s *svc) Start() error {
s.Lock()
defer s.Unlock()
return nil
}
func (s *svc) advertiseEvents(advertChan chan *router.Advert, stream pb.Router_AdvertiseService) error { func (s *svc) advertiseEvents(advertChan chan *router.Advert, stream pb.Router_AdvertiseService) error {
go func() { go func() {
<-s.exit <-s.exit
@ -202,8 +195,8 @@ func (s *svc) Process(advert *router.Advert) error {
return nil return nil
} }
// Remote router cannot be stopped // Remote router cannot be closed
func (s *svc) Stop() error { func (s *svc) Close() error {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()