From 6f77a0cf7e0b494472ec9c5e40571e1c68bd238b Mon Sep 17 00:00:00 2001 From: tiecheng <18768171164@163.com> Date: Thu, 30 Jul 2020 19:31:26 +0800 Subject: [PATCH] add code format and comment --- cluster/cluster_impl/zone_aware_cluster.go | 7 +++-- .../zone_aware_cluster_invoker.go | 9 ++++-- .../zone_aware_cluster_invoker_test.go | 28 +++++++++++++------ config/reference_config.go | 4 ++- config/registry_config.go | 3 +- 5 files changed, 36 insertions(+), 15 deletions(-) diff --git a/cluster/cluster_impl/zone_aware_cluster.go b/cluster/cluster_impl/zone_aware_cluster.go index b5d43c704..2e19be476 100644 --- a/cluster/cluster_impl/zone_aware_cluster.go +++ b/cluster/cluster_impl/zone_aware_cluster.go @@ -31,7 +31,10 @@ func init() { extension.SetCluster(zoneAware, NewZoneAwareCluster) } -// NewZoneAwareCluster ... +// NewZoneAwareCluster returns a zoneaware cluster instance. +// +// More than one registry for subscription. +// Usually it is used for choose between registries. func NewZoneAwareCluster() cluster.Cluster { return &zoneAwareCluster{} } @@ -41,7 +44,7 @@ func (cluster *zoneAwareCluster) Join(directory cluster.Directory) protocol.Invo return newZoneAwareClusterInvoker(directory) } -// Get cluster name +// GetZoneAwareName get cluster name func GetZoneAwareName() string { return zoneAware } diff --git a/cluster/cluster_impl/zone_aware_cluster_invoker.go b/cluster/cluster_impl/zone_aware_cluster_invoker.go index f3b5fbe52..60683c32d 100644 --- a/cluster/cluster_impl/zone_aware_cluster_invoker.go +++ b/cluster/cluster_impl/zone_aware_cluster_invoker.go @@ -56,7 +56,8 @@ func (invoker *zoneAwareClusterInvoker) Invoke(ctx context.Context, invocation p // First, pick the invoker (XXXClusterInvoker) that comes from the local registry, distinguish by a 'preferred' key. for _, invoker := range invokers { - if invoker.IsAvailable() && "true" == invoker.GetUrl().GetParam(constant.REGISTRY_KEY+"."+constant.PREFERRED_KEY, "false") { + if invoker.IsAvailable() && + "true" == invoker.GetUrl().GetParam(constant.REGISTRY_KEY+"."+constant.PREFERRED_KEY, "false") { return invoker.Invoke(ctx, invocation) } } @@ -65,7 +66,8 @@ func (invoker *zoneAwareClusterInvoker) Invoke(ctx context.Context, invocation p zone := invocation.AttachmentsByKey(constant.REGISTRY_ZONE, "") if "" != zone { for _, invoker := range invokers { - if invoker.IsAvailable() && zone == invoker.GetUrl().GetParam(constant.REGISTRY_KEY+"."+constant.ZONE_KEY, "") { + if invoker.IsAvailable() && + zone == invoker.GetUrl().GetParam(constant.REGISTRY_KEY+"."+constant.ZONE_KEY, "") { return invoker.Invoke(ctx, invocation) } } @@ -73,7 +75,8 @@ func (invoker *zoneAwareClusterInvoker) Invoke(ctx context.Context, invocation p force := invocation.AttachmentsByKey(constant.REGISTRY_ZONE_FORCE, "") if "true" == force { return &protocol.RPCResult{ - Err: fmt.Errorf("no registry instance in zone or no available providers in the registry, zone: %v, "+ + Err: fmt.Errorf("no registry instance in zone or "+ + "no available providers in the registry, zone: %v, "+ " registries: %v", zone, invoker.GetUrl()), } } diff --git a/cluster/cluster_impl/zone_aware_cluster_invoker_test.go b/cluster/cluster_impl/zone_aware_cluster_invoker_test.go index f1c0ac04b..4ac865972 100644 --- a/cluster/cluster_impl/zone_aware_cluster_invoker_test.go +++ b/cluster/cluster_impl/zone_aware_cluster_invoker_test.go @@ -39,10 +39,13 @@ import ( func Test_ZoneWareInvokerWithPreferredSuccess(t *testing.T) { ctrl := gomock.NewController(t) - // In Go versions 1.14+, if you pass a *testing.T into gomock.NewController(t) you no longer need to call ctrl.Finish(). + // In Go versions 1.14+, if you pass a *testing.T + // into gomock.NewController(t) you no longer need to call ctrl.Finish(). //defer ctrl.Finish() - mockResult := &protocol.RPCResult{Attrs: map[string]string{constant.PREFERRED_KEY: "true"}, Rest: rest{tried: 0, success: true}} + mockResult := &protocol.RPCResult{ + Attrs: map[string]string{constant.PREFERRED_KEY: "true"}, + Rest: rest{tried: 0, success: true}} var invokers []protocol.Invoker for i := 0; i < 2; i++ { @@ -77,7 +80,8 @@ func Test_ZoneWareInvokerWithPreferredSuccess(t *testing.T) { func Test_ZoneWareInvokerWithWeightSuccess(t *testing.T) { ctrl := gomock.NewController(t) - // In Go versions 1.14+, if you pass a *testing.T into gomock.NewController(t) you no longer need to call ctrl.Finish(). + // In Go versions 1.14+, if you pass a *testing.T + // into gomock.NewController(t) you no longer need to call ctrl.Finish(). //defer ctrl.Finish() w1 := "50" @@ -94,13 +98,17 @@ func Test_ZoneWareInvokerWithWeightSuccess(t *testing.T) { url.SetParam(constant.REGISTRY_KEY+"."+constant.WEIGHT_KEY, w1) invoker.EXPECT().Invoke(gomock.Any()).DoAndReturn( func(invocation protocol.Invocation) protocol.Result { - return &protocol.RPCResult{Attrs: map[string]string{constant.WEIGHT_KEY: w1}, Rest: rest{tried: 0, success: true}} + return &protocol.RPCResult{ + Attrs: map[string]string{constant.WEIGHT_KEY: w1}, + Rest: rest{tried: 0, success: true}} }).MaxTimes(100) } else { url.SetParam(constant.REGISTRY_KEY+"."+constant.WEIGHT_KEY, w2) invoker.EXPECT().Invoke(gomock.Any()).DoAndReturn( func(invocation protocol.Invocation) protocol.Result { - return &protocol.RPCResult{Attrs: map[string]string{constant.WEIGHT_KEY: w2}, Rest: rest{tried: 0, success: true}} + return &protocol.RPCResult{ + Attrs: map[string]string{constant.WEIGHT_KEY: w2}, + Rest: rest{tried: 0, success: true}} }).MaxTimes(100) } invokers = append(invokers, invoker) @@ -130,7 +138,8 @@ func Test_ZoneWareInvokerWithZoneSuccess(t *testing.T) { var zoneArray = []string{"hangzhou", "shanghai"} ctrl := gomock.NewController(t) - // In Go versions 1.14+, if you pass a *testing.T into gomock.NewController(t) you no longer need to call ctrl.Finish(). + // In Go versions 1.14+, if you pass a *testing.T + // into gomock.NewController(t) you no longer need to call ctrl.Finish(). //defer ctrl.Finish() var invokers []protocol.Invoker @@ -144,7 +153,9 @@ func Test_ZoneWareInvokerWithZoneSuccess(t *testing.T) { invoker.EXPECT().GetUrl().Return(url).AnyTimes() invoker.EXPECT().Invoke(gomock.Any()).DoAndReturn( func(invocation protocol.Invocation) protocol.Result { - return &protocol.RPCResult{Attrs: map[string]string{constant.ZONE_KEY: zoneValue}, Rest: rest{tried: 0, success: true}} + return &protocol.RPCResult{ + Attrs: map[string]string{constant.ZONE_KEY: zoneValue}, + Rest: rest{tried: 0, success: true}} }) invokers = append(invokers, invoker) } @@ -165,7 +176,8 @@ func Test_ZoneWareInvokerWithZoneSuccess(t *testing.T) { func Test_ZoneWareInvokerWithZoneForceFail(t *testing.T) { ctrl := gomock.NewController(t) - // In Go versions 1.14+, if you pass a *testing.T into gomock.NewController(t) you no longer need to call ctrl.Finish(). + // In Go versions 1.14+, if you pass a *testing.T + // into gomock.NewController(t) you no longer need to call ctrl.Finish(). //defer ctrl.Finish() var invokers []protocol.Invoker diff --git a/config/reference_config.go b/config/reference_config.go index e6e421897..1ce60d367 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -151,7 +151,9 @@ func (c *ReferenceConfig) Refer(_ interface{}) { if regUrl != nil { // for multi-subscription scenario, use 'zone-aware' policy by default cluster := extension.GetCluster(cluster_impl.GetZoneAwareName()) - // The invoker wrap sequence would be: ZoneAwareClusterInvoker(StaticDirectory) -> FailoverClusterInvoker(RegistryDirectory, routing happens here) -> Invoker + // The invoker wrap sequence would be: + // ZoneAwareClusterInvoker(StaticDirectory) -> + // FailoverClusterInvoker(RegistryDirectory, routing happens here) -> Invoker c.invoker = cluster.Join(directory.NewStaticDirectory(invokers)) } else { // not a registry url, must be direct invoke. diff --git a/config/registry_config.go b/config/registry_config.go index 92a174d8f..53c0f9f5d 100644 --- a/config/registry_config.go +++ b/config/registry_config.go @@ -48,7 +48,8 @@ type RegistryConfig struct { Preferred bool `yaml:"preferred" json:"params,omitempty" property:"preferred"` // The region where the registry belongs, usually used to isolate traffics Zone string `yaml:"zone" json:"params,omitempty" property:"zone"` - // Affects traffic distribution among registries, useful when subscribe to multiple registries Take effect only when no preferred registry is specified. + // Affects traffic distribution among registries, + // useful when subscribe to multiple registries Take effect only when no preferred registry is specified. Weight int64 `yaml:"weight" json:"params,omitempty" property:"weight"` Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"` } -- GitLab