Skip to content

Commit

Permalink
Add unit test and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoYi-byte committed May 13, 2024
1 parent 8162ac2 commit 697ed6f
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ The echo method uses the following configuration (0.3, 100) and other methods us

| Variable | Introduction |
|------------|------------------------------------|
| enable | Whether to enable degradation |
| percentage | The percentage of dropped requests |

Example:
Expand Down
7 changes: 4 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,10 @@ echo 方法使用下面的配置(0.3、100),其他方法使用全局默认

[JSON Schema](https://github.com/cloudwego/kitex/blob/develop/pkg/circuitbreak/item_circuit_breaker.go#L30)

| 参数 | 说明 |
|------------|---------|
| percentage | 丢弃请求的比例 |
| 参数 | 说明 |
|------------|----------|
| enable | 是否开启降级策略 |
| percentage | 丢弃请求的比例 |

例子:

Expand Down
3 changes: 1 addition & 2 deletions client/circuit_breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import (
"context"
"strings"

"github.com/kitex-contrib/config-etcd/pkg/utils"

"github.com/cloudwego/kitex/pkg/klog"

"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/circuitbreak"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/kitex-contrib/config-etcd/etcd"
"github.com/kitex-contrib/config-etcd/utils"
)

// WithCircuitBreaker sets the circuit breaker policy from etcd configuration center.
Expand Down
2 changes: 1 addition & 1 deletion client/degradation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"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/pkg/utils"
"github.com/kitex-contrib/config-etcd/utils"
)

func WithDegradation(dest, src string, etcdClient etcd.Client, uniqueID int64, opts utils.Options) []client.Option {
Expand Down
3 changes: 1 addition & 2 deletions client/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ package client
import (
"context"

"github.com/kitex-contrib/config-etcd/pkg/utils"

"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/retry"
"github.com/kitex-contrib/config-etcd/etcd"
"github.com/kitex-contrib/config-etcd/utils"
)

// WithRetryPolicy sets the retry policy from etcd configuration center.
Expand Down
3 changes: 1 addition & 2 deletions client/rpc_timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ package client
import (
"context"

"github.com/kitex-contrib/config-etcd/pkg/utils"

"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/rpctimeout"
"github.com/kitex-contrib/config-etcd/etcd"
"github.com/kitex-contrib/config-etcd/utils"
)

// WithRPCTimeout sets the RPC timeout policy from etcd configuration center.
Expand Down
2 changes: 1 addition & 1 deletion client/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package client
import (
"github.com/cloudwego/kitex/client"
"github.com/kitex-contrib/config-etcd/etcd"
"github.com/kitex-contrib/config-etcd/pkg/utils"
"github.com/kitex-contrib/config-etcd/utils"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion example/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/cloudwego/kitex/pkg/klog"
etcdclient "github.com/kitex-contrib/config-etcd/client"
"github.com/kitex-contrib/config-etcd/etcd"
"github.com/kitex-contrib/config-etcd/pkg/utils"
"github.com/kitex-contrib/config-etcd/utils"
)

type configLog struct{}
Expand Down
37 changes: 37 additions & 0 deletions pkg/degradation/degradation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2023 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"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/thriftgo/pkg/test"
"testing"
)

var errFake = errors.New("fake error")

func invoke(ctx context.Context, request, response interface{}) error {
return errFake
}

func TestNewDegradationMiddleware(t *testing.T) {
container := NewContainer()
degradationMiddleware := NewDegradationMiddleware(container)
test.Assert(t, errors.Is(degradationMiddleware(invoke)(context.Background(), nil, nil), errFake))
container.NotifyPolicyChange(&Config{Enable: true, Percentage: 100})
test.Assert(t, errors.Is(degradationMiddleware(invoke)(context.Background(), nil, nil), kerrors.ErrACL))
}
10 changes: 5 additions & 5 deletions pkg/degradation/item_degradation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ import (
)

var defaultConfig = &Config{
Enabled: false,
Enable: false,
Percentage: 0,
}

type Config struct {
Enabled bool `json:"enabled"`
Enable bool `json:"enabled"`
Percentage int `json:"percentage"`
}

// DeepCopy returns a copy of the current Config
func (c *Config) DeepCopy() iface.ConfigValueItem {
result := &Config{
Enabled: c.Enabled,
Enable: c.Enable,
Percentage: c.Percentage,
}
return result
Expand All @@ -46,7 +46,7 @@ func (c *Config) DeepCopy() iface.ConfigValueItem {
// 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.Enabled == o.Enabled && c.Percentage == o.Percentage
return c.Enable == o.Enable && c.Percentage == o.Percentage
}

// Container is a wrapper for RejectFunc and Config
Expand All @@ -68,7 +68,7 @@ func (c *Container) NotifyPolicyChange(cfg *Config) {
func (c *Container) GetAclRule() acl.RejectFunc {
return func(ctx context.Context, request interface{}) (reason error) {
cfg := c.config.Load().(*Config)
if !cfg.Enabled {
if !cfg.Enable {
return nil
}
if fastrand.Intn(100) < cfg.Percentage {
Expand Down
2 changes: 1 addition & 1 deletion server/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"sync/atomic"

"github.com/kitex-contrib/config-etcd/pkg/utils"
"github.com/kitex-contrib/config-etcd/utils"

"github.com/kitex-contrib/config-etcd/etcd"

Expand Down
2 changes: 1 addition & 1 deletion server/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package server
import (
"github.com/cloudwego/kitex/server"
"github.com/kitex-contrib/config-etcd/etcd"
"github.com/kitex-contrib/config-etcd/pkg/utils"
"github.com/kitex-contrib/config-etcd/utils"
)

const (
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 697ed6f

Please sign in to comment.