From 2489243b2b5ec3f964ea792be107b16d808051ca Mon Sep 17 00:00:00 2001 From: finalt Date: Sun, 14 Jan 2024 15:32:47 +0800 Subject: [PATCH] feat:Solving Circular Dependency in Dubbo-Go (#2576) * Solving Circular Dependency in Dubbo-Go * fix test * modify comment * modify comment * fix test --- internal/internal.go | 35 +++++++++++++++++ internal/reflection/api.go | 38 +++++++++++++++++++ protocol/triple/health/healthServer.go | 4 ++ .../triple/reflection/serverreflection.go | 26 +++---------- protocol/triple/server.go | 4 +- protocol/triple/triple.go | 6 +-- 6 files changed, 87 insertions(+), 26 deletions(-) create mode 100644 internal/internal.go create mode 100644 internal/reflection/api.go diff --git a/internal/internal.go b/internal/internal.go new file mode 100644 index 0000000000..983a28ddbd --- /dev/null +++ b/internal/internal.go @@ -0,0 +1,35 @@ +/* + * 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. + */ + +// Package internal contains dubbo-go-internal code, to avoid polluting +// the top-level dubbo-go package. It must not import any dubbo-go symbols +// except internal symbols to avoid circular dependencies. +package internal + +import ( + "dubbo.apache.org/dubbo-go/v3/internal/reflection" +) + +var ( + // HealthSetServingStatusServing is used to set service serving status + // the initialization place is in /protocol/triple/health/healthServer.go + HealthSetServingStatusServing = func(service string) {} + // ReflectionRegister is used to register reflection service provider + // the initialization place is in /protocol/triple/reflection/serverreflection.go + ReflectionRegister = func(reflection reflection.ServiceInfoProvider) {} + // todo: add metadata func +) diff --git a/internal/reflection/api.go b/internal/reflection/api.go new file mode 100644 index 0000000000..0f7bfd7aad --- /dev/null +++ b/internal/reflection/api.go @@ -0,0 +1,38 @@ +/* + * 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. + */ + +package reflection + +import ( + "google.golang.org/grpc" +) + +// ServiceInfoProvider is an interface used to retrieve metadata about the +// services to expose. +// +// The reflection service is only interested in the service names, but the +// signature is this way so that *grpc.Server implements it. So it is okay +// for a custom implementation to return zero values for the +// grpc.ServiceInfo values in the map. +// +// # Experimental +// +// Notice: This type is EXPERIMENTAL and may be changed or removed in a +// later release. +type ServiceInfoProvider interface { + GetServiceInfo() map[string]grpc.ServiceInfo +} diff --git a/protocol/triple/health/healthServer.go b/protocol/triple/health/healthServer.go index af2e69e777..aca420d27d 100644 --- a/protocol/triple/health/healthServer.go +++ b/protocol/triple/health/healthServer.go @@ -32,6 +32,7 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/config" + "dubbo.apache.org/dubbo-go/v3/internal" "dubbo.apache.org/dubbo-go/v3/protocol/triple/health/triple_health" "dubbo.apache.org/dubbo-go/v3/server" ) @@ -171,6 +172,9 @@ func (srv *HealthTripleServer) Resume() { func init() { healthServer = NewServer() + + internal.HealthSetServingStatusServing = SetServingStatusServing + server.SetProServices(&server.ServiceDefinition{ Handler: healthServer, Info: &triple_health.Health_ServiceInfo, diff --git a/protocol/triple/reflection/serverreflection.go b/protocol/triple/reflection/serverreflection.go index ae7bd09f57..dff18213b7 100644 --- a/protocol/triple/reflection/serverreflection.go +++ b/protocol/triple/reflection/serverreflection.go @@ -25,12 +25,9 @@ import ( ) import ( - "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "google.golang.org/protobuf/proto" - "google.golang.org/protobuf/reflect/protodesc" "google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoregistry" @@ -39,26 +36,12 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/config" + "dubbo.apache.org/dubbo-go/v3/internal" + "dubbo.apache.org/dubbo-go/v3/internal/reflection" rpb "dubbo.apache.org/dubbo-go/v3/protocol/triple/reflection/triple_reflection" "dubbo.apache.org/dubbo-go/v3/server" ) -// ServiceInfoProvider is an interface used to retrieve metadata about the -// services to expose. -// -// The reflection service is only interested in the service names, but the -// signature is this way so that *grpc.Server implements it. So it is okay -// for a custom implementation to return zero values for the -// grpc.ServiceInfo values in the map. -// -// # Experimental -// -// Notice: This type is EXPERIMENTAL and may be changed or removed in a -// later release. -type ServiceInfoProvider interface { - GetServiceInfo() map[string]grpc.ServiceInfo -} - // ExtensionResolver is the interface used to query details about extensions. // This interface is satisfied by protoregistry.GlobalTypes. // @@ -79,7 +62,7 @@ func NewServer() *ReflectionServer { } type ReflectionServer struct { - s ServiceInfoProvider + s reflection.ServiceInfoProvider descResolver protodesc.Resolver extResolver ExtensionResolver } @@ -279,6 +262,7 @@ var reflectionServer *ReflectionServer func init() { reflectionServer = NewServer() + internal.ReflectionRegister = Register server.SetProServices(&server.ServiceDefinition{ Handler: reflectionServer, Info: &rpb.ServerReflection_ServiceInfo, @@ -290,6 +274,6 @@ func init() { config.SetProviderServiceWithInfo(reflectionServer, &rpb.ServerReflection_ServiceInfo) } -func Register(s ServiceInfoProvider) { +func Register(s reflection.ServiceInfoProvider) { reflectionServer.s = s } diff --git a/protocol/triple/server.go b/protocol/triple/server.go index 70ca805a85..1a403cc8c7 100644 --- a/protocol/triple/server.go +++ b/protocol/triple/server.go @@ -38,10 +38,10 @@ import ( "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/config" + "dubbo.apache.org/dubbo-go/v3/internal" "dubbo.apache.org/dubbo-go/v3/protocol" "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3" "dubbo.apache.org/dubbo-go/v3/protocol/invocation" - "dubbo.apache.org/dubbo-go/v3/protocol/triple/reflection" tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol" "dubbo.apache.org/dubbo-go/v3/server" ) @@ -94,7 +94,7 @@ func (s *Server) Start(invoker protocol.Invoker, info *server.ServiceInfo) { // old triple idl mode and non-idl mode s.compatHandleService(intfName, URL.Group(), URL.Version(), hanOpts...) } - reflection.Register(s) + internal.ReflectionRegister(s) go func() { if runErr := s.triServer.Run(); runErr != nil { diff --git a/protocol/triple/triple.go b/protocol/triple/triple.go index 74bd772b83..9ee503ff96 100644 --- a/protocol/triple/triple.go +++ b/protocol/triple/triple.go @@ -29,8 +29,8 @@ import ( "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" + "dubbo.apache.org/dubbo-go/v3/internal" "dubbo.apache.org/dubbo-go/v3/protocol" - "dubbo.apache.org/dubbo-go/v3/protocol/triple/health" "dubbo.apache.org/dubbo-go/v3/server" ) @@ -66,7 +66,7 @@ func (tp *TripleProtocol) Export(invoker protocol.Invoker) protocol.Exporter { tp.SetExporterMap(serviceKey, exporter) logger.Infof("[TRIPLE Protocol] Export service: %s", url.String()) tp.openServer(invoker, info) - health.SetServingStatusServing(url.Service()) + internal.HealthSetServingStatusServing(url.Service()) return exporter } @@ -80,7 +80,7 @@ func (tp *TripleProtocol) exportForTest(invoker protocol.Invoker, info *server.S tp.SetExporterMap(serviceKey, exporter) logger.Infof("[TRIPLE Protocol] Export service: %s", url.String()) tp.openServer(invoker, info) - health.SetServingStatusServing(url.Service()) + internal.HealthSetServingStatusServing(url.Service()) return exporter }