2019-11-11 08:23:21 +00:00
|
|
|
// Package service provides the broker service client
|
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2021-01-19 19:41:39 +03:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-03-22 08:42:38 +03:00
|
|
|
pbmicro "github.com/unistack-org/micro-broker-service/v3/micro"
|
2021-01-19 19:41:39 +03:00
|
|
|
pb "github.com/unistack-org/micro-broker-service/v3/proto"
|
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2019-11-11 08:23:21 +00:00
|
|
|
)
|
|
|
|
|
2021-01-19 19:41:39 +03:00
|
|
|
type serviceBroker struct {
|
|
|
|
addrs []string
|
|
|
|
service string
|
2021-03-22 08:42:38 +03:00
|
|
|
client pbmicro.BrokerClient
|
2021-03-24 23:23:53 +03:00
|
|
|
init bool
|
2021-01-19 19:41:39 +03:00
|
|
|
opts broker.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *serviceBroker) Address() string {
|
|
|
|
return b.addrs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *serviceBroker) Connect(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *serviceBroker) Disconnect(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *serviceBroker) Init(opts ...broker.Option) error {
|
2021-03-24 23:23:53 +03:00
|
|
|
if len(opts) == 0 && b.init {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-19 19:41:39 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&b.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cli client.Client
|
|
|
|
if b.opts.Context != nil {
|
|
|
|
if v, ok := b.opts.Context.Value(clientKey{}).(client.Client); ok && v != nil {
|
|
|
|
cli = v
|
|
|
|
}
|
|
|
|
if v, ok := b.opts.Context.Value(serviceKey{}).(string); ok && v != "" {
|
|
|
|
b.service = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 23:23:53 +03:00
|
|
|
if err := b.opts.Register.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.opts.Tracer.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.opts.Logger.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.opts.Meter.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-19 19:41:39 +03:00
|
|
|
if b.service == "" {
|
|
|
|
return fmt.Errorf("missing Service option")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cli == nil {
|
|
|
|
return fmt.Errorf("missing Client option")
|
|
|
|
}
|
|
|
|
|
2021-03-22 08:42:38 +03:00
|
|
|
b.client = pbmicro.NewBrokerClient(b.service, cli)
|
2021-01-19 19:41:39 +03:00
|
|
|
|
2021-03-24 23:26:02 +03:00
|
|
|
b.init = true
|
2021-01-19 19:41:39 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *serviceBroker) Options() broker.Options {
|
|
|
|
return b.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *serviceBroker) Publish(ctx context.Context, topic string, msg *broker.Message, opts ...broker.PublishOption) error {
|
|
|
|
if logger.V(logger.TraceLevel) {
|
|
|
|
logger.Tracef(ctx, "Publishing to topic %s broker %v", topic, b.addrs)
|
|
|
|
}
|
|
|
|
_, err := b.client.Publish(ctx, &pb.PublishRequest{
|
|
|
|
Topic: topic,
|
|
|
|
Message: &pb.Message{
|
|
|
|
Header: msg.Header,
|
|
|
|
Body: msg.Body,
|
|
|
|
},
|
|
|
|
}, client.WithAddress(b.addrs...))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *serviceBroker) Subscribe(ctx context.Context, topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
|
|
|
options := broker.NewSubscribeOptions(opts...)
|
|
|
|
if logger.V(logger.TraceLevel) {
|
|
|
|
logger.Tracef(ctx, "Subscribing to topic %s group %s broker %v", topic, options.Group, b.addrs)
|
|
|
|
}
|
|
|
|
stream, err := b.client.Subscribe(ctx, &pb.SubscribeRequest{
|
|
|
|
Topic: topic,
|
|
|
|
Group: options.Group,
|
|
|
|
}, client.WithAddress(b.addrs...), client.WithRequestTimeout(time.Hour))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sub := &serviceSub{
|
|
|
|
topic: topic,
|
|
|
|
group: options.Group,
|
|
|
|
handler: handler,
|
|
|
|
stream: stream,
|
|
|
|
closed: make(chan bool),
|
|
|
|
options: options,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sub.closed:
|
|
|
|
if logger.V(logger.TraceLevel) {
|
|
|
|
logger.Tracef(ctx, "Unsubscribed from topic %s", topic)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if logger.V(logger.TraceLevel) {
|
|
|
|
// run the subscriber
|
|
|
|
logger.Tracef(ctx, "Streaming from broker %v to topic [%s] group [%s]", b.addrs, topic, options.Group)
|
|
|
|
}
|
|
|
|
if err := sub.run(ctx); err != nil {
|
|
|
|
if logger.V(logger.TraceLevel) {
|
|
|
|
logger.Tracef(ctx, "Resubscribing to topic %s broker %v", topic, b.addrs)
|
|
|
|
}
|
|
|
|
stream, err := b.client.Subscribe(ctx, &pb.SubscribeRequest{
|
|
|
|
Topic: topic,
|
|
|
|
Group: options.Group,
|
|
|
|
}, client.WithAddress(b.addrs...), client.WithRequestTimeout(time.Hour))
|
|
|
|
if err != nil {
|
|
|
|
if logger.V(logger.TraceLevel) {
|
|
|
|
logger.Tracef(ctx, "Failed to resubscribe to topic %s: %v", topic, err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// new stream
|
|
|
|
sub.stream = stream
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return sub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *serviceBroker) String() string {
|
|
|
|
return "service"
|
2019-11-11 08:23:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 23:01:34 +03:00
|
|
|
func (b *serviceBroker) Name() string {
|
|
|
|
return b.opts.Name
|
|
|
|
}
|
|
|
|
|
2019-11-11 08:23:21 +00:00
|
|
|
func NewBroker(opts ...broker.Option) broker.Broker {
|
2021-01-19 19:41:39 +03:00
|
|
|
options := broker.NewOptions(opts...)
|
|
|
|
|
|
|
|
addrs := options.Addrs
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
addrs = []string{"127.0.0.1:8001"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &serviceBroker{
|
|
|
|
addrs: addrs,
|
|
|
|
opts: options,
|
|
|
|
}
|
2019-11-11 08:23:21 +00:00
|
|
|
}
|