2019-06-03 18:44:43 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
// 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 (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/server"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// 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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// server represents an RPC Server.
|
|
|
|
type rServer struct {
|
|
|
|
mu sync.Mutex // protects the serviceMap
|
|
|
|
serviceMap map[string]*service
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareEndpoint() returns a methodType for the provided method or nil
|
|
|
|
// in case if the method was unsuitable.
|
|
|
|
func prepareEndpoint(method reflect.Method) *methodType {
|
|
|
|
mtype := method.Type
|
|
|
|
mname := method.Name
|
|
|
|
var replyType, argType, contextType reflect.Type
|
|
|
|
var stream bool
|
|
|
|
|
|
|
|
// Endpoint() must be exported.
|
|
|
|
if method.PkgPath != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch mtype.NumIn() {
|
|
|
|
case 3:
|
|
|
|
// assuming streaming
|
|
|
|
argType = mtype.In(2)
|
|
|
|
contextType = mtype.In(1)
|
|
|
|
stream = true
|
|
|
|
case 4:
|
|
|
|
// method that takes a context
|
|
|
|
argType = mtype.In(2)
|
|
|
|
replyType = mtype.In(3)
|
|
|
|
contextType = mtype.In(1)
|
|
|
|
default:
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
2020-06-05 10:18:35 +01:00
|
|
|
logger.Errorf("method %v of %v has wrong number of ins: %v", mname, mtype, mtype.NumIn())
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if stream {
|
|
|
|
// check stream type
|
|
|
|
streamType := reflect.TypeOf((*server.Stream)(nil)).Elem()
|
|
|
|
if !argType.Implements(streamType) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
2020-06-05 10:18:35 +01:00
|
|
|
logger.Errorf("%v argument does not implement Streamer interface: %v", mname, argType)
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if not stream check the replyType
|
|
|
|
|
|
|
|
// First arg need not be a pointer.
|
|
|
|
if !isExportedOrBuiltinType(argType) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
2020-06-05 10:18:35 +01:00
|
|
|
logger.Errorf("%v argument type not exported: %v", mname, argType)
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if replyType.Kind() != reflect.Ptr {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
2020-06-05 10:18:35 +01:00
|
|
|
logger.Errorf("method %v reply type not a pointer: %v", mname, replyType)
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reply type must be exported.
|
|
|
|
if !isExportedOrBuiltinType(replyType) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
2020-06-05 10:18:35 +01:00
|
|
|
logger.Errorf("method %v reply type not exported: %v", mname, replyType)
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Endpoint() needs one out.
|
|
|
|
if mtype.NumOut() != 1 {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
2020-06-05 10:18:35 +01:00
|
|
|
logger.Errorf("method %v has wrong number of outs: %v", mname, mtype.NumOut())
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// The return type of the method must be error.
|
|
|
|
if returnType := mtype.Out(0); returnType != typeOfError {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
2020-06-05 10:18:35 +01:00
|
|
|
logger.Errorf("method %v returns %v not error", mname, returnType.String())
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &methodType{method: method, ArgType: argType, ReplyType: replyType, ContextType: contextType, stream: stream}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *rServer) register(rcvr interface{}) error {
|
|
|
|
server.mu.Lock()
|
|
|
|
defer server.mu.Unlock()
|
|
|
|
if server.serviceMap == nil {
|
|
|
|
server.serviceMap = make(map[string]*service)
|
|
|
|
}
|
|
|
|
s := new(service)
|
|
|
|
s.typ = reflect.TypeOf(rcvr)
|
|
|
|
s.rcvr = reflect.ValueOf(rcvr)
|
|
|
|
sname := reflect.Indirect(s.rcvr).Type().Name()
|
|
|
|
if sname == "" {
|
2020-06-05 10:18:35 +01:00
|
|
|
logger.Fatalf("rpc: no service name for type %v", s.typ.String())
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
if !isExported(sname) {
|
|
|
|
s := "rpc Register: type " + sname + " is not exported"
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(s)
|
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
return errors.New(s)
|
|
|
|
}
|
|
|
|
if _, present := server.serviceMap[sname]; present {
|
|
|
|
return errors.New("rpc: service already defined: " + sname)
|
|
|
|
}
|
|
|
|
s.name = sname
|
|
|
|
s.method = make(map[string]*methodType)
|
|
|
|
|
|
|
|
// Install the methods
|
|
|
|
for m := 0; m < s.typ.NumMethod(); m++ {
|
|
|
|
method := s.typ.Method(m)
|
|
|
|
if mt := prepareEndpoint(method); mt != nil {
|
|
|
|
s.method[method.Name] = mt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.method) == 0 {
|
|
|
|
s := "rpc Register: type " + sname + " has no exported methods of suitable type"
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(s)
|
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
return errors.New(s)
|
|
|
|
}
|
|
|
|
server.serviceMap[s.name] = s
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *methodType) prepareContext(ctx context.Context) reflect.Value {
|
|
|
|
if contextv := reflect.ValueOf(ctx); contextv.IsValid() {
|
|
|
|
return contextv
|
|
|
|
}
|
|
|
|
return reflect.Zero(m.ContextType)
|
|
|
|
}
|