-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
config-file part degradation #9
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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 ( | ||
kitexclient "github.com/cloudwego/kitex/client" | ||
"github.com/kitex-contrib/config-file/monitor" | ||
degradation "github.com/kitex-contrib/config-file/pkg" | ||
) | ||
|
||
func WithDegradation(watcher monitor.ConfigMonitor) []kitexclient.Option { | ||
container, keyDegradation := initDegradationOptions(watcher) | ||
return []kitexclient.Option{ | ||
kitexclient.WithACLRules(container.GetAclRule()), | ||
kitexclient.WithCloseCallbacks(func() error { | ||
watcher.DeregisterCallback(keyDegradation) | ||
return nil | ||
}), | ||
} | ||
} | ||
|
||
func initDegradationOptions(watcher monitor.ConfigMonitor) (*degradation.Container, int64) { | ||
degradationContainer := degradation.NewContainer() | ||
onChangeCallback := func() { | ||
config := getFileConfig(watcher) | ||
if config == nil { | ||
return // config is nil, do nothing, log will be printed in getFileConfig | ||
} | ||
degradationContainer.NotifyPolicyChange(config.Degradation) | ||
} | ||
keyDegradation := watcher.RegisterCallback(onChangeCallback) | ||
return degradationContainer, keyDegradation | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,10 @@ | |
} | ||
} | ||
} | ||
}, | ||
"degradation": { | ||
"enabled": true, | ||
"percentage": 0 | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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 = &DegradationConfig{ | ||
Enable: false, | ||
Percentage: 0, | ||
} | ||
|
||
type DegradationConfig struct { | ||
Enable bool `json:"enabled"` | ||
Percentage int `json:"percentage"` | ||
} | ||
|
||
// DeepCopy returns a copy of the current Config | ||
func (c *DegradationConfig) DeepCopy() iface.ConfigValueItem { | ||
result := &DegradationConfig{ | ||
Enable: c.Enable, | ||
Percentage: c.Percentage, | ||
} | ||
return result | ||
} | ||
|
||
// EqualsTo returns true if the current Config equals to the other Config | ||
func (c *DegradationConfig) EqualsTo(other iface.ConfigValueItem) bool { | ||
o, ok := other.(*DegradationConfig) | ||
if !ok { | ||
return false | ||
} | ||
if c == nil { | ||
return o == nil | ||
} | ||
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 map[string]*DegradationConfig) { | ||
for _, value := range cfg { | ||
newConfig := &DegradationConfig{ | ||
Enable: value.Enable, | ||
Percentage: value.Percentage, | ||
} | ||
c.config.Store(newConfig) | ||
} | ||
} | ||
|
||
func (c *Container) GetAclRule() acl.RejectFunc { | ||
return func(ctx context.Context, request interface{}) (reason error) { | ||
cfg := c.config.Load().(*DegradationConfig) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When users do not configure degration in kitex_client.json, this cfg would be nil. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, you just need to check whether cfg == nil here. |
||
if !cfg.Enable { | ||
return nil | ||
} | ||
if fastrand.Intn(100) < cfg.Percentage { | ||
return errRejected | ||
} | ||
return nil | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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() | ||
cfg1 := map[string]*DegradationConfig{ | ||
"someMethod": {Enable: false, Percentage: 100}, // Example method and config | ||
} | ||
cfg2 := map[string]*DegradationConfig{ | ||
"someMethod": {Enable: true, Percentage: 100}, // Example method and config | ||
} | ||
aclMiddleware := acl.NewACLMiddleware([]acl.RejectFunc{container.GetAclRule()}) | ||
test.Assert(t, errors.Is(aclMiddleware(invoke)(context.Background(), nil, nil), errFake)) | ||
container.NotifyPolicyChange(cfg1) | ||
test.Assert(t, errors.Is(aclMiddleware(invoke)(context.Background(), nil, nil), errFake)) | ||
container.NotifyPolicyChange(cfg2) | ||
test.Assert(t, errors.Is(aclMiddleware(invoke)(context.Background(), nil, nil), errRejected)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you use map?