Go implements interface access rate limit

Source: Internet
Author: User
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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.