Skip to content

Commit

Permalink
remove reflect in grpc server (#1200)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoxinge authored Jun 1, 2021
1 parent d9ee29d commit 39af0bc
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 76 deletions.
17 changes: 12 additions & 5 deletions protocol/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,31 @@ type Client struct {

// NewClient creates a new gRPC client.
func NewClient(url *common.URL) (*Client, error) {
// if global trace instance was set , it means trace function enabled. If not , will return Nooptracer
// If global trace instance was set, it means trace function enabled.
// If not, will return NoopTracer.
tracer := opentracing.GlobalTracer()
dialOpts := make([]grpc.DialOption, 0, 4)
maxMessageSize, _ := strconv.Atoi(url.GetParam(constant.MESSAGE_SIZE_KEY, "4"))

// consumer config client connectTimeout
connectTimeout := config.GetConsumerConfig().ConnectTimeout

dialOpts = append(dialOpts, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(connectTimeout), grpc.WithUnaryInterceptor(
otgrpc.OpenTracingClientInterceptor(tracer, otgrpc.LogPayloads())),
dialOpts = append(dialOpts,
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithTimeout(connectTimeout),
grpc.WithUnaryInterceptor(otgrpc.OpenTracingClientInterceptor(tracer, otgrpc.LogPayloads())),
grpc.WithStreamInterceptor(otgrpc.OpenTracingStreamClientInterceptor(tracer, otgrpc.LogPayloads())),
grpc.WithDefaultCallOptions(
grpc.CallContentSubtype(clientConf.ContentSubType),
grpc.MaxCallRecvMsgSize(1024*1024*maxMessageSize),
grpc.MaxCallSendMsgSize(1024*1024*maxMessageSize)))
grpc.MaxCallSendMsgSize(1024*1024*maxMessageSize),
),
)

conn, err := grpc.Dial(url.Location, dialOpts...)
if err != nil {
logger.Errorf("grpc dail error: %v", err)
logger.Errorf("grpc dial error: %v", err)
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion protocol/grpc/grpc_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (gp *GrpcProtocol) Destroy() {
}
}

// GetProtocol gets gRPC protocol , will create if null.
// GetProtocol gets gRPC protocol, will create if null.
func GetProtocol() protocol.Protocol {
if grpcProtocol == nil {
grpcProtocol = NewGRPCProtocol()
Expand Down
2 changes: 1 addition & 1 deletion protocol/grpc/protoc-gen-dubbo/examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
grpc-gen:
protoc -I ./ helloworld.proto --go_out=plugins=grpc:.
dubbo-gen:
protoc -I ./ helloworld.proto --dubbo_out=plugins=grpc+dubbo:.
protoc -I ./ helloworld.proto --dubbo_out=plugins=grpc+dubbo:.
50 changes: 19 additions & 31 deletions protocol/grpc/protoc-gen-dubbo/examples/helloworld.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions protocol/grpc/protoc-gen-dubbo/examples/helloworld.proto
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
syntax = "proto3";

option java_multiple_files = true;
Expand Down
39 changes: 16 additions & 23 deletions protocol/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package grpc
import (
"fmt"
"net"
"reflect"
"sync"
"time"
)
Expand All @@ -39,6 +38,16 @@ import (
"dubbo.apache.org/dubbo-go/v3/protocol"
)

// DubboGrpcService is gRPC service
type DubboGrpcService interface {
// SetProxyImpl sets proxy.
SetProxyImpl(impl protocol.Invoker)
// GetProxyImpl gets proxy.
GetProxyImpl() protocol.Invoker
// ServiceDesc gets an RPC service's specification.
ServiceDesc() *grpc.ServiceDesc
}

// Server is a gRPC server
type Server struct {
grpcServer *grpc.Server
Expand All @@ -50,16 +59,6 @@ func NewServer() *Server {
return &Server{}
}

// DubboGrpcService is gRPC service
type DubboGrpcService interface {
// SetProxyImpl sets proxy.
SetProxyImpl(impl protocol.Invoker)
// GetProxyImpl gets proxy.
GetProxyImpl() protocol.Invoker
// ServiceDesc gets an RPC service's specification.
ServiceDesc() *grpc.ServiceDesc
}

func (s *Server) SetBufferSize(n int) {
s.bufferSize = n
}
Expand All @@ -76,12 +75,15 @@ func (s *Server) Start(url *common.URL) {
panic(err)
}

// if global trace instance was set, then server tracer instance can be get. If not , will return Nooptracer
// If global trace instance was set, then server tracer instance
// can be get. If not, will return NoopTracer.
tracer := opentracing.GlobalTracer()
server := grpc.NewServer(
grpc.UnaryInterceptor(otgrpc.OpenTracingServerInterceptor(tracer)),
grpc.StreamInterceptor(otgrpc.OpenTracingStreamServerInterceptor(tracer)),
grpc.MaxRecvMsgSize(1024*1024*s.bufferSize),
grpc.MaxSendMsgSize(1024*1024*s.bufferSize))
grpc.MaxSendMsgSize(1024*1024*s.bufferSize),
)
s.grpcServer = server

go func() {
Expand Down Expand Up @@ -136,18 +138,12 @@ func waitGrpcExporter(providerServices map[string]*config.ServiceConfig) {
func registerService(providerServices map[string]*config.ServiceConfig, server *grpc.Server) {
for key, providerService := range providerServices {
service := config.GetProviderService(key)

ds, ok := service.(DubboGrpcService)
if !ok {
panic("illegal service type registered")
}

m, ok := reflect.TypeOf(service).MethodByName("SetProxyImpl")
if !ok {
panic("method SetProxyImpl is necessary for grpc service")
}
serviceKey := common.ServiceKey(providerService.InterfaceName, providerService.Group, providerService.Version)

exporter, _ := grpcProtocol.ExporterMap().Load(serviceKey)
if exporter == nil {
panic(fmt.Sprintf("no exporter found for servicekey: %v", serviceKey))
Expand All @@ -156,12 +152,9 @@ func registerService(providerServices map[string]*config.ServiceConfig, server *
if invoker == nil {
panic(fmt.Sprintf("no invoker found for servicekey: %v", serviceKey))
}
in := []reflect.Value{reflect.ValueOf(service)}
in = append(in, reflect.ValueOf(invoker))
m.Func.Call(in)

ds.SetProxyImpl(invoker)
server.RegisterService(ds.ServiceDesc(), service)

}
}

Expand Down

0 comments on commit 39af0bc

Please sign in to comment.