1Checkout gọi GhnFeeClient.quote(req), nhưng Spring AOP chuyển lời gọi vào CircuitBreaker "ghn" trước. Mạch CLOSED nên lời gọi được cho phép và kết quả sẽ được theo dõi.Checkout calls GhnFeeClient.quote(req), but Spring AOP routes it through CircuitBreaker "ghn" first. The circuit is CLOSED, so the call is permitted and its outcome will be tracked.
2quote() gọi http.post() của RestClient tới GHN; GHN trả 22.000đ. GhnFeeClient lưu cache.put(1442, 22000) để dành cho fallback, và breaker ghi một mẫu thành công.quote() calls RestClient http.post() to GHN, which returns 22,000 VND. GhnFeeClient stores cache.put(1442, 22000) for later fallbacks, and the breaker records a successful sample.
3GHN bắt đầu timeout. Mỗi lời gọi lỗi rơi vào overload fallbackFee(req, Exception): ghi log.warn rồi trả cache.get(...).orElse(FLAT_FEE). Lỗi này được đếm vào SlidingWindow.GHN starts timing out. Each failed call lands in the fallbackFee(req, Exception) overload: it logs a warning and returns cache.get(...).orElse(FLAT_FEE). The failure is counted in the SlidingWindow.
4Cửa sổ có 10 lỗi trong 20 lời gọi, chạm failure-rate-threshold 50%: mạch CLOSED -> OPEN. BreakerAlerts nghe onStateTransition trên breaker lấy từ CircuitBreakerRegistry và gửi hai cảnh báo Slack.The window holds 10 failures in 20 calls, reaching failure-rate-threshold 50%: the circuit goes CLOSED -> OPEN. BreakerAlerts listens via onStateTransition on the breaker from CircuitBreakerRegistry and sends two Slack alerts.
5Đơn mới khi OPEN: GhnFeeClient.quote() không chạy, RestClient và GHN không bị gọi. Overload fallbackFee(req, CallNotPermittedException) trả phí cache 22.000đ cho quận 1442; quận chưa có cache nhận FLAT_FEE 30.000đ.A new order while OPEN: GhnFeeClient.quote() does not run, so RestClient and GHN are not called. The fallbackFee(req, CallNotPermittedException) overload returns the cached 22,000 VND for district 1442; a district with no cache gets FLAT_FEE 30,000 VND.
6Hết wait-duration-in-open-state 30s, mạch sang HALF_OPEN và cho tối đa 3 lời gọi thử đi qua GhnFeeClient tới GHN. Lần thử đầu thành công và nhanh.After wait-duration-in-open-state 30s, the circuit moves to HALF_OPEN and admits up to 3 probes through GhnFeeClient to GHN. The first probe succeeds quickly.
7Cả 3 lần thử đều ổn nên mạch HALF_OPEN -> CLOSED, cửa sổ thống kê bắt đầu lại. Nếu một lần thử lỗi tới ngưỡng, mạch sẽ OPEN lại thêm 30s.All 3 probes succeed, so the circuit goes HALF_OPEN -> CLOSED and the window starts fresh. Had the probes failed to the threshold, it would reopen for another 30s.
8Bản Go chạy cùng luồng: FeeService.Quote gọi cb.Execute của gobreaker; lỗi hoặc ErrOpenState rơi vào nhánh default trả cache.GetOr(district, 30_000). Struct Breaker tự viết cho thấy bên trong: OPEN thì Call(fn) trả ErrOpen ngay, không gọi mạng.The Go version runs the same flow: FeeService.Quote calls gobreaker's cb.Execute; an error or ErrOpenState falls to the default branch returning cache.GetOr(district, 30_000). The hand-rolled Breaker struct shows the inside: while OPEN, Call(fn) returns ErrOpen at once without a network call.