generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support degradation config for Kitex client
- Loading branch information
1 parent
a20b754
commit b38bd3e
Showing
7 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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/utils" | ||
"github.com/kitex-contrib/config-zookeeper/zookeeper" | ||
) | ||
|
||
func WithDegradation(dest, src string, zookeeperClient zookeeper.Client, opts utils.Options) []client.Option { | ||
param, err := zookeeperClient.ClientConfigParam(&zookeeper.ConfigParamConfig{ | ||
Category: degradationConfigName, | ||
ServerServiceName: dest, | ||
ClientServiceName: src, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, f := range opts.ZookeeperCustomFunctions { | ||
f(¶m) | ||
} | ||
|
||
uid := zookeeper.GetUniqueID() | ||
path := param.Prefix + "/" + param.Path | ||
container := initDegradation(path, uid, dest, zookeeperClient) | ||
return []client.Option{ | ||
client.WithACLRules(container.GetAclRule()), | ||
client.WithCloseCallbacks(func() error { | ||
// cancel the configuration listener when client is closed. | ||
zookeeperClient.DeregisterConfig(path, uid) | ||
return nil | ||
}), | ||
} | ||
} | ||
|
||
func initDegradation(path string, uniqueID int64, dest string, zookeeperClient zookeeper.Client) *degradation.Container { | ||
container := degradation.NewContainer() | ||
onChangeCallback := func(restoreDefault bool, data string, parser zookeeper.ConfigParser) { | ||
config := °radation.Config{} | ||
if !restoreDefault { | ||
err := parser.Decode(data, config) | ||
if err != nil { | ||
klog.Warnf("[zookeeper] %s server etcd degradation config: unmarshal data %s failed: %s, skip...", path, data, err) | ||
return | ||
} | ||
} | ||
container.NotifyPolicyChange(config) | ||
} | ||
|
||
zookeeperClient.RegisterConfigCallback(context.Background(), path, uniqueID, onChangeCallback) | ||
|
||
return container | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// 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" | ||
"sync/atomic" | ||
|
||
"github.com/bytedance/gopkg/lang/fastrand" | ||
"github.com/cloudwego/configmanager/iface" | ||
"github.com/cloudwego/kitex/pkg/acl" | ||
) | ||
|
||
var errRejected = errors.New("rejected by client degradation config") | ||
|
||
var defaultConfig = &Config{ | ||
Enable: false, | ||
Percentage: 0, | ||
} | ||
|
||
type Config struct { | ||
Enable bool `json:"enable"` | ||
Percentage int `json:"percentage"` | ||
} | ||
|
||
// 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, | ||
} | ||
return result | ||
} | ||
|
||
// EqualsTo returns true if the current Config equals to the other Config | ||
func (c *Config) EqualsTo(other iface.ConfigValueItem) bool { | ||
o := other.(*Config) | ||
return c.Enable == o.Enable && c.Percentage == o.Percentage | ||
} | ||
|
||
// Container is a wrapper for Config | ||
type Container struct { | ||
config atomic.Value | ||
} | ||
|
||
func NewContainer() *Container { | ||
c := &Container{} | ||
c.config.Store(defaultConfig) | ||
return c | ||
} | ||
|
||
// NotifyPolicyChange to receive policy when it changes | ||
func (c *Container) NotifyPolicyChange(cfg *Config) { | ||
c.config.Store(cfg) | ||
} | ||
|
||
func (c *Container) GetAclRule() acl.RejectFunc { | ||
return func(ctx context.Context, request interface{}) (reason error) { | ||
cfg := c.config.Load().(*Config) | ||
if !cfg.Enable { | ||
return nil | ||
} | ||
if fastrand.Intn(100) < cfg.Percentage { | ||
return errRejected | ||
} | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters