github.com/cloudwego/kitex@v0.9.0/pkg/limiter/item_limiter.go (about)

     1  /*
     2   * Copyright 2023 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package limiter
    18  
    19  import (
    20  	"github.com/cloudwego/configmanager/iface"
    21  	"github.com/cloudwego/configmanager/util"
    22  )
    23  
    24  var _ iface.ConfigValueItem = (*LimiterConfig)(nil)
    25  
    26  // TypeLimiter serves as itemKey in ConfigValueImpl
    27  const TypeLimiter iface.ItemType = "limiter_config"
    28  
    29  var defaultLimiterConfig = &LimiterConfig{}
    30  
    31  // LimiterConfig represents the configuration for kitex server limiter
    32  // zero value means no limit.
    33  type LimiterConfig struct {
    34  	ConnectionLimit int64 `json:"connection_limit"`
    35  	QPSLimit        int64 `json:"qps_limit"`
    36  }
    37  
    38  // NewLimiterConfig decodes json bytes into a newly allocated LimiterConfig object
    39  var NewLimiterConfig = util.JsonInitializer(func() iface.ConfigValueItem {
    40  	return &LimiterConfig{}
    41  })
    42  
    43  // CopyDefaultLimitConfig copies the default limiter configuration and returns a new instance of it
    44  func CopyDefaultLimitConfig() iface.ConfigValueItem {
    45  	return defaultLimiterConfig.DeepCopy()
    46  }
    47  
    48  // DeepCopy makes a deep copy of LimiterConfig struct and returns a new instance of iface.ConfigValueItem
    49  func (l *LimiterConfig) DeepCopy() iface.ConfigValueItem {
    50  	return &LimiterConfig{
    51  		ConnectionLimit: l.ConnectionLimit,
    52  		QPSLimit:        l.QPSLimit,
    53  	}
    54  }
    55  
    56  // EqualsTo determines if the LimiterConfig is equal to the given ConfigValueItem.
    57  func (l *LimiterConfig) EqualsTo(item iface.ConfigValueItem) bool {
    58  	o := item.(*LimiterConfig)
    59  	return l.ConnectionLimit == o.ConnectionLimit &&
    60  		l.QPSLimit == o.QPSLimit
    61  }