From 68c442359a61864429aa41dd7848c67852d7caf1 Mon Sep 17 00:00:00 2001 From: zxl Date: Fri, 14 Jun 2024 21:13:21 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=8D=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/degradation.go | 23 +++++------ go.mod | 4 +- .../degradation.go | 19 +-------- pkg/degradation/item_degradation_test.go | 40 ------------------- server/suite.go | 2 +- zookeeper/zookeeper.go | 1 - 6 files changed, 13 insertions(+), 76 deletions(-) rename pkg/degradation/item_degradation.go => model/degradation.go (68%) delete mode 100644 pkg/degradation/item_degradation_test.go diff --git a/client/degradation.go b/client/degradation.go index e9dd598..a989c91 100644 --- a/client/degradation.go +++ b/client/degradation.go @@ -16,10 +16,9 @@ package client import ( "context" - "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/pkg/klog" - "github.com/kitex-contrib/config-zookeeper/pkg/degradation" + "github.com/kitex-contrib/config-zookeeper/model" "github.com/kitex-contrib/config-zookeeper/utils" "github.com/kitex-contrib/config-zookeeper/zookeeper" ) @@ -33,39 +32,35 @@ func WithDegradation(dest, src string, zookeeperClient zookeeper.Client, opts ut if err != nil { panic(err) } - for _, f := range opts.ZookeeperCustomFunctions { f(¶m) } - + key := param.Prefix + "/" + param.Path uid := zookeeper.GetUniqueID() - path := param.Prefix + "/" + param.Path - container := initDegradation(path, uid, dest, zookeeperClient) + container := initDegradationOptions(key, dest, uid, zookeeperClient) return []client.Option{ client.WithACLRules(container.GetAclRule()), client.WithCloseCallbacks(func() error { // cancel the configuration listener when client is closed. - zookeeperClient.DeregisterConfig(path, uid) + zookeeperClient.DeregisterConfig(key, uid) return nil }), } } -func initDegradation(path string, uniqueID int64, dest string, zookeeperClient zookeeper.Client) *degradation.Container { - container := degradation.NewContainer() +func initDegradationOptions(key, dest string, uniqueID int64, zooKeeperClient zookeeper.Client) *model.Container { + container := model.NewContainer() onChangeCallback := func(restoreDefault bool, data string, parser zookeeper.ConfigParser) { - config := °radation.Config{} + config := &model.Config{} if !restoreDefault { err := parser.Decode(data, config) if err != nil { - klog.Warnf("[zookeeper] %s server zookeeper degradation config: unmarshal data %s failed: %s, skip...", path, data, err) + klog.Warnf("[etcd] %s server etcd degradation config: unmarshal data %s failed: %s, skip...", key, data, err) return } } container.NotifyPolicyChange(config) } - - zookeeperClient.RegisterConfigCallback(context.Background(), path, uniqueID, onChangeCallback) - + zooKeeperClient.RegisterConfigCallback(context.Background(), key, uniqueID, onChangeCallback) return container } diff --git a/go.mod b/go.mod index 9846727..24e66fb 100644 --- a/go.mod +++ b/go.mod @@ -3,18 +3,18 @@ module github.com/kitex-contrib/config-zookeeper go 1.19 require ( + github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b + github.com/cloudwego/configmanager v0.2.0 github.com/cloudwego/kitex v0.7.3 github.com/go-zookeeper/zk v1.0.3 ) require ( github.com/apache/thrift v0.13.0 // indirect - github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect github.com/bytedance/sonic v1.9.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/chenzhuoyu/iasm v0.9.0 // indirect github.com/choleraehyq/pid v0.0.17 // indirect - github.com/cloudwego/configmanager v0.2.0 // indirect github.com/cloudwego/dynamicgo v0.1.3 // indirect github.com/cloudwego/fastpb v0.0.4 // indirect github.com/cloudwego/frugal v0.1.8 // indirect diff --git a/pkg/degradation/item_degradation.go b/model/degradation.go similarity index 68% rename from pkg/degradation/item_degradation.go rename to model/degradation.go index d1746d6..b74a5db 100644 --- a/pkg/degradation/item_degradation.go +++ b/model/degradation.go @@ -1,18 +1,4 @@ -// Copyright 2024 CloudWeGo Authors -// -// Licensed 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 degradation +package model import ( "context" @@ -38,9 +24,6 @@ type Config struct { // DeepCopy returns a copy of the current Config func (c *Config) DeepCopy() iface.ConfigValueItem { - if c == nil { - return nil - } result := &Config{ Enable: c.Enable, Percentage: c.Percentage, diff --git a/pkg/degradation/item_degradation_test.go b/pkg/degradation/item_degradation_test.go deleted file mode 100644 index a45c921..0000000 --- a/pkg/degradation/item_degradation_test.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2024 CloudWeGo Authors -// -// Licensed 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 degradation - -import ( - "context" - "errors" - "testing" - - "github.com/cloudwego/kitex/pkg/acl" - "github.com/cloudwego/thriftgo/pkg/test" -) - -var errFake = errors.New("fake error") - -func invoke(ctx context.Context, request, response interface{}) error { - return errFake -} - -func TestNewContainer(t *testing.T) { - container := NewContainer() - aclMiddleware := acl.NewACLMiddleware([]acl.RejectFunc{container.GetAclRule()}) - test.Assert(t, errors.Is(aclMiddleware(invoke)(context.Background(), nil, nil), errFake)) - container.NotifyPolicyChange(&Config{Enable: false, Percentage: 100}) - test.Assert(t, errors.Is(aclMiddleware(invoke)(context.Background(), nil, nil), errFake)) - container.NotifyPolicyChange(&Config{Enable: true, Percentage: 100}) - test.Assert(t, errors.Is(aclMiddleware(invoke)(context.Background(), nil, nil), errRejected)) -} diff --git a/server/suite.go b/server/suite.go index be1a8e5..70596ae 100644 --- a/server/suite.go +++ b/server/suite.go @@ -43,7 +43,7 @@ func NewSuite(service string, cli zookeeper.Client, opts ...utils.Option) *Zooke return su } -// Options return a list server.Option +// Options return a list client.Option func (s *ZookeeperServerSuite) Options() []server.Option { opts := make([]server.Option, 0, 2) opts = append(opts, WithLimiter(s.service, s.zookeeperClient, s.opts)) diff --git a/zookeeper/zookeeper.go b/zookeeper/zookeeper.go index 14029d7..db9d1b3 100644 --- a/zookeeper/zookeeper.go +++ b/zookeeper/zookeeper.go @@ -11,7 +11,6 @@ // 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 zookeeper import (