Skip to content

Commit

Permalink
Add discovery interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Nov 29, 2020
1 parent 66c515f commit b2d6c16
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
1 change: 0 additions & 1 deletion broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,6 +28,7 @@ type client struct {
trans transport.Transport
broker broker.Broker
log logger.Logger
disc discovery.Discovery
}

type CallError struct {
Expand Down
7 changes: 7 additions & 0 deletions discovery/discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package discovery

import "net"

type Discovery interface {
Find(svc string) (net.IP, error)
}
16 changes: 2 additions & 14 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
}
}
29 changes: 24 additions & 5 deletions service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mice

import (
"context"
"errors"
"fmt"

Expand All @@ -26,6 +25,10 @@ type Service interface {
Start() error
}

type starter interface {
Start() error
}

type service struct {
options options.Options
server server.Server
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b2d6c16

Please sign in to comment.