Skip to content

Commit

Permalink
feat:Solving Circular Dependency in Dubbo-Go (apache#2576)
Browse files Browse the repository at this point in the history
* Solving Circular Dependency in Dubbo-Go

* fix test

* modify comment

* modify comment

* fix test
  • Loading branch information
FinalT authored Jan 14, 2024
1 parent 61933d0 commit 2489243
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 26 deletions.
35 changes: 35 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
@@ -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
)
38 changes: 38 additions & 0 deletions internal/reflection/api.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions protocol/triple/health/healthServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 5 additions & 21 deletions protocol/triple/reflection/serverreflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
//
Expand All @@ -79,7 +62,7 @@ func NewServer() *ReflectionServer {
}

type ReflectionServer struct {
s ServiceInfoProvider
s reflection.ServiceInfoProvider
descResolver protodesc.Resolver
extResolver ExtensionResolver
}
Expand Down Expand Up @@ -279,6 +262,7 @@ var reflectionServer *ReflectionServer

func init() {
reflectionServer = NewServer()
internal.ReflectionRegister = Register
server.SetProServices(&server.ServiceDefinition{
Handler: reflectionServer,
Info: &rpb.ServerReflection_ServiceInfo,
Expand All @@ -290,6 +274,6 @@ func init() {
config.SetProviderServiceWithInfo(reflectionServer, &rpb.ServerReflection_ServiceInfo)
}

func Register(s ServiceInfoProvider) {
func Register(s reflection.ServiceInfoProvider) {
reflectionServer.s = s
}
4 changes: 2 additions & 2 deletions protocol/triple/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions protocol/triple/triple.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 2489243

Please sign in to comment.