add the sanity check back and refactor

This commit is contained in:
iamqizhao 2015-06-12 17:31:53 -07:00
parent 7b318285c9
commit 9738b8b68c
1 changed files with 10 additions and 1 deletions

View File

@ -38,6 +38,7 @@ import (
"fmt"
"io"
"net"
"reflect"
"strings"
"sync"
@ -138,9 +139,17 @@ func NewServer(opt ...ServerOption) *Server {
// server. Called from the IDL generated code. This must be called before
// invoking Serve.
func (s *Server) RegisterService(sd *ServiceDesc, ss interface{}) {
ht := reflect.TypeOf(sd.HandlerType).Elem()
st := reflect.TypeOf(ss)
if !st.Implements(ht) {
grpclog.Fatalf("grpc: Server.RegisterService found the handler of type %v that does not satisfy %v", st, ht)
}
s.register(sd, ss)
}
func (s *Server) register(sd *ServiceDesc, ss interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
// Does some sanity checks.
if _, ok := s.m[sd.ServiceName]; ok {
grpclog.Fatalf("grpc: Server.RegisterService found duplicate service registration for %q", sd.ServiceName)
}