Skip to content

Commit

Permalink
降级
Browse files Browse the repository at this point in the history
  • Loading branch information
zxl committed Jun 14, 2024
1 parent b9115a0 commit 68c4423
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 76 deletions.
23 changes: 9 additions & 14 deletions client/degradation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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(&param)
}

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 := &degradation.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
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 1 addition & 18 deletions pkg/degradation/item_degradation.go → model/degradation.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
Expand Down
40 changes: 0 additions & 40 deletions pkg/degradation/item_degradation_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion server/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 0 additions & 1 deletion zookeeper/zookeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 68c4423

Please sign in to comment.