2020-10-09 16:15:36 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-10-16 09:38:57 +03:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-10-09 16:15:36 +03:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2020-10-16 09:38:57 +03:00
|
|
|
"runtime/debug"
|
|
|
|
"strings"
|
2020-10-09 16:15:36 +03:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2020-10-16 09:38:57 +03:00
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/broker"
|
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/errors"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/metadata"
|
|
|
|
"go.unistack.org/micro/v4/register"
|
2020-10-09 16:15:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-07-27 23:58:06 +03:00
|
|
|
subSig = "func(context.Context, interface{}) error"
|
|
|
|
batchSubSig = "func([]context.Context, []interface{}) error"
|
2020-10-09 16:15:36 +03:00
|
|
|
)
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
// Precompute the reflect type for error. Can't use error directly
|
|
|
|
// because Typeof takes an empty interface value. This is annoying.
|
|
|
|
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
|
2020-10-09 16:15:36 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
type handler struct {
|
|
|
|
reqType reflect.Type
|
|
|
|
ctxType reflect.Type
|
2021-03-06 19:45:13 +03:00
|
|
|
method reflect.Value
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type subscriber struct {
|
|
|
|
typ reflect.Type
|
|
|
|
subscriber interface{}
|
2021-03-06 19:45:13 +03:00
|
|
|
topic string
|
2021-01-29 13:17:32 +03:00
|
|
|
endpoints []*register.Endpoint
|
2021-09-30 01:24:16 +03:00
|
|
|
handlers []*handler
|
|
|
|
opts SubscriberOptions
|
2022-03-30 15:37:02 +03:00
|
|
|
rcvr reflect.Value
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
|
2020-10-09 16:15:36 +03:00
|
|
|
// Is this an exported - upper case - name?
|
|
|
|
func isExported(name string) bool {
|
|
|
|
rune, _ := utf8.DecodeRuneInString(name)
|
|
|
|
return unicode.IsUpper(rune)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this type exported or a builtin?
|
|
|
|
func isExportedOrBuiltinType(t reflect.Type) bool {
|
|
|
|
for t.Kind() == reflect.Ptr {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
|
|
|
// PkgPath will be non-empty even for an exported type,
|
|
|
|
// so we need to check the type name as well.
|
|
|
|
return isExported(t.Name()) || t.PkgPath() == ""
|
|
|
|
}
|
|
|
|
|
2021-07-27 23:58:06 +03:00
|
|
|
// ValidateSubscriber func signature
|
2020-10-09 16:15:36 +03:00
|
|
|
func ValidateSubscriber(sub Subscriber) error {
|
|
|
|
typ := reflect.TypeOf(sub.Subscriber())
|
|
|
|
var argType reflect.Type
|
2021-04-27 08:32:47 +03:00
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Func:
|
2020-10-09 16:15:36 +03:00
|
|
|
name := "Func"
|
|
|
|
switch typ.NumIn() {
|
|
|
|
case 2:
|
|
|
|
argType = typ.In(1)
|
2021-07-27 23:58:06 +03:00
|
|
|
if sub.Options().Batch {
|
|
|
|
if argType.Kind() != reflect.Slice {
|
|
|
|
return fmt.Errorf("subscriber %v dont have required signature %s", name, batchSubSig)
|
|
|
|
}
|
2021-09-28 23:43:43 +03:00
|
|
|
if strings.Compare(fmt.Sprintf("%v", argType), "[]interface{}") == 0 {
|
2021-07-27 23:58:06 +03:00
|
|
|
return fmt.Errorf("subscriber %v dont have required signaure %s", name, batchSubSig)
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 16:15:36 +03:00
|
|
|
default:
|
2021-07-27 23:58:06 +03:00
|
|
|
return fmt.Errorf("subscriber %v takes wrong number of args: %v required signature %s or %s", name, typ.NumIn(), subSig, batchSubSig)
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
if !isExportedOrBuiltinType(argType) {
|
|
|
|
return fmt.Errorf("subscriber %v argument type not exported: %v", name, argType)
|
|
|
|
}
|
|
|
|
if typ.NumOut() != 1 {
|
2021-07-27 23:58:06 +03:00
|
|
|
return fmt.Errorf("subscriber %v has wrong number of return values: %v require signature %s or %s",
|
|
|
|
name, typ.NumOut(), subSig, batchSubSig)
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
if returnType := typ.Out(0); returnType != typeOfError {
|
|
|
|
return fmt.Errorf("subscriber %v returns %v not error", name, returnType.String())
|
|
|
|
}
|
2021-04-27 08:32:47 +03:00
|
|
|
default:
|
2020-10-09 16:15:36 +03:00
|
|
|
hdlr := reflect.ValueOf(sub.Subscriber())
|
|
|
|
name := reflect.Indirect(hdlr).Type().Name()
|
|
|
|
|
|
|
|
for m := 0; m < typ.NumMethod(); m++ {
|
|
|
|
method := typ.Method(m)
|
|
|
|
switch method.Type.NumIn() {
|
|
|
|
case 3:
|
|
|
|
argType = method.Type.In(2)
|
|
|
|
default:
|
2021-07-27 23:58:06 +03:00
|
|
|
return fmt.Errorf("subscriber %v.%v takes wrong number of args: %v required signature %s or %s",
|
|
|
|
name, method.Name, method.Type.NumIn(), subSig, batchSubSig)
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !isExportedOrBuiltinType(argType) {
|
|
|
|
return fmt.Errorf("%v argument type not exported: %v", name, argType)
|
|
|
|
}
|
|
|
|
if method.Type.NumOut() != 1 {
|
|
|
|
return fmt.Errorf(
|
2021-07-27 23:58:06 +03:00
|
|
|
"subscriber %v.%v has wrong number of return values: %v require signature %s or %s",
|
|
|
|
name, method.Name, method.Type.NumOut(), subSig, batchSubSig)
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
if returnType := method.Type.Out(0); returnType != typeOfError {
|
|
|
|
return fmt.Errorf("subscriber %v.%v returns %v not error", name, method.Name, returnType.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-16 09:38:57 +03:00
|
|
|
|
|
|
|
func newSubscriber(topic string, sub interface{}, opts ...SubscriberOption) Subscriber {
|
2021-01-29 13:17:32 +03:00
|
|
|
var endpoints []*register.Endpoint
|
2020-10-16 09:38:57 +03:00
|
|
|
var handlers []*handler
|
|
|
|
|
|
|
|
options := NewSubscriberOptions(opts...)
|
|
|
|
|
|
|
|
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 13:17:32 +03:00
|
|
|
ep := ®ister.Endpoint{
|
2020-11-18 16:50:41 +03:00
|
|
|
Name: "Func",
|
2021-01-29 13:17:32 +03:00
|
|
|
Request: register.ExtractSubValue(typ),
|
2020-11-18 16:50:41 +03:00
|
|
|
Metadata: metadata.New(2),
|
|
|
|
}
|
|
|
|
ep.Metadata.Set("topic", topic)
|
|
|
|
ep.Metadata.Set("subscriber", "true")
|
|
|
|
endpoints = append(endpoints, ep)
|
2020-10-16 09:38:57 +03:00
|
|
|
} 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 13:17:32 +03:00
|
|
|
ep := ®ister.Endpoint{
|
2020-11-18 16:50:41 +03:00
|
|
|
Name: name + "." + method.Name,
|
2021-01-29 13:17:32 +03:00
|
|
|
Request: register.ExtractSubValue(method.Type),
|
2020-11-18 16:50:41 +03:00
|
|
|
Metadata: metadata.New(2),
|
|
|
|
}
|
|
|
|
ep.Metadata.Set("topic", topic)
|
|
|
|
ep.Metadata.Set("subscriber", "true")
|
|
|
|
endpoints = append(endpoints, ep)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &subscriber{
|
|
|
|
rcvr: reflect.ValueOf(sub),
|
|
|
|
typ: reflect.TypeOf(sub),
|
|
|
|
topic: topic,
|
|
|
|
subscriber: sub,
|
|
|
|
handlers: handlers,
|
|
|
|
endpoints: endpoints,
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
//nolint:gocyclo
|
2021-07-27 23:58:06 +03:00
|
|
|
func (n *noopServer) newBatchSubHandler(sb *subscriber, opts Options) broker.BatchHandler {
|
|
|
|
return func(ps broker.Events) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
n.RLock()
|
|
|
|
config := n.opts
|
|
|
|
n.RUnlock()
|
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
|
|
|
config.Logger.Error(n.opts.Context, "panic recovered: ", r)
|
|
|
|
config.Logger.Error(n.opts.Context, string(debug.Stack()))
|
|
|
|
}
|
|
|
|
err = errors.InternalServerError(n.opts.Name+".subscriber", "panic recovered: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
msgs := make([]Message, 0, len(ps))
|
|
|
|
ctxs := make([]context.Context, 0, len(ps))
|
|
|
|
for _, p := range ps {
|
|
|
|
msg := p.Message()
|
|
|
|
// if we don't have headers, create empty map
|
|
|
|
if msg.Header == nil {
|
|
|
|
msg.Header = metadata.New(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
ct, _ := msg.Header.Get(metadata.HeaderContentType)
|
|
|
|
if len(ct) == 0 {
|
|
|
|
msg.Header.Set(metadata.HeaderContentType, defaultContentType)
|
|
|
|
ct = defaultContentType
|
|
|
|
}
|
|
|
|
hdr := metadata.Copy(msg.Header)
|
|
|
|
topic, _ := msg.Header.Get(metadata.HeaderTopic)
|
|
|
|
ctxs = append(ctxs, metadata.NewIncomingContext(sb.opts.Context, hdr))
|
|
|
|
msgs = append(msgs, &rpcMessage{
|
|
|
|
topic: topic,
|
|
|
|
contentType: ct,
|
|
|
|
header: msg.Header,
|
|
|
|
body: msg.Body,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
results := make(chan error, len(sb.handlers))
|
|
|
|
|
|
|
|
for i := 0; i < len(sb.handlers); i++ {
|
|
|
|
handler := sb.handlers[i]
|
|
|
|
|
|
|
|
var req reflect.Value
|
|
|
|
|
|
|
|
switch handler.reqType.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
req = reflect.New(handler.reqType.Elem())
|
|
|
|
default:
|
|
|
|
req = reflect.New(handler.reqType.Elem()).Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
reqType := handler.reqType
|
2021-09-28 23:43:43 +03:00
|
|
|
var cf codec.Codec
|
2021-07-27 23:58:06 +03:00
|
|
|
for _, msg := range msgs {
|
2021-09-28 23:43:43 +03:00
|
|
|
cf, err = n.newCodec(msg.ContentType())
|
2021-07-27 23:58:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rb := reflect.New(req.Type().Elem())
|
2022-03-21 15:23:41 +03:00
|
|
|
if err = cf.ReadBody(bytes.NewReader(msg.(*rpcMessage).body), rb.Interface()); err != nil {
|
2021-07-27 23:58:06 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg.(*rpcMessage).codec = cf
|
|
|
|
msg.(*rpcMessage).payload = rb.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := func(ctxs []context.Context, ms []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(ctxs))
|
|
|
|
}
|
|
|
|
payloads := reflect.MakeSlice(reqType, 0, len(ms))
|
|
|
|
for _, m := range ms {
|
2022-03-21 15:23:41 +03:00
|
|
|
payloads = reflect.Append(payloads, reflect.ValueOf(m.Body()))
|
2021-07-27 23:58:06 +03:00
|
|
|
}
|
|
|
|
vals = append(vals, payloads)
|
|
|
|
|
|
|
|
returnValues := handler.method.Call(vals)
|
|
|
|
if rerr := returnValues[0].Interface(); rerr != nil {
|
|
|
|
return rerr.(error)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(opts.BatchSubWrappers); i > 0; i-- {
|
|
|
|
fn = opts.BatchSubWrappers[i-1](fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.wg != nil {
|
|
|
|
n.wg.Add(1)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
if n.wg != nil {
|
|
|
|
defer n.wg.Done()
|
|
|
|
}
|
|
|
|
results <- fn(ctxs, msgs)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
var errors []string
|
|
|
|
for i := 0; i < len(sb.handlers); i++ {
|
|
|
|
if rerr := <-results; rerr != nil {
|
|
|
|
errors = append(errors, rerr.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errors) > 0 {
|
|
|
|
err = fmt.Errorf("subscriber error: %s", strings.Join(errors, "\n"))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:gocyclo
|
|
|
|
func (n *noopServer) newSubHandler(sb *subscriber, opts Options) broker.Handler {
|
2020-10-16 09:38:57 +03:00
|
|
|
return func(p broker.Event) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2020-11-04 00:38:12 +03:00
|
|
|
n.RLock()
|
|
|
|
config := n.opts
|
|
|
|
n.RUnlock()
|
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-01-10 19:24:03 +03:00
|
|
|
config.Logger.Error(n.opts.Context, "panic recovered: ", r)
|
|
|
|
config.Logger.Error(n.opts.Context, string(debug.Stack()))
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
err = errors.InternalServerError(n.opts.Name+".subscriber", "panic recovered: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
msg := p.Message()
|
|
|
|
// if we don't have headers, create empty map
|
|
|
|
if msg.Header == nil {
|
2021-07-27 23:58:06 +03:00
|
|
|
msg.Header = metadata.New(2)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ct := msg.Header["Content-Type"]
|
|
|
|
if len(ct) == 0 {
|
2021-07-27 23:58:06 +03:00
|
|
|
msg.Header.Set(metadata.HeaderContentType, defaultContentType)
|
2020-10-16 09:38:57 +03:00
|
|
|
ct = defaultContentType
|
|
|
|
}
|
|
|
|
cf, err := n.newCodec(ct)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-27 23:58:06 +03:00
|
|
|
hdr := metadata.New(len(msg.Header))
|
2020-10-16 09:38:57 +03:00
|
|
|
for k, v := range msg.Header {
|
2021-02-13 01:46:16 +03:00
|
|
|
if k == "Content-Type" {
|
|
|
|
continue
|
|
|
|
}
|
2021-07-27 23:58:06 +03:00
|
|
|
hdr.Set(k, v)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
2021-02-13 01:46:16 +03:00
|
|
|
|
|
|
|
ctx := metadata.NewIncomingContext(sb.opts.Context, hdr)
|
2020-10-16 09:38:57 +03:00
|
|
|
|
|
|
|
results := make(chan error, len(sb.handlers))
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
if err = cf.ReadBody(bytes.NewBuffer(msg.Body), req.Interface()); err != nil {
|
2020-10-16 09:38:57 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := func(ctx context.Context, msg 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:23:41 +03:00
|
|
|
vals = append(vals, reflect.ValueOf(msg.Body()))
|
2020-10-16 09:38:57 +03:00
|
|
|
|
|
|
|
returnValues := handler.method.Call(vals)
|
|
|
|
if rerr := returnValues[0].Interface(); rerr != nil {
|
|
|
|
return rerr.(error)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(opts.SubWrappers); i > 0; i-- {
|
|
|
|
fn = opts.SubWrappers[i-1](fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.wg != nil {
|
|
|
|
n.wg.Add(1)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
if n.wg != nil {
|
|
|
|
defer n.wg.Done()
|
|
|
|
}
|
2021-04-27 08:32:47 +03:00
|
|
|
cerr := fn(ctx, &rpcMessage{
|
2020-10-16 09:38:57 +03:00
|
|
|
topic: sb.topic,
|
|
|
|
contentType: ct,
|
|
|
|
payload: req.Interface(),
|
|
|
|
header: msg.Header,
|
|
|
|
})
|
2021-04-27 08:32:47 +03:00
|
|
|
results <- cerr
|
2020-10-16 09:38:57 +03:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
var errors []string
|
|
|
|
for i := 0; i < len(sb.handlers); i++ {
|
|
|
|
if rerr := <-results; rerr != nil {
|
|
|
|
errors = append(errors, rerr.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errors) > 0 {
|
|
|
|
err = fmt.Errorf("subscriber error: %s", strings.Join(errors, "\n"))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Topic() string {
|
|
|
|
return s.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Subscriber() interface{} {
|
|
|
|
return s.subscriber
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (s *subscriber) Endpoints() []*register.Endpoint {
|
2020-10-16 09:38:57 +03:00
|
|
|
return s.endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Options() SubscriberOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|