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

Use class name as the default reference name #1339

Merged
merged 24 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
518be2b
build(deps): bump actions/cache from v2.1.4 to v2.1.5
dependabot[bot] Apr 19, 2021
3162e41
Merge pull request #1162 from apache/dependabot/github_actions/develo…
AlexStocks Apr 21, 2021
2138190
Merge branch 'develop' of https://github.com/apache/dubbo-go into dev…
AlexStocks Apr 27, 2021
bef8e95
Merge branch '3.0' into develop
AlexStocks Apr 27, 2021
a85b65b
Merge branch '3.0' into develop
AlexStocks May 9, 2021
9e38afe
Merge branch '3.0' into develop
AlexStocks May 10, 2021
f0ad730
Merge branch '3.0' into develop
AlexStocks May 10, 2021
4cb6e44
improve etcd version and change create to put (#1203)
ztelur May 15, 2021
56d9d71
Merge branch '3.0' into develop
AlexStocks May 15, 2021
cc74aa5
Merge branch '3.0' into develop
AlexStocks May 18, 2021
40082d4
Merge branch '3.0' into develop
AlexStocks May 21, 2021
63c7c59
Merge pull request #2 from apache/master
alchemy-lee Jun 3, 2021
421ad54
Merge pull request #3 from apache/3.0
alchemy-lee Jun 3, 2021
7b0682f
Merge pull request #4 from apache/3.0
alchemy-lee Jul 23, 2021
e73e49b
Remove RPC Service
alchemy-lee Jul 23, 2021
047402e
use type assertion before reflect
alchemy-lee Jul 26, 2021
fb68862
modify comment
alchemy-lee Jul 26, 2021
99ec546
Merge pull request #5 from apache/3.0
alchemy-lee Jul 26, 2021
df0e23e
modify comment of BaseMetadataService
alchemy-lee Jul 28, 2021
0c966ad
Merge remote-tracking branch 'origin/rm-rpc' into rm-rpc
alchemy-lee Jul 28, 2021
3d85e2b
add the type alias of interface{}
alchemy-lee Jul 30, 2021
e441a84
Merge pull request #6 from apache/3.0
alchemy-lee Jul 30, 2021
97f25a9
modify RPCService
alchemy-lee Aug 2, 2021
a2f1a2f
Merge remote-tracking branch 'origin/rm-rpc' into rm-rpc
alchemy-lee Aug 2, 2021
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
11 changes: 5 additions & 6 deletions common/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/protocol"
Expand All @@ -38,7 +37,7 @@ import (

// nolint
type Proxy struct {
rpc common.RPCService
rpc interface{}
invoke protocol.Invoker
callback interface{}
attachments map[string]string
Expand All @@ -50,7 +49,7 @@ type (
// ProxyOption a function to init Proxy with options
ProxyOption func(p *Proxy)
// ImplementFunc function for proxy impl of RPCService functions
ImplementFunc func(p *Proxy, v common.RPCService)
ImplementFunc func(p *Proxy, v interface{})
)

var typError = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem()).Type()
Expand Down Expand Up @@ -87,15 +86,15 @@ func WithProxyImplementFunc(f ImplementFunc) ProxyOption {
// type XxxProvider struct {
// Yyy func(ctx context.Context, args []interface{}, rsp *Zzz) error
// }
func (p *Proxy) Implement(v common.RPCService) {
func (p *Proxy) Implement(v interface{}) {
p.once.Do(func() {
p.implement(p, v)
p.rpc = v
})
}

// Get gets rpc service instance.
func (p *Proxy) Get() common.RPCService {
func (p *Proxy) Get() interface{} {
return p.rpc
}

Expand All @@ -110,7 +109,7 @@ func (p *Proxy) GetInvoker() protocol.Invoker {
}

// DefaultProxyImplementFunc the default function for proxy impl
func DefaultProxyImplementFunc(p *Proxy, v common.RPCService) {
func DefaultProxyImplementFunc(p *Proxy, v interface{}) {
alchemy-lee marked this conversation as resolved.
Show resolved Hide resolved
// check parameters, incoming interface must be a elem's pointer.
valueOf := reflect.ValueOf(v)
logger.Debugf("[Implement] reflect.TypeOf: %s", valueOf.String())
Expand Down
28 changes: 27 additions & 1 deletion common/rpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ type RPCService interface {
Reference() string
}

// GetReference return the reference id of the service.
// If the service implemented the RPCService interface,
// it will call the Reference method. If not, it will
// return the struct name as the reference id.
func GetReference(service interface{}) string {
AlexStocks marked this conversation as resolved.
Show resolved Hide resolved
if s, ok := service.(RPCService); ok {
return s.Reference()
}

ref := ""
sType := reflect.TypeOf(service)
kind := sType.Kind()
switch kind {
case reflect.Struct:
ref = sType.Name()
case reflect.Ptr:
sName := sType.Elem().Name()
if sName != "" {
ref = sName
} else {
ref = sType.Elem().Field(0).Name
}
}
return ref
}

// AsyncCallbackService callback interface for async
type AsyncCallbackService interface {
// Callback: callback
Expand Down Expand Up @@ -186,7 +212,7 @@ func (sm *serviceMap) GetInterface(interfaceName string) []*Service {
}

// Register registers a service by @interfaceName and @protocol
func (sm *serviceMap) Register(interfaceName, protocol, group, version string, rcvr RPCService) (string, error) {
func (sm *serviceMap) Register(interfaceName, protocol, group, version string, rcvr interface{}) (string, error) {
if sm.serviceMap[protocol] == nil {
sm.serviceMap[protocol] = make(map[string]*Service)
}
Expand Down
33 changes: 33 additions & 0 deletions common/rpc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,36 @@ func TestSuiteMethod(t *testing.T) {
methodType = suiteMethod(method)
assert.Nil(t, methodType)
}

type ServiceWithoutRef struct{}

func TestGetReference(t *testing.T) {
s0 := &TestService{}
ref0 := GetReference(s0)
assert.Equal(t, referenceTestPath, ref0)

//s1 := TestService{}
//ref1 := GetReference(s1)
//assert.Equal(t, referenceTestPath, ref1)

s2 := &struct {
TestService
}{}
ref2 := GetReference(s2)
assert.Equal(t, referenceTestPath, ref2)

expectedReference := "ServiceWithoutRef"
s3 := &ServiceWithoutRef{}
ref3 := GetReference(s3)
assert.Equal(t, expectedReference, ref3)

s4 := ServiceWithoutRef{}
ref4 := GetReference(s4)
assert.Equal(t, expectedReference, ref4)

s5 := &struct {
ServiceWithoutRef
}{}
ref5 := GetReference(s5)
assert.Equal(t, expectedReference, ref5)
}
7 changes: 4 additions & 3 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,14 @@ func LoadWithOptions(options ...LoaderInitOption) {
}

// GetRPCService get rpc service for consumer
func GetRPCService(name string) common.RPCService {
func GetRPCService(name string) interface{} {
return consumerConfig.References[name].GetRPCService()
}

// RPCService create rpc service for consumer
func RPCService(service common.RPCService) {
consumerConfig.References[service.Reference()].Implement(service)
func RPCService(service interface{}) {
ref := common.GetReference(service)
consumerConfig.References[ref].Implement(service)
}

// GetMetricConfig find the MetricConfig
Expand Down
12 changes: 6 additions & 6 deletions config/config_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func TestLoad(t *testing.T) {
RPCService(ms2)
assert.NotEqual(t, ms2, GetRPCService(ms2.Reference()))

conServices = map[string]common.RPCService{}
proServices = map[string]common.RPCService{}
conServices = map[string]interface{}{}
proServices = map[string]interface{}{}
err := common.ServiceMap.UnRegister("com.MockService", "mock",
common.ServiceKey("com.MockService", "huadong_idc", "1.0.0"))
assert.Nil(t, err)
Expand Down Expand Up @@ -154,8 +154,8 @@ func TestLoadWithSingleReg(t *testing.T) {
RPCService(ms2)
assert.NotEqual(t, ms2, GetRPCService(ms2.Reference()))

conServices = map[string]common.RPCService{}
proServices = map[string]common.RPCService{}
conServices = map[string]interface{}{}
proServices = map[string]interface{}{}
common.ServiceMap.UnRegister("com.MockService", "mock", common.ServiceKey("com.MockService", "huadong_idc", "1.0.0"))
consumerConfig = nil
providerConfig = nil
Expand Down Expand Up @@ -193,8 +193,8 @@ func TestWithNoRegLoad(t *testing.T) {
RPCService(ms2)
assert.NotEqual(t, ms2, GetRPCService(ms2.Reference()))

conServices = map[string]common.RPCService{}
proServices = map[string]common.RPCService{}
conServices = map[string]interface{}{}
proServices = map[string]interface{}{}
err := common.ServiceMap.UnRegister("com.MockService", "mock",
common.ServiceKey("com.MockService", "huadong_idc", "1.0.0"))
assert.Nil(t, err)
Expand Down
4 changes: 2 additions & 2 deletions config/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ func (c *ReferenceConfig) Refer(_ interface{}) {

// Implement
// @v is service provider implemented RPCService
func (c *ReferenceConfig) Implement(v common.RPCService) {
func (c *ReferenceConfig) Implement(v interface{}) {
c.pxy.Implement(v)
}

// GetRPCService gets RPCService from proxy
func (c *ReferenceConfig) GetRPCService() common.RPCService {
func (c *ReferenceConfig) GetRPCService() interface{} {
return c.pxy.Get()
}

Expand Down
20 changes: 11 additions & 9 deletions config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,34 @@ import (
)

var (
conServices = map[string]common.RPCService{} // service name -> service
proServices = map[string]common.RPCService{} // service name -> service
conServices = map[string]interface{}{} // service name -> service
proServices = map[string]interface{}{} // service name -> service
)

// SetConsumerService is called by init() of implement of RPCService
func SetConsumerService(service common.RPCService) {
conServices[service.Reference()] = service
func SetConsumerService(service interface{}) {
ref := common.GetReference(service)
conServices[ref] = service
}

// SetProviderService is called by init() of implement of RPCService
func SetProviderService(service common.RPCService) {
proServices[service.Reference()] = service
func SetProviderService(service interface{}) {
ref := common.GetReference(service)
proServices[ref] = service
}

// GetConsumerService gets ConsumerService by @name
func GetConsumerService(name string) common.RPCService {
func GetConsumerService(name string) interface{} {
return conServices[name]
}

// GetProviderService gets ProviderService by @name
func GetProviderService(name string) common.RPCService {
func GetProviderService(name string) interface{} {
return proServices[name]
}

// GetAllProviderService gets all ProviderService
func GetAllProviderService() map[string]common.RPCService {
func GetAllProviderService() map[string]interface{} {
return proServices
}

Expand Down
4 changes: 2 additions & 2 deletions config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type ServiceConfig struct {
unexported *atomic.Bool
exported *atomic.Bool
export bool // a flag to control whether the current service should export or not
rpcService common.RPCService
rpcService interface{}
cacheMutex sync.Mutex
cacheProtocol protocol.Protocol

Expand Down Expand Up @@ -267,7 +267,7 @@ func (c *ServiceConfig) Unexport() {
}

// Implement only store the @s and return
func (c *ServiceConfig) Implement(s common.RPCService) {
func (c *ServiceConfig) Implement(s interface{}) {
c.rpcService = s
}

Expand Down
2 changes: 1 addition & 1 deletion metadata/service/local_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (mts *BaseMetadataService) ServiceName() (string, error) {
return mts.serviceName, nil
}

// Version will return the version of metadata service
// Reference will return the reference id of metadata service
func (mts *BaseMetadataService) Reference() string {
return constant.SIMPLE_METADATA_SERVICE_NAME
}
Expand Down