generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 4
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 (#7)
* feat: support degradation config for Kitex client * feat: support degradation config for Kitex client * feat: add license header * Add unit test and docs * typo fix * fix lint problem and modify pr-check.yml * delete degradation middleware and use acl middleware * update returned error * update test
- Loading branch information
1 parent
ee1fe89
commit 7fe0870
Showing
8 changed files
with
231 additions
and
2 deletions.
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
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,66 @@ | ||
// 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 client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cloudwego/kitex/client" | ||
"github.com/cloudwego/kitex/pkg/klog" | ||
"github.com/kitex-contrib/config-etcd/etcd" | ||
"github.com/kitex-contrib/config-etcd/pkg/degradation" | ||
"github.com/kitex-contrib/config-etcd/utils" | ||
) | ||
|
||
func WithDegradation(dest, src string, etcdClient etcd.Client, uniqueID int64, opts utils.Options) []client.Option { | ||
param, err := etcdClient.ClientConfigParam(&etcd.ConfigParamConfig{ | ||
Category: degradationConfigName, | ||
ServerServiceName: dest, | ||
ClientServiceName: src, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
for _, f := range opts.EtcdCustomFunctions { | ||
f(¶m) | ||
} | ||
key := param.Prefix + "/" + param.Path | ||
container := initDegradationOptions(key, dest, uniqueID, etcdClient) | ||
return []client.Option{ | ||
client.WithACLRules(container.GetAclRule()), | ||
client.WithCloseCallbacks(func() error { | ||
// cancel the configuration listener when client is closed. | ||
etcdClient.DeregisterConfig(key, uniqueID) | ||
return nil | ||
}), | ||
} | ||
} | ||
|
||
func initDegradationOptions(key, dest string, uniqueID int64, etcdClient etcd.Client) *degradation.Container { | ||
container := degradation.NewContainer() | ||
onChangeCallback := func(restoreDefault bool, data string, parser etcd.ConfigParser) { | ||
config := °radation.Config{} | ||
if !restoreDefault { | ||
err := parser.Decode(data, config) | ||
if err != nil { | ||
klog.Warnf("[etcd] %s server etcd degradation config: unmarshal data %s failed: %s, skip...", key, data, err) | ||
return | ||
} | ||
} | ||
container.NotifyPolicyChange(config) | ||
} | ||
etcdClient.RegisterConfigCallback(context.Background(), key, 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,81 @@ | ||
// 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 { | ||
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