github.com/polarismesh/polaris@v1.17.8/common/conn/limit/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 connlimit
    19  
    20  import (
    21  	"time"
    22  
    23  	"github.com/mitchellh/mapstructure"
    24  
    25  	"github.com/polarismesh/polaris/common/log"
    26  )
    27  
    28  // Config 连接限制配置
    29  type Config struct {
    30  	// 开启连接限制
    31  	OpenConnLimit bool `mapstructure:"openConnLimit"`
    32  
    33  	// 单个host最大的连接数,必须 > 1
    34  	MaxConnPerHost int `mapstructure:"maxConnPerHost"`
    35  
    36  	// 当前协议监听端口的最大连接数
    37  	// 兼容老版本,> 1,则开启listen的全局限制;< 1则不开启listen的全局限制
    38  	MaxConnLimit int `mapstructure:"maxConnLimit"`
    39  
    40  	// 白名单,不进行host连接数限制
    41  	WhiteList string `mapstructure:"whiteList"`
    42  
    43  	// 读超时
    44  	ReadTimeout time.Duration `mapstructure:"readTimeout"`
    45  
    46  	// 回收连接统计数据的周期
    47  	PurgeCounterInterval time.Duration `mapstructure:"purgeCounterInterval"`
    48  
    49  	// 回收连接的最大超时时间
    50  	PurgeCounterExpire time.Duration `mapstructure:"purgeCounterExpire"`
    51  }
    52  
    53  // ParseConnLimitConfig 解析配置
    54  func ParseConnLimitConfig(raw map[interface{}]interface{}) (*Config, error) {
    55  	if raw == nil {
    56  		return nil, nil
    57  	}
    58  
    59  	config := &Config{}
    60  	decodeConfig := &mapstructure.DecoderConfig{
    61  		DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
    62  		Result:     config,
    63  	}
    64  	decoder, err := mapstructure.NewDecoder(decodeConfig)
    65  	if err != nil {
    66  		log.Errorf("conn limit new decoder err: %s", err.Error())
    67  		return nil, err
    68  	}
    69  
    70  	err = decoder.Decode(raw)
    71  	if err != nil {
    72  		log.Errorf("parse conn limit config(%+v) err: %s", raw, err.Error())
    73  		return nil, err
    74  	}
    75  
    76  	return config, nil
    77  }