2019-06-03 18:44:43 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2019-11-25 22:50:59 +03:00
|
|
|
"runtime/debug"
|
2019-06-17 17:54:37 +08:00
|
|
|
"strings"
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/broker"
|
|
|
|
"github.com/micro/go-micro/v2/errors"
|
2020-02-23 16:45:20 +03:00
|
|
|
log "github.com/micro/go-micro/v2/logger"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/metadata"
|
|
|
|
"github.com/micro/go-micro/v2/registry"
|
|
|
|
"github.com/micro/go-micro/v2/server"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
subSig = "func(context.Context, interface{}) error"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
opts server.SubscriberOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
|
|
|
options := server.SubscriberOptions{
|
|
|
|
AutoAck: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
"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)
|
|
|
|
|
|
|
|
endpoints = append(endpoints, ®istry.Endpoint{
|
|
|
|
Name: name + "." + method.Name,
|
|
|
|
Request: extractSubValue(method.Type),
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateSubscriber(sub server.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 {
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcServer) createSubHandler(sb *subscriber, opts server.Options) broker.Handler {
|
2019-12-09 11:08:21 +08:00
|
|
|
return func(p broker.Event) (err error) {
|
2019-11-25 22:50:59 +03:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2020-02-23 16:45:20 +03:00
|
|
|
log.Error("panic recovered: ", r)
|
|
|
|
log.Error(string(debug.Stack()))
|
2019-11-25 22:50:59 +03:00
|
|
|
err = errors.InternalServerError("go.micro.server", "panic recovered: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
msg := p.Message()
|
|
|
|
ct := msg.Header["Content-Type"]
|
2019-06-17 17:54:37 +08:00
|
|
|
if len(ct) == 0 {
|
|
|
|
msg.Header["Content-Type"] = defaultContentType
|
|
|
|
ct = defaultContentType
|
|
|
|
}
|
2019-08-18 11:28:21 +01:00
|
|
|
cf, err := g.newGRPCCodec(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
|
|
|
|
}
|
|
|
|
delete(hdr, "Content-Type")
|
|
|
|
ctx := metadata.NewContext(context.Background(), hdr)
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
vals = append(vals, reflect.ValueOf(msg.Payload()))
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(opts.SubWrappers); i > 0; i-- {
|
|
|
|
fn = opts.SubWrappers[i-1](fn)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
2020-02-26 21:34:40 +03:00
|
|
|
err := 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,
|
|
|
|
body: msg.Body,
|
2019-06-03 18:44:43 +01:00
|
|
|
})
|
2020-02-26 21:34:40 +03:00
|
|
|
results <- err
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Endpoints() []*registry.Endpoint {
|
|
|
|
return s.endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Options() server.SubscriberOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|