Skip to content

Commit

Permalink
feat: copy bare configurations from config to global (#2413)
Browse files Browse the repository at this point in the history
  • Loading branch information
DMwangnima authored Sep 2, 2023
1 parent 4ceaaa9 commit 38e57c7
Show file tree
Hide file tree
Showing 18 changed files with 438 additions and 242 deletions.
56 changes: 56 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package client

import (
"context"
commonCfg "dubbo.apache.org/dubbo-go/v3/common/config"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/global"
)

import (
Expand All @@ -35,6 +38,8 @@ type Client struct {
invoker protocol.Invoker
info *ClientInfo
cfg *ReferenceConfig

cliOpts *ClientOptions
}

type ClientInfo struct {
Expand All @@ -44,6 +49,57 @@ type ClientInfo struct {
Meta map[string]interface{}
}

func (cli *Client) init(opts ...global.ReferenceOption) error {
refCfg := global.DefaultReferenceConfig()
for _, opt := range opts {
opt(refCfg)
}

// init method
if length := len(refCfg.Methods); length > 0 {
cli.methodsCompat = make([]*config.MethodConfig, length)

Check failure on line 60 in client/client.go

View workflow job for this annotation

GitHub Actions / lint (1.17)

cli.methodsCompat undefined (type *Client has no field or method methodsCompat) (typecheck)
for i, method := range refCfg.Methods {
cli.methodsCompat[i] = compatMethodConfig(method)

Check failure on line 62 in client/client.go

View workflow job for this annotation

GitHub Actions / lint (1.17)

cli.methodsCompat undefined (type *Client has no field or method methodsCompat) (typecheck)
if err := cli.methodsCompat[i].Init(); err != nil {

Check failure on line 63 in client/client.go

View workflow job for this annotation

GitHub Actions / lint (1.17)

cli.methodsCompat undefined (type *Client has no field or method methodsCompat) (typecheck)
return err
}
}

}
// init application
if refCfg.pplication != nil {

Check failure on line 70 in client/client.go

View workflow job for this annotation

GitHub Actions / lint (1.17)

refCfg.pplication undefined (type *global.ReferenceConfig has no field or method pplication) (typecheck)
rc.applicationCompat = compatApplicationConfig(rc.application)

Check failure on line 71 in client/client.go

View workflow job for this annotation

GitHub Actions / lint (1.17)

undeclared name: `rc` (typecheck)
if err := rc.applicationCompat.Init(); err != nil {

Check failure on line 72 in client/client.go

View workflow job for this annotation

GitHub Actions / lint (1.17)

undeclared name: `rc` (typecheck)
return err
}
rc.metaDataType = rc.applicationCompat.MetadataType
if rc.Group == "" {
rc.Group = rc.applicationCompat.Group
}
if rc.Version == "" {
rc.Version = rc.applicationCompat.Version
}
}
// init cluster
if rc.Cluster == "" {
rc.Cluster = "failover"
}
// todo: move to registry package
// init registries
if rc.registries != nil {
rc.registriesCompat = make(map[string]*config.RegistryConfig)
for key, reg := range rc.registries {
rc.registriesCompat[key] = compatRegistryConfig(reg)
if err := rc.registriesCompat[key].Init(); err != nil {
return err
}
}
}
rc.RegistryIDs = commonCfg.TranslateIds(rc.RegistryIDs)

return commonCfg.Verify(rc)
}

func (cli *Client) call(ctx context.Context, paramsRawVals []interface{}, interfaceName, methodName, callType string, opts ...CallOption) (protocol.Result, error) {
// get a default CallOptions
// apply CallOption
Expand Down
21 changes: 19 additions & 2 deletions client/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package client
import (
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/registry"
)

// these functions are used to resolve circular dependencies temporarily.
Expand All @@ -40,7 +39,7 @@ func compatApplicationConfig(c *global.ApplicationConfig) *config.ApplicationCon
}
}

func compatRegistryConfig(c *registry.RegistryConfig) *config.RegistryConfig {
func compatRegistryConfig(c *global.RegistryConfig) *config.RegistryConfig {
return &config.RegistryConfig{
Protocol: c.Protocol,
Timeout: c.Timeout,
Expand All @@ -60,3 +59,21 @@ func compatRegistryConfig(c *registry.RegistryConfig) *config.RegistryConfig {
UseAsConfigCenter: c.UseAsConfigCenter,
}
}

func compatMethodConfig(c *global.MethodConfig) *config.MethodConfig {
return &config.MethodConfig{
InterfaceId: c.InterfaceId,
InterfaceName: c.InterfaceName,
Name: c.Name,
Retries: c.Retries,
LoadBalance: c.LoadBalance,
Weight: c.Weight,
TpsLimitInterval: c.TpsLimitInterval,
TpsLimitRate: c.TpsLimitRate,
TpsLimitStrategy: c.TpsLimitStrategy,
ExecuteLimit: c.ExecuteLimit,
ExecuteLimitRejectedHandler: c.ExecuteLimitRejectedHandler,
Sticky: c.Sticky,
RequestTimeout: c.RequestTimeout,
}
}
104 changes: 100 additions & 4 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,109 @@
package client

import (
"dubbo.apache.org/dubbo-go/v3/common"
commonCfg "dubbo.apache.org/dubbo-go/v3/common/config"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/protocol"
"dubbo.apache.org/dubbo-go/v3/proxy"
"github.com/creasty/defaults"
"strconv"
)

import (
"dubbo.apache.org/dubbo-go/v3/registry"
)
type ClientOptions struct {
Application *global.ApplicationConfig
Consumer *global.ConsumerConfig
Reference *global.ReferenceConfig
Registries map[string]*global.RegistryConfig

pxy *proxy.Proxy
id string
invoker protocol.Invoker
urls []*common.URL
metaDataType string
info *ClientInfo

methodsCompat []*config.MethodConfig
}

func (cliOpts *ClientOptions) init() error {
if err := defaults.Set(cliOpts); err != nil {
return err
}

// init method
methods := cliOpts.Reference.Methods
if length := len(methods); length > 0 {
cliOpts.methodsCompat = make([]*config.MethodConfig, length)
for i, method := range methods {
cliOpts.methodsCompat[i] = compatMethodConfig(method)
if err := cliOpts.methodsCompat[i].Init(); err != nil {
return err
}
}

}

// init application
if rc.application != nil {
rc.applicationCompat = compatApplicationConfig(rc.application)
if err := rc.applicationCompat.Init(); err != nil {
return err
}
rc.metaDataType = rc.applicationCompat.MetadataType
if rc.Group == "" {
rc.Group = rc.applicationCompat.Group
}
if rc.Version == "" {
rc.Version = rc.applicationCompat.Version
}
}
// init cluster
if rc.Cluster == "" {
rc.Cluster = "failover"
}
// todo: move to registry package
// init registries
if rc.registries != nil {
rc.registriesCompat = make(map[string]*config.RegistryConfig)
for key, reg := range rc.registries {
rc.registriesCompat[key] = compatRegistryConfig(reg)
if err := rc.registriesCompat[key].Init(); err != nil {
return err
}
}
}
rc.RegistryIDs = commonCfg.TranslateIds(rc.RegistryIDs)

return commonCfg.Verify(rc)
}

type ClientOption func(*ClientOptions)

func WithApplication(application *global.ApplicationConfig) ClientOption {

Check failure on line 101 in client/options.go

View workflow job for this annotation

GitHub Actions / lint (1.17)

other declaration of WithApplication (typecheck)
return func(opts *ClientOptions) {
opts.Application = application
}
}

func WithConsumer(consumer *global.ConsumerConfig) ClientOption {
return func(opts *ClientOptions) {
opts.Consumer = consumer
}
}

func WithReference(reference *global.ReferenceConfig) ClientOption {
return func(opts *ClientOptions) {
opts.Reference = reference
}
}

func WithRegistries(registries map[string]*global.RegistryConfig) ClientOption {

Check failure on line 119 in client/options.go

View workflow job for this annotation

GitHub Actions / lint (1.17)

other declaration of WithRegistries (typecheck)
return func(opts *ClientOptions) {
opts.Registries = registries
}
}

// todo: need to be consistent with MethodConfig
type CallOptions struct {
Expand Down Expand Up @@ -218,7 +314,7 @@ func WithProxyFactory(proxyFactory string) ReferenceOption {

// ----------From RegistryConfig----------

func WithRegistries(registries map[string]*registry.RegistryConfig) ReferenceOption {
func WithRegistries(registries map[string]*global.RegistryConfig) ReferenceOption {

Check failure on line 317 in client/options.go

View workflow job for this annotation

GitHub Actions / lint (1.17)

`WithRegistries` redeclared in this block (typecheck)
return func(cfg *ReferenceConfig) {
cfg.registries = registries
}
Expand Down
3 changes: 1 addition & 2 deletions client/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (
"dubbo.apache.org/dubbo-go/v3/protocol"
"dubbo.apache.org/dubbo-go/v3/protocol/protocolwrapper"
"dubbo.apache.org/dubbo-go/v3/proxy"
"dubbo.apache.org/dubbo-go/v3/registry"
)

// ReferenceConfig is the configuration of service consumer
Expand Down Expand Up @@ -89,7 +88,7 @@ type ReferenceConfig struct {
application *global.ApplicationConfig
applicationCompat *config.ApplicationConfig

registries map[string]*registry.RegistryConfig
registries map[string]*global.RegistryConfig
registriesCompat map[string]*config.RegistryConfig
}

Expand Down
29 changes: 12 additions & 17 deletions compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ package dubbo

import (
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/config_center"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/metadata/report"
"dubbo.apache.org/dubbo-go/v3/metrics"
"dubbo.apache.org/dubbo-go/v3/protocol"
"dubbo.apache.org/dubbo-go/v3/registry"
"go.uber.org/atomic"
)

Expand Down Expand Up @@ -78,7 +73,7 @@ func compatApplicationConfig(c *global.ApplicationConfig) *config.ApplicationCon
}
}

func compatProtocolConfig(c *protocol.ProtocolConfig) *config.ProtocolConfig {
func compatProtocolConfig(c *global.ProtocolConfig) *config.ProtocolConfig {
return &config.ProtocolConfig{
Name: c.Name,
Ip: c.Ip,
Expand All @@ -89,7 +84,7 @@ func compatProtocolConfig(c *protocol.ProtocolConfig) *config.ProtocolConfig {
}
}

func compatRegistryConfig(c *registry.RegistryConfig) *config.RegistryConfig {
func compatRegistryConfig(c *global.RegistryConfig) *config.RegistryConfig {
return &config.RegistryConfig{
Protocol: c.Protocol,
Timeout: c.Timeout,
Expand All @@ -110,7 +105,7 @@ func compatRegistryConfig(c *registry.RegistryConfig) *config.RegistryConfig {
}
}

func compatCenterConfig(c *config_center.CenterConfig) *config.CenterConfig {
func compatCenterConfig(c *global.CenterConfig) *config.CenterConfig {
return &config.CenterConfig{
Protocol: c.Protocol,
Address: c.Address,
Expand All @@ -127,7 +122,7 @@ func compatCenterConfig(c *config_center.CenterConfig) *config.CenterConfig {
}
}

func compatMetadataReportConfig(c *report.MetadataReportConfig) *config.MetadataReportConfig {
func compatMetadataReportConfig(c *global.MetadataReportConfig) *config.MetadataReportConfig {
return &config.MetadataReportConfig{
Protocol: c.Protocol,
Address: c.Address,
Expand Down Expand Up @@ -156,21 +151,21 @@ func compatProviderConfig(c *global.ProviderConfig) *config.ProviderConfig {

func compatConsumerConfig(c *global.ConsumerConfig) *config.ConsumerConfig {
return &config.ConsumerConfig{
Filter: c.Filter,
RegistryIDs: c.RegistryIDs,
Protocol: c.Protocol,
RequestTimeout: c.RequestTimeout,
ProxyFactory: c.ProxyFactory,
Check: c.Check,
AdaptiveService: c.AdaptiveService,
Filter: c.Filter,
RegistryIDs: c.RegistryIDs,
Protocol: c.Protocol,
RequestTimeout: c.RequestTimeout,
ProxyFactory: c.ProxyFactory,
Check: c.Check,
AdaptiveService: c.AdaptiveService,
TracingKey: c.TracingKey,
FilterConf: c.FilterConf,
MaxWaitTimeForServiceDiscovery: c.MaxWaitTimeForServiceDiscovery,
MeshEnabled: c.MeshEnabled,
}
}

func compatMetricConfig(c *metrics.MetricConfig) *config.MetricConfig {
func compatMetricConfig(c *global.MetricConfig) *config.MetricConfig {
return &config.MetricConfig{
Mode: c.Mode,
Namespace: c.Namespace,
Expand Down
Loading

0 comments on commit 38e57c7

Please sign in to comment.