In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly analyzes the relevant knowledge points on how to realize the daily limit of users in Go. The content is detailed and easy to understand, and the operation details are reasonable. It has certain reference value. If you are interested, you may wish to follow Xiao Bian to have a look. Let's follow Xiao Bian to learn more about how to realize the daily limit of users in Go.
Daily limit for users in Go (e.g. only three benefits per day)
If you write a bug management system and use PeriodLimit, you can limit each tester to one bug per day. Is it easier to work?
The essential reason for the popularity of microservice architecture today is to reduce the overall complexity of the system, spread the system risk evenly to the subsystems to maximize the stability of the system, and divide the domain into different subsystems. After each subsystem can be independently developed, tested and released, the rhythm and efficiency of R & D can be significantly improved.
However, it also brings problems, such as: the call link is too long, the deployment architecture complexity is increased, and various middleware needs to support distributed scenarios. In order to ensure the normal operation of microservices, service governance is indispensable, usually including: current limiting, degradation, fuse.
Current limiting refers to limiting the frequency of interface calls so as not to drag down the system beyond the upper load limit.
The following mainly explains the fixed-time window current-limiting algorithm.
working principle
From a certain time point, the number of requests coming each time is +1, and whether the number of requests in the current time window exceeds the limit is judged at the same time. If the number exceeds the limit, the request is rejected, and then the counter is cleared to wait for the request at the beginning of the next time window.
advantages and disadvantages
advantages
The implementation is simple and efficient, especially suitable for limiting scenarios such as a user can only send 10 articles a day, only send 5 SMS Captcha, and only try to log in 5 times. Such scenarios are very common in actual business.
disadvantages
The disadvantage of fixed time window throttling is that it cannot handle critical area request burst scenarios.
Assuming that there are 100 requests per 1s, the user initiates 200 requests within 1s from the middle 500ms, and all 200 requests can be passed at this time. This is inconsistent with our expectation of 100 times of current limiting in 1s, which stems from the fact that the fine granularity of current limiting is too coarse.
go-zero code implementation
core/limit/periodlimit.go
Redis expiration time is used in go-zero to simulate a fixed time window.
redis lua script:
-- KYES[1]: Limiter key-- ARGV[1]:qos, Maximum requests per unit time-- ARGV[2]: unit limit window time--maximum number of requests, equal to p.quotalocal limit = tonumber(ARGV[1])--window is a unit current-limiting period, here using expired simulation window effect, equal to p.permitlocal window = tonumber(ARGV[2])--Number of requests +1, Total number of requests obtained local current = redis.call ("INCRBY",KYES[1], 1)--if this is the first request, set the expiration time and return success if current == 1 then redis.call("expire",KYES[1],window) return 1--if the number of current requests is less than limit elseif current
< limit then return 1-- 如果当前请求数量==limit则返回 最后一次请求elseif current == limit then return 2-- 请求数量>limit returns failure else return 0end
Fixed Time Window Current Limiter Definition
type ( // PeriodOption defines the method to customize a PeriodLimit. // go Common option parameter mode//If there are many parameters, it is recommended to use this mode to set the parameter PeriodOption func(l *PeriodLimit) // A PeriodLimit is used to limit requests during a period of time. //Fixed Time Window Current Limiter PeriodLimit struct { //window size, unit s period int //request upper limit quota int //Storage limitStore *redis.Redis // key prefix keyPrefix string //Linear current limiting. Periodic current limiting can be achieved when this option is enabled. //For example, when quota=5, the actual value of quota may be 5.4.3.2.1, showing periodic changes. align bool })
Note that the align parameter, align=true, causes the request cap to change periodically.
For example, when quota=5, the actual quota may be 5.4.3.2.1, showing periodic changes.
throttle logic
In fact, the current limiting logic is implemented in the lua script above. What needs attention is the return value.
0: indicates error, such as redis fault, overload
1: Permitted
2: Allowed, but the upper limit has been reached in the current window. If it is a batch business, you can sleep and wait for the next window (the author considers it very carefully).
3: Rejected
// Take requests a permit, it returns the permit state.// Perform current limiting//Note the return value:// 0: indicates error, such as redis fault or overload// 1: Allow// 2: Allow but the upper limit of the current window has been reached// 3: Reject func (h *PeriodLimit) Take(key string) (int, error) { //Execute lua script resp, err := h.limitStore.Eval(periodScript, []string{h.keyPrefix + key}, []string{ strconv.Itoa(h.quota), strconv.Itoa(h.calcExpireSeconds()), }) if err != nil { return Unknown, err } code, ok := resp. (int64) if ! ok { return Unknown, ErrUnknownCode } switch code { case internalOverQuota: return OverQuota, nil case internalAllowed: return Allowed, nil case internalHitQuota: return HitQuota, nil default: return Unknown, ErrUnknownCode }}
This fixed window current limit may be used to limit, for example, a user to send 5 Captcha SMS messages a day, in which case we need to correspond to the Chinese time zone (GMT+8), and in fact, the current limit time should start from zero, in which case we need extra alignment (set align to true).
//Calculate the expiration time, that is, the window time size//If align==true//Linear current limiting is enabled, periodic current limiting can be implemented.//For example, when quota=5, the actual value of quota may be 5.4.3.2.1, showing periodic changes func (h *PeriodLimit) calcExpireSeconds() int { if h.align { now := time.Now() _, offset := now.Zone() unix := now.Unix() + int64(offset) return h.period - int(unix%int64(h.period)) } return h.period} This article mainly analyzes the relevant knowledge points on how to realize the daily limit of users in Go. The content is detailed and easy to understand, and the operation details are reasonable. It has certain reference value. If you are interested, you may wish to follow Xiao Bian to have a look. Let's follow Xiao Bian to learn more about how to realize the daily limit of users in Go.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.