github.com/polarismesh/polaris@v1.17.8/plugin/ratelimit/lrurate/lrurate.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 lrurate 19 20 import ( 21 "errors" 22 23 "github.com/polarismesh/polaris/plugin" 24 ) 25 26 const ( 27 // PluginName lru rate plugin 28 PluginName = "lrurate" 29 ) 30 31 var ( 32 rateLimitIPLruSize int 33 rateLimitIPRate int 34 rateLimitIPBurst int 35 rateLimitServiceLruSize int 36 rateLimitServiceRate int 37 rateLimitServiceBurst int 38 ) 39 40 // init 自注册到插件列表 41 func init() { 42 plugin.RegisterPlugin(PluginName, &LRURate{}) 43 } 44 45 // LRURate Ratelimit 46 type LRURate struct{} 47 48 // Name 返回插件名 49 func (m *LRURate) Name() string { 50 return PluginName 51 } 52 53 // Initialize 初始化函数 54 func (m *LRURate) Initialize(c *plugin.ConfigEntry) error { 55 if err := parseRateLimitIPOption(c.Option); err != nil { 56 return err 57 } 58 if err := parseRateLimitServiceOption(c.Option); err != nil { 59 return err 60 } 61 62 if err := initEnv(); err != nil { 63 return err 64 } 65 66 return nil 67 } 68 69 // parseRateLimitIPOption 获取IP相关的参数 70 func parseRateLimitIPOption(opt map[string]interface{}) error { 71 var ok bool 72 var val interface{} 73 74 if val = opt["rateLimitIPLruSize"]; val == nil { 75 return errors.New("not found ratelimit::lrurate::rateLimitIPLruSize") 76 } 77 78 if rateLimitIPLruSize, ok = val.(int); !ok || rateLimitIPLruSize <= 0 { 79 return errors.New("invalid ratelimit::lrurate::rateLimitIPLruSize, must be int and > 0") 80 } 81 82 if val = opt["rateLimitIPRate"]; val == nil { 83 return errors.New("not found ratelimit::lrurate::rateLimitIPRate") 84 } 85 86 if rateLimitIPRate, ok = val.(int); !ok || rateLimitIPRate <= 0 { 87 return errors.New("invalid ratelimit::lrurate::rateLimitIPRate, must be int and > 0") 88 } 89 90 if val = opt["rateLimitIPBurst"]; val == nil { 91 return errors.New("not found ratelimit::lrurate::rateLimitIPBurst") 92 } 93 94 if rateLimitIPBurst, ok = val.(int); !ok || rateLimitIPBurst <= 0 { 95 return errors.New("invalid ratelimit::lrurate::rateLimitIPBurst, must be int and > 0") 96 } 97 98 return nil 99 } 100 101 // parseRateLimitServiceOption 获取service相关的参数 102 func parseRateLimitServiceOption(opt map[string]interface{}) error { 103 var ok bool 104 var val interface{} 105 106 if val = opt["rateLimitServiceLruSize"]; val == nil { 107 return errors.New("not found ratelimit::lrurate::rateLimitServiceLruSize") 108 } 109 110 if rateLimitServiceLruSize, ok = val.(int); !ok || rateLimitServiceLruSize <= 0 { 111 return errors.New("invalid ratelimit::lrurate::rateLimitServiceLruSize, must be int and > 0") 112 } 113 114 if val = opt["rateLimitServiceRate"]; val == nil { 115 return errors.New("not found ratelimit::lrurate::rateLimitServiceRate") 116 } 117 118 if rateLimitServiceRate, ok = val.(int); !ok || rateLimitServiceRate <= 0 { 119 return errors.New("invalid ratelimit::lrurate::rateLimitServiceRate, must be int and > 0") 120 } 121 122 if val = opt["rateLimitServiceBurst"]; val == nil { 123 return errors.New("not found ratelimit::lrurate::rateLimitServiceBurst") 124 } 125 126 if rateLimitServiceBurst, ok = val.(int); !ok || rateLimitServiceBurst <= 0 { 127 return errors.New("invalid ratelimit::lrurate::rateLimitServiceBurst, must be int and > 0") 128 } 129 130 return nil 131 } 132 133 // Destroy 销毁函数 134 func (m *LRURate) Destroy() error { 135 return nil 136 } 137 138 // Allow 实现CMDB插件接口 139 func (m *LRURate) Allow(rateType plugin.RatelimitType, id string) bool { 140 switch rateType { 141 case plugin.IPRatelimit: 142 return allowIP(id) 143 case plugin.ServiceRatelimit: 144 return allowService(id) 145 } 146 147 // 默认允许访问 148 return true 149 }