generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 8
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 (#25)
* add new * feat: support degradation nacos-config for Kitex client * feat: support nacos degradation * feat: support nacos degradation * feat: support nacos degradation * feat: support nacos degradation * feat: support nacos degradation * feat: support nacos degradation * rollback go.mod/sum files * feat: retrun DeepCooy of defaultConfig * feat: update README file
- Loading branch information
Showing
12 changed files
with
463 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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 ( | ||
"github.com/cloudwego/kitex/client" | ||
"github.com/cloudwego/kitex/pkg/klog" | ||
"github.com/kitex-contrib/config-nacos/pkg/degradation" | ||
"github.com/nacos-group/nacos-sdk-go/vo" | ||
|
||
"github.com/kitex-contrib/config-nacos/nacos" | ||
"github.com/kitex-contrib/config-nacos/utils" | ||
) | ||
|
||
// WithDegradation sets the degradation policy from nacos configuration center. | ||
func WithDegradation(dest, src string, nacosClient nacos.Client, opts utils.Options) []client.Option { | ||
param, err := nacosClient.ClientConfigParam(&nacos.ConfigParamConfig{ | ||
Category: degradationName, | ||
ServerServiceName: dest, | ||
ClientServiceName: src, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, f := range opts.NacosCustomFunctions { | ||
f(¶m) | ||
} | ||
|
||
uniqueID := nacos.GetUniqueID() | ||
|
||
degradationContainer := initDegradation(param, dest, src, nacosClient, uniqueID) | ||
|
||
return []client.Option{ | ||
client.WithACLRules(degradationContainer.GetACLRule()), | ||
client.WithCloseCallbacks(func() error { | ||
err := nacosClient.DeregisterConfig(param, uniqueID) | ||
if err != nil { | ||
return err | ||
} | ||
// cancel the configuration listener when client is closed. | ||
return nil | ||
}), | ||
} | ||
} | ||
|
||
func initDegradation(param vo.ConfigParam, dest, src string, | ||
nacosClient nacos.Client, uniqueID int64, | ||
) *degradation.Container { | ||
degradationContainer := degradation.NewDegradationContainer() | ||
|
||
onChangeCallback := func(data string, parser nacos.ConfigParser) { | ||
config := °radation.Config{} | ||
err := parser.Decode(param.Type, data, config) | ||
if err != nil { | ||
klog.Warnf("[nacos] %s client nacos rpc degradation: unmarshal data %s failed: %s, skip...", dest, data, err) | ||
return | ||
} | ||
// update degradation config | ||
degradationContainer.NotifyPolicyChange(config) | ||
} | ||
|
||
nacosClient.RegisterConfigCallback(param, onChangeCallback, uniqueID) | ||
|
||
return degradationContainer | ||
} |
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,90 @@ | ||
// 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" | ||
"sync" | ||
"sync/atomic" | ||
|
||
"github.com/bytedance/gopkg/lang/fastrand" | ||
"github.com/cloudwego/kitex/pkg/acl" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var errorDegradation = errors.New("rejected by client degradation config") | ||
|
||
// DegradationConfig is policy config of degradator. | ||
// DON'T FORGET to update DeepCopy() and Equals() if you add new fields. | ||
type Config struct { | ||
Enable bool `json:"enable"` | ||
Percentage int `json:"percentage"` | ||
} | ||
|
||
type Container struct { | ||
sync.RWMutex | ||
config atomic.Value | ||
} | ||
|
||
var defaultConfig = &Config{Enable: false, Percentage: 0} | ||
|
||
// GetDefaultDegradationConfig return defaultConfig of degradation. | ||
func GetDefaultDegradationConfig() *Config { | ||
return defaultConfig.DeepCopy() | ||
} | ||
|
||
func NewDegradationContainer() *Container { | ||
degradationContainer := &Container{} | ||
degradationContainer.config.Store(GetDefaultDegradationConfig()) | ||
return degradationContainer | ||
} | ||
|
||
func (s *Container) NotifyPolicyChange(cfg *Config) { | ||
s.config.Store(cfg) | ||
} | ||
|
||
func (s *Container) GetACLRule() acl.RejectFunc { | ||
return func(ctx context.Context, request interface{}) (reason error) { | ||
config := s.config.Load().(*Config) | ||
if !config.Enable { | ||
return nil | ||
} | ||
if fastrand.Intn(100) < config.Percentage { | ||
return errorDegradation | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
// DeepCopy returns a full copy of DegradationConfig. | ||
func (c *Config) DeepCopy() *Config { | ||
if c == nil { | ||
return nil | ||
} | ||
return &Config{ | ||
Enable: c.Enable, | ||
Percentage: c.Percentage, | ||
} | ||
} | ||
|
||
func (c *Config) Equals(other *Config) bool { | ||
if c == nil && other == nil { | ||
return true | ||
} | ||
if c == nil || other == nil { | ||
return false | ||
} | ||
return c.Enable == other.Enable && c.Percentage == other.Percentage | ||
} |
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 := NewDegradationContainer() | ||
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), errorDegradation)) | ||
} |
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,78 @@ | ||
// 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 ( | ||
"github.com/cloudwego/kitex/client" | ||
"github.com/cloudwego/kitex/pkg/klog" | ||
"github.com/kitex-contrib/config-nacos/v2/pkg/degradation" | ||
"github.com/nacos-group/nacos-sdk-go/v2/vo" | ||
|
||
"github.com/kitex-contrib/config-nacos/v2/nacos" | ||
"github.com/kitex-contrib/config-nacos/v2/utils" | ||
) | ||
|
||
// WithDegradation sets the degradation policy from nacos configuration center. | ||
func WithDegradation(dest, src string, nacosClient nacos.Client, opts utils.Options) []client.Option { | ||
param, err := nacosClient.ClientConfigParam(&nacos.ConfigParamConfig{ | ||
Category: degradationName, | ||
ServerServiceName: dest, | ||
ClientServiceName: src, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, f := range opts.NacosCustomFunctions { | ||
f(¶m) | ||
} | ||
|
||
uniqueID := nacos.GetUniqueID() | ||
|
||
degradationContainer := initDegradation(param, dest, src, nacosClient, uniqueID) | ||
|
||
return []client.Option{ | ||
client.WithACLRules(degradationContainer.GetACLRule()), | ||
client.WithCloseCallbacks(func() error { | ||
err := nacosClient.DeregisterConfig(param, uniqueID) | ||
if err != nil { | ||
return err | ||
} | ||
// cancel the configuration listener when client is closed. | ||
return nil | ||
}), | ||
} | ||
} | ||
|
||
func initDegradation(param vo.ConfigParam, dest, src string, | ||
nacosClient nacos.Client, uniqueID int64, | ||
) *degradation.Container { | ||
degradationContainer := degradation.NewDegradationContainer() | ||
|
||
onChangeCallback := func(data string, parser nacos.ConfigParser) { | ||
config := °radation.Config{} | ||
err := parser.Decode(param.Type, data, config) | ||
if err != nil { | ||
klog.Warnf("[nacos] %s client nacos rpc degradation: unmarshal data %s failed: %s, skip...", dest, data, err) | ||
return | ||
} | ||
// update degradation config | ||
degradationContainer.NotifyPolicyChange(config) | ||
} | ||
|
||
nacosClient.RegisterConfigCallback(param, onChangeCallback, uniqueID) | ||
|
||
return degradationContainer | ||
} |
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
Oops, something went wrong.