2020-09-03 12:21:00 +03:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2021-10-27 01:04:58 +03:00
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2020-09-03 12:21:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
subSig = "func(context.Context, interface{}) error"
|
|
|
|
)
|
|
|
|
|
|
|
|
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
|
|
|
|
|
|
|
|
type handler struct {
|
|
|
|
reqType reflect.Type
|
|
|
|
ctxType reflect.Type
|
2021-04-26 01:10:37 +03:00
|
|
|
method reflect.Value
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type tcpSubscriber struct {
|
|
|
|
topic string
|
|
|
|
rcvr reflect.Value
|
|
|
|
typ reflect.Type
|
|
|
|
subscriber interface{}
|
|
|
|
handlers []*handler
|
2021-01-29 15:16:03 +03:00
|
|
|
endpoints []*register.Endpoint
|
2020-09-03 12:21:00 +03:00
|
|
|
opts server.SubscriberOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this an exported - upper case - name?
|
|
|
|
func isExported(name string) bool {
|
|
|
|
r, _ := utf8.DecodeRuneInString(name)
|
|
|
|
return unicode.IsUpper(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
|
|
|
options := server.SubscriberOptions{
|
|
|
|
AutoAck: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:16:03 +03:00
|
|
|
var endpoints []*register.Endpoint
|
2020-09-03 12:21:00 +03: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:16:03 +03:00
|
|
|
ep := ®ister.Endpoint{
|
2020-11-18 13:02:53 +03:00
|
|
|
Name: "Func",
|
2021-01-29 15:16:03 +03:00
|
|
|
Request: register.ExtractSubValue(typ),
|
2020-11-18 13:02:53 +03:00
|
|
|
Metadata: metadata.New(2),
|
|
|
|
}
|
|
|
|
ep.Metadata.Set("topic", topic)
|
|
|
|
ep.Metadata.Set("subscriber", "true")
|
|
|
|
endpoints = append(endpoints, ep)
|
2020-09-03 12:21:00 +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 15:16:03 +03:00
|
|
|
ep := ®ister.Endpoint{
|
2020-11-18 13:02:53 +03:00
|
|
|
Name: name + "." + method.Name,
|
2021-01-29 15:16:03 +03:00
|
|
|
Request: register.ExtractSubValue(method.Type),
|
2020-11-18 13:02:53 +03:00
|
|
|
Metadata: metadata.New(2),
|
|
|
|
}
|
|
|
|
ep.Metadata.Set("topic", topic)
|
|
|
|
ep.Metadata.Set("subscriber", "true")
|
|
|
|
endpoints = append(endpoints, ep)
|
2020-09-03 12:21:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tcpSubscriber{
|
|
|
|
rcvr: reflect.ValueOf(sub),
|
|
|
|
typ: reflect.TypeOf(sub),
|
|
|
|
topic: topic,
|
|
|
|
subscriber: sub,
|
|
|
|
handlers: handlers,
|
|
|
|
endpoints: endpoints,
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateSubscriber(sub server.Subscriber) error {
|
|
|
|
typ := reflect.TypeOf(sub.Subscriber())
|
|
|
|
var argType reflect.Type
|
|
|
|
|
2021-04-26 01:10:37 +03:00
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Func:
|
2020-09-03 12:21:00 +03:00
|
|
|
name := "Func"
|
|
|
|
switch typ.NumIn() {
|
|
|
|
case 2:
|
|
|
|
argType = typ.In(1)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("subscriber %v takes wrong number of args: %v required signature %s", name, typ.NumIn(), subSig)
|
|
|
|
}
|
|
|
|
if !isExportedOrBuiltinType(argType) {
|
|
|
|
return fmt.Errorf("subscriber %v argument type not exported: %v", name, argType)
|
|
|
|
}
|
|
|
|
if typ.NumOut() != 1 {
|
|
|
|
return fmt.Errorf("subscriber %v has wrong number of outs: %v require signature %s",
|
|
|
|
name, typ.NumOut(), subSig)
|
|
|
|
}
|
|
|
|
if returnType := typ.Out(0); returnType != typeOfError {
|
|
|
|
return fmt.Errorf("subscriber %v returns %v not error", name, returnType.String())
|
|
|
|
}
|
2021-04-26 01:10:37 +03:00
|
|
|
default:
|
2020-09-03 12:21:00 +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:
|
|
|
|
return fmt.Errorf("subscriber %v.%v takes wrong number of args: %v required signature %s",
|
|
|
|
name, method.Name, method.Type.NumIn(), subSig)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isExportedOrBuiltinType(argType) {
|
|
|
|
return fmt.Errorf("%v argument type not exported: %v", name, argType)
|
|
|
|
}
|
|
|
|
if method.Type.NumOut() != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"subscriber %v.%v has wrong number of outs: %v require signature %s",
|
|
|
|
name, method.Name, method.Type.NumOut(), subSig)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tcpServer) createSubHandler(sb *tcpSubscriber, opts server.Options) broker.Handler {
|
|
|
|
return func(p broker.Event) error {
|
|
|
|
msg := p.Message()
|
|
|
|
ct := msg.Header["Content-Type"]
|
|
|
|
cf, err := s.newCodec(ct)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-26 01:10:37 +03:00
|
|
|
hdr := make(map[string]string, len(msg.Header)-1)
|
2020-09-03 12:21:00 +03:00
|
|
|
for k, v := range msg.Header {
|
2021-04-26 01:10:37 +03:00
|
|
|
if k == "Content-Type" {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-03 12:21:00 +03:00
|
|
|
hdr[k] = v
|
|
|
|
}
|
2021-04-26 01:10:37 +03:00
|
|
|
ctx := metadata.NewIncomingContext(context.Background(), hdr)
|
2020-09-03 12:21:00 +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-25 23:56:38 +03:00
|
|
|
buf := bytes.NewBuffer(msg.Body)
|
2020-09-03 12:21:00 +03:00
|
|
|
|
2020-11-25 23:56:38 +03:00
|
|
|
if err := cf.ReadHeader(buf, &codec.Message{}, codec.Event); err != nil {
|
2020-09-03 12:21:00 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-25 23:56:38 +03:00
|
|
|
if err := cf.ReadBody(buf, req.Interface()); err != nil {
|
2020-09-03 12:21:00 +03: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:30:19 +03:00
|
|
|
vals = append(vals, reflect.ValueOf(msg.Body()))
|
2020-09-03 12:21:00 +03:00
|
|
|
|
|
|
|
returnValues := handler.method.Call(vals)
|
|
|
|
if err := returnValues[0].Interface(); err != nil {
|
|
|
|
return err.(error)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(opts.SubWrappers); i > 0; i-- {
|
|
|
|
fn = opts.SubWrappers[i-1](fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
results <- fn(ctx, &tcpMessage{
|
|
|
|
topic: sb.topic,
|
|
|
|
contentType: ct,
|
|
|
|
payload: req.Interface(),
|
|
|
|
header: msg.Header,
|
2020-11-25 23:56:38 +03:00
|
|
|
codec: cf,
|
2020-09-03 12:21:00 +03:00
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
var errors []string
|
|
|
|
|
|
|
|
for i := 0; i < len(sb.handlers); i++ {
|
|
|
|
if err := <-results; err != nil {
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return fmt.Errorf("subscriber error: %s", strings.Join(errors, "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tcpSubscriber) Topic() string {
|
|
|
|
return s.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tcpSubscriber) Subscriber() interface{} {
|
|
|
|
return s.subscriber
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:16:03 +03:00
|
|
|
func (s *tcpSubscriber) Endpoints() []*register.Endpoint {
|
2020-09-03 12:21:00 +03:00
|
|
|
return s.endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tcpSubscriber) Options() server.SubscriberOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|