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"
|
2020-09-20 16:08:45 +03:00
|
|
|
"fmt"
|
2019-06-03 18:44:43 +01:00
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2020-09-20 16:08:45 +03:00
|
|
|
"github.com/unistack-org/micro/v3/server"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
2021-04-26 19:04:27 +03:00
|
|
|
// Precompute the reflect type for error. Can't use error directly
|
|
|
|
// because Typeof takes an empty interface value. This is annoying.
|
|
|
|
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
|
2019-06-03 18:44:43 +01:00
|
|
|
|
|
|
|
type methodType struct {
|
|
|
|
ArgType reflect.Type
|
|
|
|
ReplyType reflect.Type
|
|
|
|
ContextType reflect.Type
|
2021-04-26 19:04:27 +03:00
|
|
|
method reflect.Method
|
2019-06-03 18:44:43 +01:00
|
|
|
stream bool
|
|
|
|
}
|
|
|
|
|
2021-04-26 19:04:27 +03:00
|
|
|
// type reflectionType func(context.Context, server.Stream) error
|
2020-09-20 16:08:45 +03:00
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
type service struct {
|
2021-04-26 19:04:27 +03:00
|
|
|
typ reflect.Type
|
|
|
|
method map[string]*methodType
|
|
|
|
rcvr reflect.Value
|
|
|
|
name string
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// server represents an RPC Server.
|
|
|
|
type rServer struct {
|
|
|
|
serviceMap map[string]*service
|
2021-04-26 19:04:27 +03:00
|
|
|
mu sync.RWMutex
|
|
|
|
// reflection bool
|
2019-06-03 18:44:43 +01: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() == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareEndpoint() returns a methodType for the provided method or nil
|
|
|
|
// in case if the method was unsuitable.
|
2020-11-05 00:50:40 +03:00
|
|
|
func prepareEndpoint(method reflect.Method) (*methodType, error) {
|
2019-06-03 18:44:43 +01:00
|
|
|
mtype := method.Type
|
|
|
|
mname := method.Name
|
|
|
|
var replyType, argType, contextType reflect.Type
|
|
|
|
var stream bool
|
|
|
|
|
|
|
|
// Endpoint() must be exported.
|
|
|
|
if method.PkgPath != "" {
|
2020-11-05 00:50:40 +03:00
|
|
|
return nil, fmt.Errorf("Endpoint must be exported")
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-05 00:50:40 +03:00
|
|
|
return nil, fmt.Errorf("method %v of %v has wrong number of ins: %v", mname, mtype, mtype.NumIn())
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
2021-04-26 19:04:27 +03:00
|
|
|
switch stream {
|
|
|
|
case true:
|
2019-06-03 18:44:43 +01:00
|
|
|
// check stream type
|
|
|
|
streamType := reflect.TypeOf((*server.Stream)(nil)).Elem()
|
|
|
|
if !argType.Implements(streamType) {
|
2020-11-05 00:50:40 +03:00
|
|
|
return nil, fmt.Errorf("%v argument does not implement Streamer interface: %v", mname, argType)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
2021-04-26 19:04:27 +03:00
|
|
|
default:
|
2019-06-03 18:44:43 +01:00
|
|
|
// First arg need not be a pointer.
|
|
|
|
if !isExportedOrBuiltinType(argType) {
|
2020-11-05 00:50:40 +03:00
|
|
|
return nil, fmt.Errorf("%v argument type not exported: %v", mname, argType)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if replyType.Kind() != reflect.Ptr {
|
2020-11-05 00:50:40 +03:00
|
|
|
return nil, fmt.Errorf("method %v reply type not a pointer: %v", mname, replyType)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reply type must be exported.
|
|
|
|
if !isExportedOrBuiltinType(replyType) {
|
2020-11-05 00:50:40 +03:00
|
|
|
return nil, fmt.Errorf("method %v reply type not exported: %v", mname, replyType)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Endpoint() needs one out.
|
|
|
|
if mtype.NumOut() != 1 {
|
2020-11-05 00:50:40 +03:00
|
|
|
return nil, fmt.Errorf("method %v has wrong number of outs: %v", mname, mtype.NumOut())
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
// The return type of the method must be error.
|
|
|
|
if returnType := mtype.Out(0); returnType != typeOfError {
|
2020-11-05 00:50:40 +03:00
|
|
|
return nil, fmt.Errorf("method %v returns %v not error", mname, returnType.String())
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
2020-11-05 00:50:40 +03:00
|
|
|
return &methodType{method: method, ArgType: argType, ReplyType: replyType, ContextType: contextType, stream: stream}, nil
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *rServer) register(rcvr interface{}) error {
|
|
|
|
server.mu.Lock()
|
|
|
|
defer server.mu.Unlock()
|
|
|
|
if server.serviceMap == nil {
|
|
|
|
server.serviceMap = make(map[string]*service)
|
|
|
|
}
|
2020-09-20 16:08:45 +03:00
|
|
|
s := &service{}
|
2019-06-03 18:44:43 +01:00
|
|
|
s.typ = reflect.TypeOf(rcvr)
|
|
|
|
s.rcvr = reflect.ValueOf(rcvr)
|
|
|
|
sname := reflect.Indirect(s.rcvr).Type().Name()
|
|
|
|
if sname == "" {
|
2020-09-20 16:08:45 +03:00
|
|
|
return fmt.Errorf("rpc: no service name for type %v", s.typ.String())
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
if !isExported(sname) {
|
2020-11-05 00:50:40 +03:00
|
|
|
return fmt.Errorf("rpc Register: type %s is not exported", sname)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
if _, present := server.serviceMap[sname]; present {
|
2020-09-20 16:08:45 +03:00
|
|
|
return fmt.Errorf("rpc: service already defined: " + sname)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
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)
|
2020-11-05 00:50:40 +03:00
|
|
|
mt, err := prepareEndpoint(method)
|
|
|
|
if mt != nil && err == nil {
|
2019-06-03 18:44:43 +01:00
|
|
|
s.method[method.Name] = mt
|
2020-11-05 00:50:40 +03:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.method) == 0 {
|
2020-11-05 00:50:40 +03:00
|
|
|
return fmt.Errorf("rpc Register: type %s has no exported methods of suitable type", sname)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|