github.com/polarismesh/polaris@v1.17.8/plugin/ratelimit/token/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 token 19 20 import ( 21 "fmt" 22 23 "github.com/mitchellh/mapstructure" 24 ) 25 26 // Config 限流配置类 27 type Config struct { 28 // 是否启用远程配置,默认false。TODO 暂时无远程配置,后续版本补全 29 RemoteConf bool `mapstructure:"remote-conf"` 30 31 // IP限流相关配置 32 IPLimitConf *ResourceLimitConfig `mapstructure:"ip-limit"` 33 34 // 接口限流相关配置 35 APILimitConf *APILimitConfig `mapstructure:"api-limit"` 36 37 // 基于实例的限流配置 38 InstanceLimitConf *ResourceLimitConfig `mapstructure:"instance-limit"` 39 } 40 41 // BucketRatelimit 针对令牌桶的具体配置 42 type BucketRatelimit struct { 43 // 是否开启限流 44 Open bool `mapstructure:"open"` 45 46 // 令牌桶大小 47 Bucket int `mapstructure:"bucket"` 48 49 // 每秒加入的令牌数 50 Rate int `mapstructure:"rate"` 51 } 52 53 // ResourceLimitConfig 基于资源的限流配置 54 // 资源可以是:IP,实例,服务等 55 type ResourceLimitConfig struct { 56 // 是否开启instance限流 57 Open bool `mapstructure:"open"` 58 59 // 全局限制规则,只有一条规则 60 Global *BucketRatelimit `mapstructure:"global"` 61 62 // 本地缓存最大多少个instance的限制器 63 MaxResourceCacheAmount int `mapstructure:"resource-cache-amount"` 64 65 // 白名单 66 WhiteList []string `mapstructure:"white-list"` 67 } 68 69 // APILimitConfig api限流配置 70 type APILimitConfig struct { 71 // 系统是否开启API限流 72 Open bool `mapstructure:"open"` 73 74 // 配置规则集合 75 Rules []*RateLimitRule `mapstructure:"rules"` 76 77 // 每个接口的单独配置 78 Apis []*APILimitInfo `mapstructure:"apis"` 79 } 80 81 // RateLimitRule 限流规则 82 type RateLimitRule struct { 83 // 规则名 84 Name string `mapstructure:"name"` 85 86 // 规则的限制 87 Limit *BucketRatelimit `mapstructure:"limit"` 88 } 89 90 // APILimitInfo 每个接口的单独配置信息 91 type APILimitInfo struct { 92 // 接口名,比如对于HTTP,就是:方法+URL 93 Name string `mapstructure:"name"` 94 95 // 限制规则名 96 Rule string `mapstructure:"rule"` 97 } 98 99 // decodeConfig 把map解码为Config对象 100 func decodeConfig(data map[string]interface{}) (*Config, error) { 101 if data == nil { 102 return nil, fmt.Errorf("plugin(%s) option is empty", PluginName) 103 } 104 var config Config 105 if err := mapstructure.Decode(data, &config); err != nil { 106 log.Errorf("[Plugin][%s] decode config err: %s", PluginName, err.Error()) 107 return nil, err 108 } 109 110 /*log.Infof("%+v", config) 111 log.Infof("%+v", config.IPLimitConf.Global) 112 log.Infof("%+v", config.APILimitConf) 113 for _, entry := range config.APILimitConf.Rules { 114 log.Infof("%s->%+v", entry.Name, entry.Limit) 115 } 116 for _, entry := range config.APILimitConf.Apis { 117 log.Infof("%+v", entry) 118 }*/ 119 120 return &config, nil 121 }