Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove reflect in grpc server #1200

Merged
merged 1 commit into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -38,6 +37,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 @@ -49,16 +58,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 @@ -75,12 +74,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 @@ -134,18 +136,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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 'ds' may be nil ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 'service' may be nil ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 @@ -154,12 +150,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