2019-08-24 09:46:55 +01:00
|
|
|
// Package broker is a tunnel broker
|
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-25 13:44:41 +03:00
|
|
|
"fmt"
|
2019-08-24 09:46:55 +01:00
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
2020-08-25 15:41:48 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2020-09-20 16:57:54 +03:00
|
|
|
"github.com/unistack-org/micro/v3/network/transport"
|
|
|
|
"github.com/unistack-org/micro/v3/network/tunnel"
|
2019-08-24 09:46:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type tunBroker struct {
|
|
|
|
opts broker.Options
|
|
|
|
tunnel tunnel.Tunnel
|
|
|
|
}
|
|
|
|
|
|
|
|
type tunSubscriber struct {
|
|
|
|
topic string
|
|
|
|
handler broker.Handler
|
|
|
|
opts broker.SubscribeOptions
|
|
|
|
|
|
|
|
closed chan bool
|
|
|
|
listener tunnel.Listener
|
|
|
|
}
|
|
|
|
|
2020-08-27 11:18:02 +03:00
|
|
|
type tunEvent struct {
|
|
|
|
topic string
|
|
|
|
message *broker.Message
|
|
|
|
}
|
|
|
|
|
2019-08-24 09:46:55 +01:00
|
|
|
// used to access tunnel from options context
|
|
|
|
type tunnelKey struct{}
|
|
|
|
type tunnelAddr struct{}
|
|
|
|
|
|
|
|
func (t *tunBroker) Init(opts ...broker.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&t.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunBroker) Options() broker.Options {
|
|
|
|
return t.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunBroker) Address() string {
|
|
|
|
return t.tunnel.Address()
|
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
func (t *tunBroker) Connect(ctx context.Context) error {
|
2020-11-03 02:02:32 +03:00
|
|
|
return t.tunnel.Connect(ctx)
|
2019-08-24 09:46:55 +01:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
func (t *tunBroker) Disconnect(ctx context.Context) error {
|
2020-11-03 02:02:32 +03:00
|
|
|
return t.tunnel.Close(ctx)
|
2019-08-24 09:46:55 +01:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
func (t *tunBroker) Publish(ctx context.Context, topic string, m *broker.Message, opts ...broker.PublishOption) error {
|
2019-08-24 09:46:55 +01:00
|
|
|
// TODO: this is probably inefficient, we might want to just maintain an open connection
|
|
|
|
// it may be easier to add broadcast to the tunnel
|
2020-11-03 02:02:32 +03:00
|
|
|
c, err := t.tunnel.Dial(ctx, topic, tunnel.DialMode(tunnel.Multicast))
|
2019-08-24 09:46:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
return c.Send(&transport.Message{
|
|
|
|
Header: m.Header,
|
|
|
|
Body: m.Body,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
func (t *tunBroker) Subscribe(ctx context.Context, topic string, h broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
2020-11-03 02:02:32 +03:00
|
|
|
l, err := t.tunnel.Listen(ctx, topic, tunnel.ListenMode(tunnel.Multicast))
|
2019-08-24 09:46:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tunSub := &tunSubscriber{
|
|
|
|
topic: topic,
|
|
|
|
handler: h,
|
2020-10-16 09:38:57 +03:00
|
|
|
opts: broker.NewSubscribeOptions(opts...),
|
2019-08-24 09:46:55 +01:00
|
|
|
closed: make(chan bool),
|
|
|
|
listener: l,
|
|
|
|
}
|
|
|
|
|
|
|
|
// start processing
|
|
|
|
go tunSub.run()
|
|
|
|
|
|
|
|
return tunSub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunBroker) String() string {
|
|
|
|
return "tunnel"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunSubscriber) run() {
|
|
|
|
for {
|
|
|
|
// accept a new connection
|
|
|
|
c, err := t.listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-t.closed:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// receive message
|
|
|
|
m := new(transport.Message)
|
|
|
|
if err := c.Recv(m); err != nil {
|
2020-09-05 02:43:16 +03:00
|
|
|
if logger.V(logger.ErrorLevel) {
|
2020-11-04 00:38:12 +03:00
|
|
|
logger.Error(err.Error())
|
2020-08-25 15:41:48 +03:00
|
|
|
}
|
2020-09-05 02:43:16 +03:00
|
|
|
if err = c.Close(); err != nil {
|
|
|
|
if logger.V(logger.ErrorLevel) {
|
2020-11-04 00:38:12 +03:00
|
|
|
logger.Error(err.Error())
|
2020-09-05 02:43:16 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-24 09:46:55 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the connection
|
|
|
|
c.Close()
|
|
|
|
|
|
|
|
// handle the message
|
2020-08-27 11:18:02 +03:00
|
|
|
go t.handler(&tunEvent{
|
|
|
|
topic: t.topic,
|
|
|
|
message: &broker.Message{
|
|
|
|
Header: m.Header,
|
|
|
|
Body: m.Body,
|
|
|
|
},
|
2019-08-24 09:46:55 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunSubscriber) Options() broker.SubscribeOptions {
|
|
|
|
return t.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunSubscriber) Topic() string {
|
|
|
|
return t.topic
|
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
func (t *tunSubscriber) Unsubscribe(ctx context.Context) error {
|
2019-08-24 09:46:55 +01:00
|
|
|
select {
|
|
|
|
case <-t.closed:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(t.closed)
|
|
|
|
return t.listener.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 11:18:02 +03:00
|
|
|
func (t *tunEvent) Topic() string {
|
|
|
|
return t.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunEvent) Message() *broker.Message {
|
|
|
|
return t.message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunEvent) Ack() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunEvent) Error() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-25 13:44:41 +03:00
|
|
|
func NewBroker(opts ...broker.Option) (broker.Broker, error) {
|
2020-10-16 09:38:57 +03:00
|
|
|
options := broker.NewOptions(opts...)
|
2020-08-25 13:44:41 +03:00
|
|
|
|
2019-08-24 09:46:55 +01:00
|
|
|
t, ok := options.Context.Value(tunnelKey{}).(tunnel.Tunnel)
|
|
|
|
if !ok {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("tunnel not set")
|
2019-08-24 09:46:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
a, ok := options.Context.Value(tunnelAddr{}).(string)
|
|
|
|
if ok {
|
|
|
|
// initialise address
|
2020-08-25 13:44:41 +03:00
|
|
|
if err := t.Init(tunnel.Address(a)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-24 09:46:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(options.Addrs) > 0 {
|
|
|
|
// initialise nodes
|
2020-08-25 13:44:41 +03:00
|
|
|
if err := t.Init(tunnel.Nodes(options.Addrs...)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-24 09:46:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &tunBroker{
|
|
|
|
opts: options,
|
|
|
|
tunnel: t,
|
2020-08-25 13:44:41 +03:00
|
|
|
}, nil
|
2019-08-24 09:46:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress sets the tunnel address
|
|
|
|
func WithAddress(a string) broker.Option {
|
|
|
|
return func(o *broker.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, tunnelAddr{}, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTunnel sets the internal tunnel
|
|
|
|
func WithTunnel(t tunnel.Tunnel) broker.Option {
|
|
|
|
return func(o *broker.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, tunnelKey{}, t)
|
|
|
|
}
|
|
|
|
}
|