2015-06-12 19:52:27 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-11-28 11:22:29 +00:00
|
|
|
"bytes"
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2015-12-02 17:42:14 +00:00
|
|
|
"fmt"
|
2015-06-12 19:52:27 +01:00
|
|
|
"reflect"
|
2018-04-26 10:47:13 +01:00
|
|
|
"strings"
|
2015-06-12 19:52:27 +01:00
|
|
|
|
2015-11-20 16:17:33 +00:00
|
|
|
"github.com/micro/go-micro/broker"
|
2015-11-28 11:22:29 +00:00
|
|
|
"github.com/micro/go-micro/codec"
|
2016-01-28 17:55:28 +00:00
|
|
|
"github.com/micro/go-micro/metadata"
|
2015-11-20 16:17:33 +00:00
|
|
|
"github.com/micro/go-micro/registry"
|
2015-06-12 19:52:27 +01:00
|
|
|
)
|
|
|
|
|
2015-12-02 17:42:14 +00:00
|
|
|
const (
|
|
|
|
subSig = "func(context.Context, interface{}) error"
|
|
|
|
)
|
|
|
|
|
2015-06-12 19:52:27 +01:00
|
|
|
type handler struct {
|
|
|
|
method reflect.Value
|
|
|
|
reqType reflect.Type
|
|
|
|
ctxType reflect.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
type subscriber struct {
|
|
|
|
topic string
|
|
|
|
rcvr reflect.Value
|
|
|
|
typ reflect.Type
|
|
|
|
subscriber interface{}
|
|
|
|
handlers []*handler
|
|
|
|
endpoints []*registry.Endpoint
|
2016-01-08 14:02:32 +00:00
|
|
|
opts SubscriberOptions
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 14:02:32 +00:00
|
|
|
func newSubscriber(topic string, sub interface{}, opts ...SubscriberOption) Subscriber {
|
2019-05-24 20:06:27 +08:00
|
|
|
options := SubscriberOptions{
|
|
|
|
AutoAck: true,
|
|
|
|
}
|
|
|
|
|
2016-01-08 14:02:32 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2015-06-12 19:52:27 +01:00
|
|
|
var endpoints []*registry.Endpoint
|
|
|
|
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)
|
|
|
|
|
|
|
|
endpoints = append(endpoints, ®istry.Endpoint{
|
|
|
|
Name: "Func",
|
|
|
|
Request: extractSubValue(typ),
|
|
|
|
Metadata: map[string]string{
|
2015-10-11 12:12:41 +01:00
|
|
|
"topic": topic,
|
|
|
|
"subscriber": "true",
|
2015-06-12 19:52:27 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
2015-10-11 12:12:41 +01:00
|
|
|
hdlr := reflect.ValueOf(sub)
|
|
|
|
name := reflect.Indirect(hdlr).Type().Name()
|
|
|
|
|
2015-06-12 19:52:27 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
endpoints = append(endpoints, ®istry.Endpoint{
|
2015-10-11 12:12:41 +01:00
|
|
|
Name: name + "." + method.Name,
|
2015-06-12 19:52:27 +01:00
|
|
|
Request: extractSubValue(method.Type),
|
|
|
|
Metadata: map[string]string{
|
2015-10-11 12:12:41 +01:00
|
|
|
"topic": topic,
|
|
|
|
"subscriber": "true",
|
2015-06-12 19:52:27 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &subscriber{
|
|
|
|
rcvr: reflect.ValueOf(sub),
|
|
|
|
typ: reflect.TypeOf(sub),
|
|
|
|
topic: topic,
|
|
|
|
subscriber: sub,
|
|
|
|
handlers: handlers,
|
|
|
|
endpoints: endpoints,
|
2016-01-08 14:02:32 +00:00
|
|
|
opts: options,
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:42:14 +00:00
|
|
|
func validateSubscriber(sub Subscriber) error {
|
|
|
|
typ := reflect.TypeOf(sub.Subscriber())
|
|
|
|
var argType reflect.Type
|
|
|
|
|
|
|
|
if typ.Kind() == reflect.Func {
|
|
|
|
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 {
|
2016-04-06 18:03:27 +01:00
|
|
|
return fmt.Errorf("subscriber %v has wrong number of outs: %v require signature %s",
|
2015-12-02 17:42:14 +00:00
|
|
|
name, typ.NumOut(), subSig)
|
|
|
|
}
|
|
|
|
if returnType := typ.Out(0); returnType != typeOfError {
|
|
|
|
return fmt.Errorf("subscriber %v returns %v not error", name, returnType.String())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-12-31 18:11:46 +00:00
|
|
|
func (s *rpcServer) createSubHandler(sb *subscriber, opts Options) broker.Handler {
|
2015-12-23 19:16:55 +00:00
|
|
|
return func(p broker.Publication) error {
|
|
|
|
msg := p.Message()
|
2019-01-11 15:49:54 +00:00
|
|
|
|
|
|
|
// get codec
|
2015-12-02 20:56:50 +00:00
|
|
|
ct := msg.Header["Content-Type"]
|
2019-01-11 15:49:54 +00:00
|
|
|
|
|
|
|
// default content type
|
|
|
|
if len(ct) == 0 {
|
|
|
|
msg.Header["Content-Type"] = DefaultContentType
|
|
|
|
ct = DefaultContentType
|
|
|
|
}
|
|
|
|
|
|
|
|
// get codec
|
2015-12-02 20:56:50 +00:00
|
|
|
cf, err := s.newCodec(ct)
|
2015-11-28 11:22:29 +00:00
|
|
|
if err != nil {
|
2015-12-23 19:16:55 +00:00
|
|
|
return err
|
2015-11-28 11:22:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 15:49:54 +00:00
|
|
|
// copy headers
|
2015-06-12 19:52:27 +01:00
|
|
|
hdr := make(map[string]string)
|
|
|
|
for k, v := range msg.Header {
|
|
|
|
hdr[k] = v
|
|
|
|
}
|
2019-01-11 15:49:54 +00:00
|
|
|
|
|
|
|
// create context
|
2016-01-28 17:55:28 +00:00
|
|
|
ctx := metadata.NewContext(context.Background(), hdr)
|
2015-06-12 19:52:27 +01:00
|
|
|
|
2018-04-26 10:47:13 +01:00
|
|
|
results := make(chan error, len(sb.handlers))
|
|
|
|
|
2015-12-02 19:56:57 +00:00
|
|
|
for i := 0; i < len(sb.handlers); i++ {
|
|
|
|
handler := sb.handlers[i]
|
|
|
|
|
2015-06-12 19:52:27 +01:00
|
|
|
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
|
|
|
|
}
|
2015-12-02 19:56:57 +00:00
|
|
|
if isVal {
|
|
|
|
req = req.Elem()
|
|
|
|
}
|
2015-06-12 19:52:27 +01:00
|
|
|
|
2015-12-02 19:56:57 +00:00
|
|
|
b := &buffer{bytes.NewBuffer(msg.Body)}
|
|
|
|
co := cf(b)
|
|
|
|
defer co.Close()
|
|
|
|
|
|
|
|
if err := co.ReadHeader(&codec.Message{}, codec.Publication); err != nil {
|
2015-12-23 19:16:55 +00:00
|
|
|
return err
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
|
|
|
|
2015-12-02 19:56:57 +00:00
|
|
|
if err := co.ReadBody(req.Interface()); err != nil {
|
2015-12-23 19:16:55 +00:00
|
|
|
return err
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 18:21:02 +01:00
|
|
|
fn := func(ctx context.Context, msg Message) error {
|
2015-12-02 19:56:57 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:21:02 +01:00
|
|
|
vals = append(vals, reflect.ValueOf(msg.Payload()))
|
2015-12-02 19:56:57 +00:00
|
|
|
|
|
|
|
returnValues := handler.method.Call(vals)
|
|
|
|
if err := returnValues[0].Interface(); err != nil {
|
|
|
|
return err.(error)
|
|
|
|
}
|
|
|
|
return nil
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
|
|
|
|
2015-12-31 18:11:46 +00:00
|
|
|
for i := len(opts.SubWrappers); i > 0; i-- {
|
|
|
|
fn = opts.SubWrappers[i-1](fn)
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
|
|
|
|
2019-05-29 09:28:04 +01:00
|
|
|
if s.wg != nil {
|
|
|
|
s.wg.Add(1)
|
|
|
|
}
|
|
|
|
|
2017-05-31 19:47:41 +01:00
|
|
|
go func() {
|
2019-05-29 09:28:04 +01:00
|
|
|
if s.wg != nil {
|
|
|
|
defer s.wg.Done()
|
|
|
|
}
|
|
|
|
|
2018-04-26 10:47:13 +01:00
|
|
|
results <- fn(ctx, &rpcMessage{
|
2017-05-31 19:47:41 +01:00
|
|
|
topic: sb.topic,
|
|
|
|
contentType: ct,
|
2018-04-14 18:21:02 +01:00
|
|
|
payload: req.Interface(),
|
2017-05-31 19:47:41 +01:00
|
|
|
})
|
|
|
|
}()
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
2018-04-26 10:47:13 +01: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"))
|
|
|
|
}
|
|
|
|
|
2015-12-23 19:16:55 +00:00
|
|
|
return nil
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Topic() string {
|
|
|
|
return s.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Subscriber() interface{} {
|
|
|
|
return s.subscriber
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Endpoints() []*registry.Endpoint {
|
|
|
|
return s.endpoints
|
|
|
|
}
|
2016-01-08 14:02:32 +00:00
|
|
|
|
|
|
|
func (s *subscriber) Options() SubscriberOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|