2019-06-03 18:44:43 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2019-06-17 17:54:37 +08:00
|
|
|
"strings"
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2021-10-27 01:00:27 +03:00
|
|
|
"go.unistack.org/micro/v3/broker"
|
2024-04-06 22:32:12 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
2024-04-23 08:01:44 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2021-10-27 01:00:27 +03:00
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2024-04-23 08:01:44 +03:00
|
|
|
"go.unistack.org/micro/v3/options"
|
2021-10-27 01:00:27 +03:00
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
2024-04-06 22:32:12 +03:00
|
|
|
var _ server.Message = &rpcMessage{}
|
|
|
|
|
|
|
|
type rpcMessage struct {
|
|
|
|
payload interface{}
|
|
|
|
codec codec.Codec
|
|
|
|
header metadata.Metadata
|
|
|
|
topic string
|
|
|
|
contentType string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcMessage) ContentType() string {
|
|
|
|
return r.contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcMessage) Topic() string {
|
|
|
|
return r.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcMessage) Body() interface{} {
|
|
|
|
return r.payload
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcMessage) Header() metadata.Metadata {
|
|
|
|
return r.header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcMessage) Codec() codec.Codec {
|
|
|
|
return r.codec
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
type handler struct {
|
|
|
|
reqType reflect.Type
|
|
|
|
ctxType reflect.Type
|
2021-04-26 19:04:27 +03:00
|
|
|
method reflect.Value
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type subscriber struct {
|
|
|
|
topic string
|
|
|
|
rcvr reflect.Value
|
|
|
|
typ reflect.Type
|
|
|
|
subscriber interface{}
|
|
|
|
handlers []*handler
|
2021-01-29 15:13:56 +03:00
|
|
|
endpoints []*register.Endpoint
|
2019-06-03 18:44:43 +01:00
|
|
|
opts server.SubscriberOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
2020-11-03 02:15:31 +03:00
|
|
|
options := server.NewSubscriberOptions(opts...)
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2021-01-29 15:13:56 +03:00
|
|
|
var endpoints []*register.Endpoint
|
2019-06-03 18:44:43 +01:00
|
|
|
var handlers []*handler
|
|
|
|
|
|
|
|
if typ := reflect.TypeOf(sub); typ.Kind() == reflect.Func {
|
|
|
|
h := &handler{
|
|
|
|
method: reflect.ValueOf(sub),
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ.NumIn() {
|
|
|
|
case 1:
|
|
|
|
h.reqType = typ.In(0)
|
|
|
|
case 2:
|
|
|
|
h.ctxType = typ.In(0)
|
|
|
|
h.reqType = typ.In(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers = append(handlers, h)
|
|
|
|
|
2021-01-29 15:13:56 +03:00
|
|
|
endpoints = append(endpoints, ®ister.Endpoint{
|
2019-06-03 18:44:43 +01:00
|
|
|
Name: "Func",
|
2021-01-29 15:13:56 +03:00
|
|
|
Request: register.ExtractSubValue(typ),
|
2019-06-03 18:44:43 +01:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"topic": topic,
|
|
|
|
"subscriber": "true",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
hdlr := reflect.ValueOf(sub)
|
|
|
|
name := reflect.Indirect(hdlr).Type().Name()
|
|
|
|
|
|
|
|
for m := 0; m < typ.NumMethod(); m++ {
|
|
|
|
method := typ.Method(m)
|
|
|
|
h := &handler{
|
|
|
|
method: method.Func,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch method.Type.NumIn() {
|
|
|
|
case 2:
|
|
|
|
h.reqType = method.Type.In(1)
|
|
|
|
case 3:
|
|
|
|
h.ctxType = method.Type.In(1)
|
|
|
|
h.reqType = method.Type.In(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers = append(handlers, h)
|
|
|
|
|
2021-01-29 15:13:56 +03:00
|
|
|
endpoints = append(endpoints, ®ister.Endpoint{
|
2019-06-03 18:44:43 +01:00
|
|
|
Name: name + "." + method.Name,
|
2021-01-29 15:13:56 +03:00
|
|
|
Request: register.ExtractSubValue(method.Type),
|
2019-06-03 18:44:43 +01:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"topic": topic,
|
|
|
|
"subscriber": "true",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &subscriber{
|
|
|
|
rcvr: reflect.ValueOf(sub),
|
|
|
|
typ: reflect.TypeOf(sub),
|
|
|
|
topic: topic,
|
|
|
|
subscriber: sub,
|
|
|
|
handlers: handlers,
|
|
|
|
endpoints: endpoints,
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 13:08:06 +03:00
|
|
|
func (g *Server) createSubHandler(sb *subscriber, opts server.Options) broker.Handler {
|
2020-09-20 16:08:45 +03:00
|
|
|
return func(p broker.Event) (err error) {
|
|
|
|
msg := p.Message()
|
2020-03-05 14:55:46 +00:00
|
|
|
// if we don't have headers, create empty map
|
2020-03-05 17:45:31 +03:00
|
|
|
if msg.Header == nil {
|
|
|
|
msg.Header = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
ct := msg.Header["Content-Type"]
|
2019-06-17 17:54:37 +08:00
|
|
|
if len(ct) == 0 {
|
2022-03-21 13:03:48 +03:00
|
|
|
msg.Header["Content-Type"] = DefaultContentType
|
|
|
|
ct = DefaultContentType
|
2019-06-17 17:54:37 +08:00
|
|
|
}
|
2020-11-26 01:17:21 +03:00
|
|
|
cf, err := g.newCodec(ct)
|
2019-06-03 18:44:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:15:20 +03:00
|
|
|
hdr := make(map[string]string, len(msg.Header))
|
2019-06-03 18:44:43 +01:00
|
|
|
for k, v := range msg.Header {
|
|
|
|
hdr[k] = v
|
|
|
|
}
|
2021-02-12 20:36:25 +03:00
|
|
|
|
|
|
|
ctx := metadata.NewIncomingContext(sb.opts.Context, hdr)
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2019-06-17 17:54:37 +08:00
|
|
|
results := make(chan error, len(sb.handlers))
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
for i := 0; i < len(sb.handlers); i++ {
|
|
|
|
handler := sb.handlers[i]
|
|
|
|
|
|
|
|
var isVal bool
|
|
|
|
var req reflect.Value
|
|
|
|
|
|
|
|
if handler.reqType.Kind() == reflect.Ptr {
|
|
|
|
req = reflect.New(handler.reqType.Elem())
|
|
|
|
} else {
|
|
|
|
req = reflect.New(handler.reqType)
|
|
|
|
isVal = true
|
|
|
|
}
|
|
|
|
if isVal {
|
|
|
|
req = req.Elem()
|
|
|
|
}
|
|
|
|
|
2019-11-25 22:50:59 +03:00
|
|
|
if err = cf.Unmarshal(msg.Body, req.Interface()); err != nil {
|
2019-06-03 18:44:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := func(ctx context.Context, msg server.Message) error {
|
|
|
|
var vals []reflect.Value
|
|
|
|
if sb.typ.Kind() != reflect.Func {
|
|
|
|
vals = append(vals, sb.rcvr)
|
|
|
|
}
|
|
|
|
if handler.ctxType != nil {
|
|
|
|
vals = append(vals, reflect.ValueOf(ctx))
|
|
|
|
}
|
|
|
|
|
2022-03-21 15:28:39 +03:00
|
|
|
vals = append(vals, reflect.ValueOf(msg.Body()))
|
2019-06-03 18:44:43 +01:00
|
|
|
|
|
|
|
returnValues := handler.method.Call(vals)
|
2019-11-25 22:50:59 +03:00
|
|
|
if rerr := returnValues[0].Interface(); rerr != nil {
|
|
|
|
return rerr.(error)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-23 08:01:44 +03:00
|
|
|
opts.Hooks.EachNext(func(hook options.Hook) {
|
|
|
|
if h, ok := hook.(server.HookSubHandler); ok {
|
|
|
|
fn = h(fn)
|
|
|
|
}
|
|
|
|
})
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2019-06-17 17:07:55 +08:00
|
|
|
if g.wg != nil {
|
|
|
|
g.wg.Add(1)
|
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
go func() {
|
2019-06-17 17:07:55 +08:00
|
|
|
if g.wg != nil {
|
|
|
|
defer g.wg.Done()
|
|
|
|
}
|
2021-04-26 19:04:27 +03:00
|
|
|
cerr := fn(ctx, &rpcMessage{
|
2019-06-03 18:44:43 +01:00
|
|
|
topic: sb.topic,
|
|
|
|
contentType: ct,
|
|
|
|
payload: req.Interface(),
|
2019-11-25 16:31:43 +00:00
|
|
|
header: msg.Header,
|
2019-06-03 18:44:43 +01:00
|
|
|
})
|
2021-04-26 19:04:27 +03:00
|
|
|
results <- cerr
|
2019-06-03 18:44:43 +01:00
|
|
|
}()
|
|
|
|
}
|
2019-06-17 17:54:37 +08:00
|
|
|
var errors []string
|
|
|
|
for i := 0; i < len(sb.handlers); i++ {
|
2020-02-26 21:34:40 +03:00
|
|
|
if rerr := <-results; rerr != nil {
|
2019-11-25 22:50:59 +03:00
|
|
|
errors = append(errors, rerr.Error())
|
2019-06-17 17:54:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errors) > 0 {
|
2019-11-25 22:50:59 +03:00
|
|
|
err = fmt.Errorf("subscriber error: %s", strings.Join(errors, "\n"))
|
2019-06-17 17:54:37 +08:00
|
|
|
}
|
2019-11-25 22:50:59 +03:00
|
|
|
|
|
|
|
return err
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Topic() string {
|
|
|
|
return s.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Subscriber() interface{} {
|
|
|
|
return s.subscriber
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:13:56 +03:00
|
|
|
func (s *subscriber) Endpoints() []*register.Endpoint {
|
2019-06-03 18:44:43 +01:00
|
|
|
return s.endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Options() server.SubscriberOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|
2024-04-23 08:01:44 +03:00
|
|
|
|
|
|
|
func (g *Server) subscribe() error {
|
|
|
|
config := g.opts
|
|
|
|
subCtx := config.Context
|
|
|
|
|
|
|
|
for sb := range g.subscribers {
|
|
|
|
|
|
|
|
if cx := sb.Options().Context; cx != nil {
|
|
|
|
subCtx = cx
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := []broker.SubscribeOption{
|
|
|
|
broker.SubscribeContext(subCtx),
|
|
|
|
broker.SubscribeAutoAck(sb.Options().AutoAck),
|
|
|
|
broker.SubscribeBodyOnly(sb.Options().BodyOnly),
|
|
|
|
}
|
|
|
|
|
|
|
|
if queue := sb.Options().Queue; len(queue) > 0 {
|
|
|
|
opts = append(opts, broker.SubscribeGroup(queue))
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
|
|
|
config.Logger.Info(config.Context, "subscribing to topic: "+sb.Topic())
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := config.Broker.Subscribe(subCtx, sb.Topic(), g.createSubHandler(sb, config), opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
g.subscribers[sb] = []broker.Subscriber{sub}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|