2020-07-27 13:22:00 +01:00
|
|
|
package mucp
|
2015-12-03 00:59:32 +00:00
|
|
|
|
2015-12-01 23:32:23 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//
|
|
|
|
// Meh, we need to get rid of this shit
|
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2015-12-01 23:32:23 +00:00
|
|
|
"errors"
|
2019-11-25 16:31:43 +00:00
|
|
|
"fmt"
|
2015-12-01 23:32:23 +00:00
|
|
|
"io"
|
|
|
|
"reflect"
|
2019-12-03 01:36:14 +03:00
|
|
|
"runtime/debug"
|
2015-12-01 23:32:23 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
"github.com/micro/go-micro/v3/codec"
|
|
|
|
merrors "github.com/micro/go-micro/v3/errors"
|
|
|
|
"github.com/micro/go-micro/v3/server"
|
2015-12-01 23:32:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-12-02 00:47:52 +00:00
|
|
|
lastStreamResponseError = errors.New("EOS")
|
2015-12-01 23:32:23 +00:00
|
|
|
|
|
|
|
// Precompute the reflect type for error. Can't use error directly
|
|
|
|
// because Typeof takes an empty interface value. This is annoying.
|
|
|
|
typeOfError = reflect.TypeOf((*error)(nil)).Elem()
|
|
|
|
)
|
|
|
|
|
|
|
|
type methodType struct {
|
|
|
|
sync.Mutex // protects counters
|
|
|
|
method reflect.Method
|
|
|
|
ArgType reflect.Type
|
|
|
|
ReplyType reflect.Type
|
|
|
|
ContextType reflect.Type
|
|
|
|
stream bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type service struct {
|
|
|
|
name string // name of service
|
|
|
|
rcvr reflect.Value // receiver of methods for the service
|
|
|
|
typ reflect.Type // type of the receiver
|
|
|
|
method map[string]*methodType // registered methods
|
|
|
|
}
|
|
|
|
|
|
|
|
type request struct {
|
2019-01-08 15:38:25 +00:00
|
|
|
msg *codec.Message
|
|
|
|
next *request // for free list in Server
|
2015-12-01 23:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
2019-01-08 15:38:25 +00:00
|
|
|
msg *codec.Message
|
|
|
|
next *response // for free list in Server
|
2015-12-01 23:32:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 14:44:40 +00:00
|
|
|
// router represents an RPC router.
|
|
|
|
type router struct {
|
2019-11-25 16:31:43 +00:00
|
|
|
name string
|
|
|
|
|
|
|
|
mu sync.Mutex // protects the serviceMap
|
|
|
|
serviceMap map[string]*service
|
|
|
|
|
|
|
|
reqLock sync.Mutex // protects freeReq
|
|
|
|
freeReq *request
|
|
|
|
|
|
|
|
respLock sync.Mutex // protects freeResp
|
|
|
|
freeResp *response
|
|
|
|
|
|
|
|
// handler wrappers
|
2020-07-27 13:22:00 +01:00
|
|
|
hdlrWrappers []server.HandlerWrapper
|
2019-11-25 16:31:43 +00:00
|
|
|
// subscriber wrappers
|
2020-07-27 13:22:00 +01:00
|
|
|
subWrappers []server.SubscriberWrapper
|
2019-11-25 16:31:43 +00:00
|
|
|
|
|
|
|
su sync.RWMutex
|
|
|
|
subscribers map[string][]*subscriber
|
2015-12-01 23:32:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// rpcRouter encapsulates functions that become a server.Router
|
|
|
|
type rpcRouter struct {
|
2020-07-27 13:22:00 +01:00
|
|
|
h func(context.Context, server.Request, interface{}) error
|
|
|
|
m func(context.Context, server.Message) error
|
2019-11-27 17:12:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (r rpcRouter) ProcessMessage(ctx context.Context, msg server.Message) error {
|
2019-11-27 17:12:07 +00:00
|
|
|
return r.m(ctx, msg)
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (r rpcRouter) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error {
|
2019-11-27 17:12:07 +00:00
|
|
|
return r.h(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
|
2019-01-11 15:49:54 +00:00
|
|
|
func newRpcRouter() *router {
|
2019-01-07 14:44:40 +00:00
|
|
|
return &router{
|
2019-11-25 16:31:43 +00:00
|
|
|
serviceMap: make(map[string]*service),
|
|
|
|
subscribers: make(map[string][]*subscriber),
|
2019-01-07 14:44:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 23:32:23 +00: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() == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareMethod returns a methodType for the provided method or nil
|
|
|
|
// in case if the method was unsuitable.
|
|
|
|
func prepareMethod(method reflect.Method) *methodType {
|
|
|
|
mtype := method.Type
|
|
|
|
mname := method.Name
|
|
|
|
var replyType, argType, contextType reflect.Type
|
2015-12-18 01:01:59 +00:00
|
|
|
var stream bool
|
2015-12-01 23:32:23 +00:00
|
|
|
|
|
|
|
// Method must be exported.
|
|
|
|
if method.PkgPath != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch mtype.NumIn() {
|
2015-12-18 01:01:59 +00:00
|
|
|
case 3:
|
|
|
|
// assuming streaming
|
|
|
|
argType = mtype.In(2)
|
|
|
|
contextType = mtype.In(1)
|
|
|
|
stream = true
|
2015-12-01 23:32:23 +00:00
|
|
|
case 4:
|
|
|
|
// method that takes a context
|
|
|
|
argType = mtype.In(2)
|
|
|
|
replyType = mtype.In(3)
|
|
|
|
contextType = mtype.In(1)
|
|
|
|
default:
|
2020-06-05 10:18:35 +01:00
|
|
|
log.Errorf("method %v of %v has wrong number of ins: %v", mname, mtype, mtype.NumIn())
|
2015-12-01 23:32:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-18 01:01:59 +00:00
|
|
|
if stream {
|
|
|
|
// check stream type
|
2020-07-27 13:22:00 +01:00
|
|
|
streamType := reflect.TypeOf((*server.Stream)(nil)).Elem()
|
2015-12-18 01:01:59 +00:00
|
|
|
if !argType.Implements(streamType) {
|
2020-06-05 10:18:35 +01:00
|
|
|
log.Errorf("%v argument does not implement Stream interface: %v", mname, argType)
|
2015-12-01 23:32:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-18 01:01:59 +00:00
|
|
|
} else {
|
|
|
|
// if not stream check the replyType
|
|
|
|
|
|
|
|
// First arg need not be a pointer.
|
|
|
|
if !isExportedOrBuiltinType(argType) {
|
2020-06-05 10:18:35 +01:00
|
|
|
log.Errorf("%v argument type not exported: %v", mname, argType)
|
2015-12-01 23:32:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-18 01:01:59 +00:00
|
|
|
|
|
|
|
if replyType.Kind() != reflect.Ptr {
|
2020-06-05 10:18:35 +01:00
|
|
|
log.Errorf("method %v reply type not a pointer: %v", mname, replyType)
|
2015-12-01 23:32:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-18 01:01:59 +00:00
|
|
|
|
|
|
|
// Reply type must be exported.
|
|
|
|
if !isExportedOrBuiltinType(replyType) {
|
2020-06-05 10:18:35 +01:00
|
|
|
log.Errorf("method %v reply type not exported: %v", mname, replyType)
|
2015-12-01 23:32:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method needs one out.
|
|
|
|
if mtype.NumOut() != 1 {
|
2020-06-05 10:18:35 +01:00
|
|
|
log.Errorf("method %v has wrong number of outs: %v", mname, mtype.NumOut())
|
2015-12-01 23:32:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// The return type of the method must be error.
|
|
|
|
if returnType := mtype.Out(0); returnType != typeOfError {
|
2020-06-05 10:18:35 +01:00
|
|
|
log.Errorf("method %v returns %v not error", mname, returnType.String())
|
2015-12-01 23:32:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &methodType{method: method, ArgType: argType, ReplyType: replyType, ContextType: contextType, stream: stream}
|
|
|
|
}
|
|
|
|
|
2019-01-22 13:52:18 +00:00
|
|
|
func (router *router) sendResponse(sending sync.Locker, req *request, reply interface{}, cc codec.Writer, last bool) error {
|
2019-01-08 15:38:25 +00:00
|
|
|
msg := new(codec.Message)
|
|
|
|
msg.Type = codec.Response
|
2019-01-07 14:44:40 +00:00
|
|
|
resp := router.getResponse()
|
2019-01-08 15:38:25 +00:00
|
|
|
resp.msg = msg
|
|
|
|
|
|
|
|
resp.msg.Id = req.msg.Id
|
2015-12-01 23:32:23 +00:00
|
|
|
sending.Lock()
|
2019-01-22 13:52:18 +00:00
|
|
|
err := cc.Write(resp.msg, reply)
|
2015-12-01 23:32:23 +00:00
|
|
|
sending.Unlock()
|
2019-01-07 14:44:40 +00:00
|
|
|
router.freeResponse(resp)
|
2015-12-01 23:32:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-22 13:52:18 +00:00
|
|
|
func (s *service) call(ctx context.Context, router *router, sending *sync.Mutex, mtype *methodType, req *request, argv, replyv reflect.Value, cc codec.Writer) error {
|
|
|
|
defer router.freeRequest(req)
|
|
|
|
|
2015-12-01 23:32:23 +00:00
|
|
|
function := mtype.method.Func
|
|
|
|
var returnValues []reflect.Value
|
|
|
|
|
2015-12-02 20:56:50 +00:00
|
|
|
r := &rpcRequest{
|
2019-01-09 16:20:57 +00:00
|
|
|
service: req.msg.Target,
|
2019-01-08 20:32:47 +00:00
|
|
|
contentType: req.msg.Header["Content-Type"],
|
2019-01-18 10:12:57 +00:00
|
|
|
method: req.msg.Method,
|
2019-01-10 21:25:31 +00:00
|
|
|
endpoint: req.msg.Endpoint,
|
2019-01-09 16:20:57 +00:00
|
|
|
body: req.msg.Body,
|
2020-03-22 07:25:23 +08:00
|
|
|
header: req.msg.Header,
|
2019-06-20 12:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// only set if not nil
|
|
|
|
if argv.IsValid() {
|
|
|
|
r.rawBody = argv.Interface()
|
2015-12-02 20:56:50 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 23:32:23 +00:00
|
|
|
if !mtype.stream {
|
2020-07-27 13:22:00 +01:00
|
|
|
fn := func(ctx context.Context, req server.Request, rsp interface{}) error {
|
2019-01-09 16:20:57 +00:00
|
|
|
returnValues = function.Call([]reflect.Value{s.rcvr, mtype.prepareContext(ctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)})
|
2015-12-02 00:47:52 +00:00
|
|
|
|
|
|
|
// The return value for the method is an error.
|
|
|
|
if err := returnValues[0].Interface(); err != nil {
|
|
|
|
return err.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:02:19 +01:00
|
|
|
// wrap the handler
|
|
|
|
for i := len(router.hdlrWrappers); i > 0; i-- {
|
|
|
|
fn = router.hdlrWrappers[i-1](fn)
|
|
|
|
}
|
|
|
|
|
2019-01-22 13:52:18 +00:00
|
|
|
// execute handler
|
|
|
|
if err := fn(ctx, r, replyv.Interface()); err != nil {
|
|
|
|
return err
|
2015-12-01 23:32:23 +00:00
|
|
|
}
|
2015-12-02 00:47:52 +00:00
|
|
|
|
2019-01-22 13:52:18 +00:00
|
|
|
// send response
|
|
|
|
return router.sendResponse(sending, req, replyv.Interface(), cc, true)
|
2015-12-01 23:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// declare a local error to see if we errored out already
|
|
|
|
// keep track of the type, to make sure we return
|
|
|
|
// the same one consistently
|
2019-06-03 15:55:47 +01:00
|
|
|
rawStream := &rpcStream{
|
2015-12-18 01:01:59 +00:00
|
|
|
context: ctx,
|
2019-01-09 19:28:13 +00:00
|
|
|
codec: cc.(codec.Codec),
|
2015-12-18 01:01:59 +00:00
|
|
|
request: r,
|
2019-01-08 15:38:25 +00:00
|
|
|
id: req.msg.Id,
|
2015-12-01 23:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke the method, providing a new value for the reply.
|
2020-07-27 13:22:00 +01:00
|
|
|
fn := func(ctx context.Context, req server.Request, stream interface{}) error {
|
2015-12-18 01:01:59 +00:00
|
|
|
returnValues = function.Call([]reflect.Value{s.rcvr, mtype.prepareContext(ctx), reflect.ValueOf(stream)})
|
2015-12-02 00:47:52 +00:00
|
|
|
if err := returnValues[0].Interface(); err != nil {
|
|
|
|
// the function returned an error, we use that
|
|
|
|
return err.(error)
|
2019-06-03 15:55:47 +01:00
|
|
|
} else if serr := rawStream.Error(); serr == io.EOF || serr == io.ErrUnexpectedEOF {
|
|
|
|
return nil
|
2015-12-02 00:47:52 +00:00
|
|
|
} else {
|
|
|
|
// no error, we send the special EOS error
|
|
|
|
return lastStreamResponseError
|
|
|
|
}
|
2015-12-01 23:32:23 +00:00
|
|
|
}
|
2015-12-02 00:47:52 +00:00
|
|
|
|
2019-06-07 15:02:19 +01:00
|
|
|
// wrap the handler
|
|
|
|
for i := len(router.hdlrWrappers); i > 0; i-- {
|
|
|
|
fn = router.hdlrWrappers[i-1](fn)
|
|
|
|
}
|
|
|
|
|
2015-12-02 20:56:50 +00:00
|
|
|
// client.Stream request
|
|
|
|
r.stream = true
|
|
|
|
|
2019-01-22 13:52:18 +00:00
|
|
|
// execute handler
|
2019-06-03 15:55:47 +01:00
|
|
|
return fn(ctx, r, rawStream)
|
2015-12-01 23:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *methodType) prepareContext(ctx context.Context) reflect.Value {
|
|
|
|
if contextv := reflect.ValueOf(ctx); contextv.IsValid() {
|
|
|
|
return contextv
|
|
|
|
}
|
|
|
|
return reflect.Zero(m.ContextType)
|
|
|
|
}
|
|
|
|
|
2019-01-07 14:44:40 +00:00
|
|
|
func (router *router) getRequest() *request {
|
|
|
|
router.reqLock.Lock()
|
|
|
|
req := router.freeReq
|
2015-12-01 23:32:23 +00:00
|
|
|
if req == nil {
|
|
|
|
req = new(request)
|
|
|
|
} else {
|
2019-01-07 14:44:40 +00:00
|
|
|
router.freeReq = req.next
|
2015-12-01 23:32:23 +00:00
|
|
|
*req = request{}
|
|
|
|
}
|
2019-01-07 14:44:40 +00:00
|
|
|
router.reqLock.Unlock()
|
2015-12-01 23:32:23 +00:00
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2019-01-07 14:44:40 +00:00
|
|
|
func (router *router) freeRequest(req *request) {
|
|
|
|
router.reqLock.Lock()
|
|
|
|
req.next = router.freeReq
|
|
|
|
router.freeReq = req
|
|
|
|
router.reqLock.Unlock()
|
2015-12-01 23:32:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 14:44:40 +00:00
|
|
|
func (router *router) getResponse() *response {
|
|
|
|
router.respLock.Lock()
|
|
|
|
resp := router.freeResp
|
2015-12-01 23:32:23 +00:00
|
|
|
if resp == nil {
|
|
|
|
resp = new(response)
|
|
|
|
} else {
|
2019-01-07 14:44:40 +00:00
|
|
|
router.freeResp = resp.next
|
2015-12-01 23:32:23 +00:00
|
|
|
*resp = response{}
|
|
|
|
}
|
2019-01-07 14:44:40 +00:00
|
|
|
router.respLock.Unlock()
|
2015-12-01 23:32:23 +00:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2019-01-07 14:44:40 +00:00
|
|
|
func (router *router) freeResponse(resp *response) {
|
|
|
|
router.respLock.Lock()
|
|
|
|
resp.next = router.freeResp
|
|
|
|
router.freeResp = resp
|
|
|
|
router.respLock.Unlock()
|
2015-12-01 23:32:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (router *router) readRequest(r server.Request) (service *service, mtype *methodType, req *request, argv, replyv reflect.Value, keepReading bool, err error) {
|
2019-01-09 16:20:57 +00:00
|
|
|
cc := r.Codec()
|
|
|
|
|
2019-01-08 15:38:25 +00:00
|
|
|
service, mtype, req, keepReading, err = router.readHeader(cc)
|
2015-12-01 23:32:23 +00:00
|
|
|
if err != nil {
|
|
|
|
if !keepReading {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// discard body
|
2019-01-08 15:38:25 +00:00
|
|
|
cc.ReadBody(nil)
|
2015-12-01 23:32:23 +00:00
|
|
|
return
|
|
|
|
}
|
2015-12-18 01:01:59 +00:00
|
|
|
// is it a streaming request? then we don't read the body
|
|
|
|
if mtype.stream {
|
2020-01-02 18:23:43 +00:00
|
|
|
if cc.(codec.Codec).String() != "grpc" {
|
|
|
|
cc.ReadBody(nil)
|
|
|
|
}
|
2015-12-18 01:01:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-01 23:32:23 +00:00
|
|
|
// Decode the argument value.
|
|
|
|
argIsValue := false // if true, need to indirect before calling.
|
|
|
|
if mtype.ArgType.Kind() == reflect.Ptr {
|
|
|
|
argv = reflect.New(mtype.ArgType.Elem())
|
|
|
|
} else {
|
|
|
|
argv = reflect.New(mtype.ArgType)
|
|
|
|
argIsValue = true
|
|
|
|
}
|
|
|
|
// argv guaranteed to be a pointer now.
|
2019-01-08 15:38:25 +00:00
|
|
|
if err = cc.ReadBody(argv.Interface()); err != nil {
|
2015-12-01 23:32:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if argIsValue {
|
|
|
|
argv = argv.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mtype.stream {
|
|
|
|
replyv = reflect.New(mtype.ReplyType.Elem())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-09 19:28:13 +00:00
|
|
|
func (router *router) readHeader(cc codec.Reader) (service *service, mtype *methodType, req *request, keepReading bool, err error) {
|
2015-12-01 23:32:23 +00:00
|
|
|
// Grab the request header.
|
2019-01-08 15:38:25 +00:00
|
|
|
msg := new(codec.Message)
|
|
|
|
msg.Type = codec.Request
|
2019-01-07 14:44:40 +00:00
|
|
|
req = router.getRequest()
|
2019-01-08 15:38:25 +00:00
|
|
|
req.msg = msg
|
|
|
|
|
|
|
|
err = cc.ReadHeader(msg, msg.Type)
|
2015-12-01 23:32:23 +00:00
|
|
|
if err != nil {
|
|
|
|
req = nil
|
|
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
|
|
|
return
|
|
|
|
}
|
2019-01-07 14:44:40 +00:00
|
|
|
err = errors.New("rpc: router cannot decode request: " + err.Error())
|
2015-12-01 23:32:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We read the header successfully. If we see an error now,
|
|
|
|
// we can still recover and move on to the next request.
|
|
|
|
keepReading = true
|
|
|
|
|
2019-01-10 21:25:31 +00:00
|
|
|
serviceMethod := strings.Split(req.msg.Endpoint, ".")
|
2015-12-01 23:32:23 +00:00
|
|
|
if len(serviceMethod) != 2 {
|
2019-01-11 13:44:47 +00:00
|
|
|
err = errors.New("rpc: service/endpoint request ill-formed: " + req.msg.Endpoint)
|
2015-12-01 23:32:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Look up the request.
|
2019-01-07 14:44:40 +00:00
|
|
|
router.mu.Lock()
|
|
|
|
service = router.serviceMap[serviceMethod[0]]
|
|
|
|
router.mu.Unlock()
|
2015-12-01 23:32:23 +00:00
|
|
|
if service == nil {
|
2019-01-11 13:44:47 +00:00
|
|
|
err = errors.New("rpc: can't find service " + serviceMethod[0])
|
2015-12-01 23:32:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
mtype = service.method[serviceMethod[1]]
|
|
|
|
if mtype == nil {
|
2019-01-11 13:44:47 +00:00
|
|
|
err = errors.New("rpc: can't find method " + serviceMethod[1])
|
2015-12-01 23:32:23 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-01-09 09:06:30 +00:00
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (router *router) NewHandler(h interface{}, opts ...server.HandlerOption) server.Handler {
|
2019-01-09 09:06:30 +00:00
|
|
|
return newRpcHandler(h, opts...)
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (router *router) Handle(h server.Handler) error {
|
2019-01-09 09:06:30 +00:00
|
|
|
router.mu.Lock()
|
|
|
|
defer router.mu.Unlock()
|
|
|
|
if router.serviceMap == nil {
|
|
|
|
router.serviceMap = make(map[string]*service)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(h.Name()) == 0 {
|
|
|
|
return errors.New("rpc.Handle: handler has no name")
|
|
|
|
}
|
|
|
|
if !isExported(h.Name()) {
|
|
|
|
return errors.New("rpc.Handle: type " + h.Name() + " is not exported")
|
|
|
|
}
|
|
|
|
|
|
|
|
rcvr := h.Handler()
|
|
|
|
s := new(service)
|
|
|
|
s.typ = reflect.TypeOf(rcvr)
|
|
|
|
s.rcvr = reflect.ValueOf(rcvr)
|
|
|
|
|
|
|
|
// check name
|
|
|
|
if _, present := router.serviceMap[h.Name()]; present {
|
|
|
|
return errors.New("rpc.Handle: service already defined: " + h.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
s.name = h.Name()
|
|
|
|
s.method = make(map[string]*methodType)
|
|
|
|
|
|
|
|
// Install the methods
|
|
|
|
for m := 0; m < s.typ.NumMethod(); m++ {
|
|
|
|
method := s.typ.Method(m)
|
|
|
|
if mt := prepareMethod(method); mt != nil {
|
|
|
|
s.method[method.Name] = mt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check there are methods
|
|
|
|
if len(s.method) == 0 {
|
|
|
|
return errors.New("rpc Register: type " + s.name + " has no exported methods of suitable type")
|
|
|
|
}
|
|
|
|
|
|
|
|
// save handler
|
|
|
|
router.serviceMap[s.name] = s
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (router *router) ServeRequest(ctx context.Context, r server.Request, rsp server.Response) error {
|
2019-01-09 09:06:30 +00:00
|
|
|
sending := new(sync.Mutex)
|
2019-01-09 16:20:57 +00:00
|
|
|
service, mtype, req, argv, replyv, keepReading, err := router.readRequest(r)
|
2019-01-09 09:06:30 +00:00
|
|
|
if err != nil {
|
|
|
|
if !keepReading {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// send a response if we actually managed to read a header.
|
|
|
|
if req != nil {
|
|
|
|
router.freeRequest(req)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-01-22 13:52:18 +00:00
|
|
|
return service.call(ctx, router, sending, mtype, req, argv, replyv, rsp.Codec())
|
2019-01-09 09:06:30 +00:00
|
|
|
}
|
2019-11-25 16:31:43 +00:00
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (router *router) NewSubscriber(topic string, handler interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
2019-11-25 16:31:43 +00:00
|
|
|
return newSubscriber(topic, handler, opts...)
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (router *router) Subscribe(s server.Subscriber) error {
|
2019-11-25 16:31:43 +00:00
|
|
|
sub, ok := s.(*subscriber)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid subscriber: expected *subscriber")
|
|
|
|
}
|
|
|
|
if len(sub.handlers) == 0 {
|
|
|
|
return fmt.Errorf("invalid subscriber: no handler functions")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateSubscriber(sub); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
router.su.Lock()
|
|
|
|
defer router.su.Unlock()
|
|
|
|
|
|
|
|
// append to subscribers
|
|
|
|
subs := router.subscribers[sub.Topic()]
|
|
|
|
subs = append(subs, sub)
|
|
|
|
router.subscribers[sub.Topic()] = subs
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (router *router) String() string {
|
|
|
|
return "mucp"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (router *router) ProcessMessage(ctx context.Context, msg server.Message) (err error) {
|
2019-12-03 01:36:14 +03:00
|
|
|
defer func() {
|
|
|
|
// recover any panics
|
|
|
|
if r := recover(); r != nil {
|
2020-06-05 10:18:35 +01:00
|
|
|
log.Errorf("panic recovered: %v", r)
|
2020-02-23 16:45:20 +03:00
|
|
|
log.Error(string(debug.Stack()))
|
2019-12-03 01:36:14 +03:00
|
|
|
err = merrors.InternalServerError("go.micro.server", "panic recovered: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-11-25 16:31:43 +00:00
|
|
|
router.su.RLock()
|
|
|
|
// get the subscribers by topic
|
|
|
|
subs, ok := router.subscribers[msg.Topic()]
|
2020-02-26 21:34:40 +03:00
|
|
|
// unlock since we only need to get the subs
|
|
|
|
router.su.RUnlock()
|
2019-11-25 16:31:43 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-03 01:36:14 +03:00
|
|
|
var errResults []string
|
2019-11-25 16:31:43 +00:00
|
|
|
|
|
|
|
// we may have multiple subscribers for the topic
|
|
|
|
for _, sub := range subs {
|
|
|
|
// we may have multiple handlers per subscriber
|
|
|
|
for i := 0; i < len(sub.handlers); i++ {
|
|
|
|
// get the handler
|
|
|
|
handler := sub.handlers[i]
|
|
|
|
|
|
|
|
var isVal bool
|
|
|
|
var req reflect.Value
|
|
|
|
|
|
|
|
// check whether the handler is a pointer
|
|
|
|
if handler.reqType.Kind() == reflect.Ptr {
|
|
|
|
req = reflect.New(handler.reqType.Elem())
|
|
|
|
} else {
|
|
|
|
req = reflect.New(handler.reqType)
|
|
|
|
isVal = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// if its a value get the element
|
|
|
|
if isVal {
|
|
|
|
req = req.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
cc := msg.Codec()
|
|
|
|
|
|
|
|
// read the header. mostly a noop
|
2019-12-03 01:36:14 +03:00
|
|
|
if err = cc.ReadHeader(&codec.Message{}, codec.Event); err != nil {
|
2019-11-25 16:31:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the body into the handler request value
|
2019-12-03 01:36:14 +03:00
|
|
|
if err = cc.ReadBody(req.Interface()); err != nil {
|
2019-11-25 16:31:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the handler which will honour the SubscriberFunc type
|
2020-07-27 13:22:00 +01:00
|
|
|
fn := func(ctx context.Context, msg server.Message) error {
|
2019-11-25 16:31:43 +00:00
|
|
|
var vals []reflect.Value
|
|
|
|
if sub.typ.Kind() != reflect.Func {
|
|
|
|
vals = append(vals, sub.rcvr)
|
|
|
|
}
|
|
|
|
if handler.ctxType != nil {
|
|
|
|
vals = append(vals, reflect.ValueOf(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// values to pass the handler
|
|
|
|
vals = append(vals, reflect.ValueOf(msg.Payload()))
|
|
|
|
|
|
|
|
// execute the actuall call of the handler
|
|
|
|
returnValues := handler.method.Call(vals)
|
2019-12-03 01:36:14 +03:00
|
|
|
if rerr := returnValues[0].Interface(); rerr != nil {
|
|
|
|
err = rerr.(error)
|
2019-11-25 16:31:43 +00:00
|
|
|
}
|
2019-12-03 01:36:14 +03:00
|
|
|
return err
|
2019-11-25 16:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrap with subscriber wrappers
|
|
|
|
for i := len(router.subWrappers); i > 0; i-- {
|
|
|
|
fn = router.subWrappers[i-1](fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new rpc message
|
|
|
|
rpcMsg := &rpcMessage{
|
|
|
|
topic: msg.Topic(),
|
|
|
|
contentType: msg.ContentType(),
|
|
|
|
payload: req.Interface(),
|
|
|
|
codec: msg.(*rpcMessage).codec,
|
|
|
|
header: msg.Header(),
|
|
|
|
body: msg.Body(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute the message handler
|
2019-12-03 01:36:14 +03:00
|
|
|
if err = fn(ctx, rpcMsg); err != nil {
|
|
|
|
errResults = append(errResults, err.Error())
|
2019-11-25 16:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no errors just return
|
2019-12-03 01:36:14 +03:00
|
|
|
if len(errResults) > 0 {
|
|
|
|
err = merrors.InternalServerError("go.micro.server", "subscriber error: %v", strings.Join(errResults, "\n"))
|
2019-11-25 16:31:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 01:36:14 +03:00
|
|
|
return err
|
2019-11-25 16:31:43 +00:00
|
|
|
}
|