1Request POST /otp?phone=0909… đi qua PhoneLimiter.Middleware. Middleware đọc r.FormValue("phone"); thiếu số điện thoại thì trả 400 "phone is required" trước khi đụng tới limiter.The POST /otp?phone=0909… request goes through PhoneLimiter.Middleware. It reads r.FormValue("phone"); without a phone it returns 400 "phone is required" before touching any limiter.
2p.get(phone) khoá mu rồi tra map limiters. Lần đầu gặp số này, nó tạo rate.NewLimiter(rate.Every(time.Minute), 3): xô bắt đầu đầy 3 token, nạp 1 token mỗi phút.p.get(phone) locks mu and looks up the limiters map. The first time it sees this phone it creates rate.NewLimiter(rate.Every(time.Minute), 3): the bucket starts full with 3 tokens and refills 1 per minute.
3Ba request liên tiếp cùng số gọi Allow() và đều lấy được token, nên Middleware gọi next.ServeHTTP(w, r) để handler OTP chạy.Three back-to-back requests for the same phone call Allow() and each takes a token, so Middleware calls next.ServeHTTP(w, r) and the OTP handler runs.
4Request thứ tư trong cùng phút: Allow() trả false vì xô đã cạn. Middleware đặt Retry-After: 60 và trả 429, không chờ và không gọi nhà cung cấp SMS.The fourth request in the same minute: Allow() returns false because the bucket is empty. Middleware sets Retry-After: 60 and returns 429, without waiting and without calling the SMS provider.
5Một phút sau, Allow() tính lại số token theo thời gian đã trôi (không có goroutine nào đổ token): có 1 token nên request được đi qua. Burst hấp thụ đợt đầu, tốc độ nạp giữ nhịp dài hạn.A minute later, Allow() recomputes tokens from elapsed time (no goroutine adds them): one token is available, so the request passes. Burst absorbs the first rush; the refill rate keeps the long-run pace.
6Java làm cùng việc trong OtpRateLimitInterceptor.preHandle: buckets.computeIfAbsent(phone, this::newBucket) tạo Bucket capacity(3).refillGreedy(1, 1 phút), rồi tryConsumeAndReturnRemaining(1). Lấy được thì đặt header X-RateLimit-Remaining và return true.Java does the same in OtpRateLimitInterceptor.preHandle: buckets.computeIfAbsent(phone, this::newBucket) builds a Bucket with capacity(3).refillGreedy(1, 1 minute), then tryConsumeAndReturnRemaining(1). If consumed, it sets X-RateLimit-Remaining and returns true.
7Lần thứ tư, probe.isConsumed() là false: waitSec = getNanosToWaitForRefill() đổi sang giây + 1, interceptor đặt status 429 và Retry-After rồi return false để request dừng ở đây.On the fourth call probe.isConsumed() is false: waitSec = getNanosToWaitForRefill() in seconds + 1, the interceptor sets status 429 and Retry-After, then returns false so the request stops here.
8Chiều ra trong Go: SmsSender.Send gọi s.limiter.Wait(ctx) trên limiter NewLimiter(100, 20), tức là CHỜ token thay vì từ chối, rồi mới s.provider.Send. Nếu ctx bị huỷ trong lúc chờ, Send trả lỗi.Outbound in Go: SmsSender.Send calls s.limiter.Wait(ctx) on a NewLimiter(100, 20), so it WAITS for a token instead of rejecting, then calls s.provider.Send. If ctx is cancelled while waiting, Send returns the error.
9Java SmsSender dùng resilience4j RateLimiter.of("sms-provider") với limitForPeriod(100) mỗi 1 giây và timeoutDuration 500ms. Không có permit trong 500ms thì decorateRunnable ném RequestNotPermitted và send trả false để bên gọi xếp hàng thử lại.Java SmsSender uses resilience4j RateLimiter.of("sms-provider") with limitForPeriod(100) per 1 second and timeoutDuration 500ms. Without a permit within 500ms, decorateRunnable throws RequestNotPermitted and send returns false so the caller can queue a retry.