From b2d6c165c52537a9865aa526b25e574f20c08029 Mon Sep 17 00:00:00 2001 From: Felipe Martinez Date: Sun, 29 Nov 2020 15:58:26 +0100 Subject: [PATCH] Add discovery interface --- broker/broker.go | 1 - client/client.go | 2 ++ discovery/discovery.go | 7 +++++++ options/options.go | 16 ++-------------- service.go | 29 ++++++++++++++++++++++++----- 5 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 discovery/discovery.go diff --git a/broker/broker.go b/broker/broker.go index 6fdd921..dcdf4c9 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -3,7 +3,6 @@ package broker import "context" type Broker interface { - Connect(ctx context.Context) error Close() error Publish(ctx context.Context, topic string, data *Message) error Subscribe(ctx context.Context, topic string, callback func(*Message)) error diff --git a/client/client.go b/client/client.go index 1f4afaa..6daaff0 100644 --- a/client/client.go +++ b/client/client.go @@ -8,6 +8,7 @@ import ( "github.com/MouseHatGames/mice/broker" "github.com/MouseHatGames/mice/codec" + "github.com/MouseHatGames/mice/discovery" "github.com/MouseHatGames/mice/logger" "github.com/MouseHatGames/mice/options" "github.com/MouseHatGames/mice/transport" @@ -27,6 +28,7 @@ type client struct { trans transport.Transport broker broker.Broker log logger.Logger + disc discovery.Discovery } type CallError struct { diff --git a/discovery/discovery.go b/discovery/discovery.go new file mode 100644 index 0000000..31e860f --- /dev/null +++ b/discovery/discovery.go @@ -0,0 +1,7 @@ +package discovery + +import "net" + +type Discovery interface { + Find(svc string) (net.IP, error) +} diff --git a/options/options.go b/options/options.go index 44cfda5..5060fe1 100644 --- a/options/options.go +++ b/options/options.go @@ -4,6 +4,7 @@ import ( "github.com/MouseHatGames/mice/broker" "github.com/MouseHatGames/mice/codec" "github.com/MouseHatGames/mice/config" + "github.com/MouseHatGames/mice/discovery" "github.com/MouseHatGames/mice/logger" "github.com/MouseHatGames/mice/router" "github.com/MouseHatGames/mice/transport" @@ -20,6 +21,7 @@ type Options struct { Router router.Router Broker broker.Broker Config config.Config + Discovery discovery.Discovery } // Option represents a function that can be used to mutate an Options object @@ -45,17 +47,3 @@ func Logger(l logger.Logger) Option { o.Logger = l } } - -// Codec sets the codec that will transform the messages -func Codec(c codec.Codec) Option { - return func(o *Options) { - o.Codec = c - } -} - -// Transport sets the transport that will deliver and receive messages to and from other services -func Transport(t transport.Transport) Option { - return func(o *Options) { - o.Transport = t - } -} diff --git a/service.go b/service.go index 77330f3..d51120e 100644 --- a/service.go +++ b/service.go @@ -1,7 +1,6 @@ package mice import ( - "context" "errors" "fmt" @@ -26,6 +25,10 @@ type Service interface { Start() error } +type starter interface { + Start() error +} + type service struct { options options.Options server server.Server @@ -75,15 +78,31 @@ func (s *service) Start() error { return errors.New("missing service name") } - if s.options.Broker != nil { - if err := s.options.Broker.Connect(context.Background()); err != nil { - return fmt.Errorf("broker connect: %w", err) - } + if err := tryStart(map[string]interface{}{ + "broker": s.options.Broker, + "codec": s.options.Codec, + "config": s.options.Config, + "discovery": s.options.Discovery, + "logger": s.options.Logger, + "transport": s.options.Transport, + }); err != nil { + return err } return s.server.Start() } +func tryStart(objs map[string]interface{}) error { + for k, v := range objs { + if s, ok := v.(starter); ok { + if err := s.Start(); err != nil { + return fmt.Errorf("start %s: %w", k, err) + } + } + } + return nil +} + func (s *service) Server() server.Server { return s.server }