github.com/polarismesh/polaris@v1.17.8/test/integrate/resource/ratelimit_config.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package resource
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/golang/protobuf/ptypes/duration"
    24  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    25  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    26  	apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage"
    27  
    28  	"github.com/polarismesh/polaris/common/utils"
    29  )
    30  
    31  /**
    32   * @brief 创建测试限流规则
    33   */
    34  func CreateRateLimits(services []*apiservice.Service) []*apitraffic.Rule {
    35  	var rateLimits []*apitraffic.Rule
    36  	for index := 0; index < 2; index++ {
    37  		rateLimit := &apitraffic.Rule{
    38  			Name:      utils.NewStringValue(fmt.Sprintf("rlimit-%d", index)),
    39  			Service:   services[index].GetName(),
    40  			Namespace: services[index].GetNamespace(),
    41  			Priority:  utils.NewUInt32Value(uint32(index)),
    42  			Resource:  apitraffic.Rule_CONCURRENCY,
    43  			Type:      apitraffic.Rule_LOCAL,
    44  			Arguments: []*apitraffic.MatchArgument{{
    45  				Type: apitraffic.MatchArgument_CUSTOM,
    46  				Key:  fmt.Sprintf("name-%d", index),
    47  				Value: &apimodel.MatchString{
    48  					Type:  apimodel.MatchString_REGEX,
    49  					Value: utils.NewStringValue(fmt.Sprintf("value-%d", index)),
    50  				},
    51  			}, {Type: apitraffic.MatchArgument_CUSTOM,
    52  				Key: fmt.Sprintf("name-%d", index+1),
    53  				Value: &apimodel.MatchString{
    54  					Type:  apimodel.MatchString_EXACT,
    55  					Value: utils.NewStringValue(fmt.Sprintf("value-%d", index+1)),
    56  				}}},
    57  			Amounts: []*apitraffic.Amount{
    58  				{
    59  					MaxAmount: utils.NewUInt32Value(uint32(index)),
    60  					ValidDuration: &duration.Duration{
    61  						Seconds: int64(index),
    62  						Nanos:   int32(index),
    63  					},
    64  				},
    65  			},
    66  			Action:  utils.NewStringValue("REJECT"),
    67  			Disable: utils.NewBoolValue(true),
    68  			Adjuster: &apitraffic.AmountAdjuster{
    69  				Climb: &apitraffic.ClimbConfig{
    70  					Enable: utils.NewBoolValue(true),
    71  					Metric: &apitraffic.ClimbConfig_MetricConfig{
    72  						Window: &duration.Duration{
    73  							Seconds: int64(index),
    74  							Nanos:   int32(index),
    75  						},
    76  						Precision: utils.NewUInt32Value(uint32(index)),
    77  						ReportInterval: &duration.Duration{
    78  							Seconds: int64(index),
    79  							Nanos:   int32(index),
    80  						},
    81  					},
    82  				},
    83  			},
    84  			RegexCombine: utils.NewBoolValue(true),
    85  			AmountMode:   apitraffic.Rule_SHARE_EQUALLY,
    86  			Failover:     apitraffic.Rule_FAILOVER_PASS,
    87  		}
    88  		rateLimits = append(rateLimits, rateLimit)
    89  	}
    90  	return rateLimits
    91  }
    92  
    93  /**
    94   * @brief 更新测试限流规则
    95   */
    96  func UpdateRateLimits(rateLimits []*apitraffic.Rule) {
    97  	for _, rateLimit := range rateLimits {
    98  		rateLimit.Arguments = []*apitraffic.MatchArgument{
    99  			{
   100  				Type: apitraffic.MatchArgument_CUSTOM,
   101  				Key:  "key1",
   102  				Value: &apimodel.MatchString{
   103  					Type:  apimodel.MatchString_REGEX,
   104  					Value: utils.NewStringValue("value-1"),
   105  				},
   106  			},
   107  		}
   108  	}
   109  }