This is a creation in Article, where the information may have evolved or changed.
Access restrictions for interfaces, 10 minutes, interface access limit 100 times
Write based on go language, pull out the unified configuration.
Func checkratelimit (IP, request, action String) bool {current: = Int (time. Now (). Unix ()) Currentstr: = StrConv. Itoa (current)//limit 100 times//timeset 600 sec.//limit 600 seconds maximum access 100 limit, Timeset: = Getratelimitconfig () allowancestr, TIMESTAMPSTR: = loadallowance (IP, request, action) allowance, _: = StrConv. Atoi (ALLOWANCESTR) Timestamp, _: = StrConv. Atoi (TIMESTAMPSTR) allowance + = Int (current-timestamp) * Limit/timesetif allowance > Limit {allowance = Limit}if allow Ance < 1 {saveallowance (IP, request, action, "0", CURRENTSTR)//returns TRUE to represent rate exceeded, error output return true} else {allowancestr = St Rconv. Itoa (allowance-1) saveallowance (IP, request, action, ALLOWANCESTR, CURRENTSTR)//return False to represent rate not exceeding return False}}func Loadallowance (IP, request, action String) (allowance, timestamp string) {redisconfig: = Getredisconfig () rs, err: = CACHE.N Ewcache ("Redis", redisconfig) if err! = Nil {fmt. PRINTLN (Err) Return}res, _: = (Redis. String (Rs. Get (ip+ "_" +request), err)) If Len (res) = = 0 {currentstr: = string (time). Now (). Unix ()) defAultlimitint, _: = Getratelimitconfig () defaultlimitstr: = StrConv. Itoa (defaultlimitint) allowance, timestamp = Defaultlimitstr, currentstr} else {kv: = strings. Split (res, "-") allowance, timestamp = kv[0], Kv[1]}return}func getratelimitconfig () (limit, Timeset int) {limit, _ = Beego . Appconfig.int ("Ratelimit") timeset, _ = Beego. Appconfig.int ("Ratetimeset") return}func saveallowance (IP, request, action, allowance, current string) {redisconfig: = Getredisconfig () rs, err: = cache. Newcache ("Redis", redisconfig) if err! = Nil {fmt. PRINTLN (Err) return}rs. Put (ip+ "_" +request, allowance+ "-" +current, 600*time. Second)}func getredisconfig () string {env: = Beego. Appconfig.string ("RunMode") Conn: = Beego. Appconfig.string ("Redisconn") Pass: = Beego. Appconfig.string ("Redispass") Name: = Beego. Appconfig.string ("Redisname") Redishash: = Make (map[string]interface{}) redishash["conn"] = connredishash["key"] = Nameif env = = "prod" {redishash["password"] = Pass}redisconfig, _: = json. Marshal (Redishash) return string (reDisconfig)}
Ability to fully restrict access to the interface