1Để dễ quan sát, Go Gateway được tạo bằng NewGateway(client, 2): sem là make(chan struct{}, 2). IssueAll chạy wg.Add(1) và một goroutine cho mỗi hoá đơn, nên ba goroutine #1, #2, #3 cùng gọi g.Issue.For clarity, the Go Gateway is built with NewGateway(client, 2): sem is make(chan struct{}, 2). IssueAll does wg.Add(1) and starts one goroutine per invoice, so goroutines #1, #2 and #3 all call g.Issue.
2#1 vào select và case g.sem <- struct{}{} thành công: chiếm 1/2 permit. Nó đăng ký defer func() { <-g.sem }() để chắc chắn trả lượt cả khi lỗi hay panic.#1 enters the select and case g.sem <- struct{}{} succeeds: it holds 1 of 2 permits. It registers defer func() { <-g.sem }() so the permit is returned even on error or panic.
3#2 cũng gửi được vì buffer còn một ô (2/2). Cả hai gọi g.client.Issue(ctx, inv) song song; EInvoiceClient nhận đúng 2 lời gọi.#2 can send too because one buffer slot remains (2 of 2). Both call g.client.Issue(ctx, inv) in parallel; EInvoiceClient receives exactly 2 calls.
4#3 thử gửi nhưng buffer đầy nên bị chặn trong select, goroutine được tạm dừng (không quay vòng). Nếu ctx bị huỷ trong lúc chờ, nhánh <-ctx.Done() trả "", ctx.Err() và IssueAll log lỗi.#3 tries to send but the buffer is full, so it blocks in the select and the goroutine parks (no busy-wait). If ctx is cancelled while waiting, the <-ctx.Done() case returns "", ctx.Err() and IssueAll logs the error.
5Lời gọi của #1 trả về, defer <-g.sem lấy một phần tử ra và trả lượt. Lệnh gửi của #3 hoàn tất ngay, #3 gọi API trong khi #2 vẫn chạy: tối đa vẫn 2 lời gọi. wg.Wait() chờ cả ba xong.#1's call returns and the deferred <-g.sem removes an element, returning the permit. #3's send completes at once and it calls the API while #2 is still running: still at most 2 calls. wg.Wait() waits for all three.
6Java MonthEndBatch.issueAll mở Executors.newVirtualThreadPerTaskExecutor() và submit một virtual thread cho mỗi hoá đơn, gọi gateway.issue(inv). Hàng nghìn virtual thread rất rẻ; giới hạn nằm ở InvoiceGateway.Java MonthEndBatch.issueAll opens Executors.newVirtualThreadPerTaskExecutor() and submits one virtual thread per invoice that calls gateway.issue(inv). Thousands of virtual threads are cheap; the limit lives in InvoiceGateway.
7InvoiceGateway.issue gọi permits.tryAcquire(3, TimeUnit.SECONDS) trên Semaphore(5, true). Có permit thì try { client.issue(invoice) } finally { permits.release() }; chờ quá 3 giây thì ném GatewayBusyException và MonthEndBatch chỉ log cảnh báo.InvoiceGateway.issue calls permits.tryAcquire(3, TimeUnit.SECONDS) on a Semaphore(5, true). With a permit it does try { client.issue(invoice) } finally { permits.release() }; after 3 seconds without one it throws GatewayBusyException and MonthEndBatch only logs a warning.
8InvoiceService làm cùng việc bằng khai báo: @Bulkhead(name = "einvoice") với max-concurrent-calls 5, max-wait-duration 3s. Đầy quá 3 giây thì resilience4j ném BulkheadFullException và gọi queueForLater, trả InvoiceNo.pending(invoice.id()).InvoiceService does the same declaratively: @Bulkhead(name = "einvoice") with max-concurrent-calls 5 and max-wait-duration 3s. Full for more than 3 seconds, resilience4j raises BulkheadFullException and calls queueForLater, which returns InvoiceNo.pending(invoice.id()).
9Semaphore có trọng số: ResizeAll tính cost = min(SizeMB*4, 512) và memBudget.Acquire(ctx, cost) trên NewWeighted(512), tức permit đo bằng MB RAM. Đủ ngân sách thì g.Go chạy resize với defer memBudget.Release(cost); cuối cùng g.Wait().A weighted semaphore: ResizeAll computes cost = min(SizeMB*4, 512) and calls memBudget.Acquire(ctx, cost) on NewWeighted(512), so permits are MB of RAM. With enough budget, g.Go runs resize with defer memBudget.Release(cost); finally g.Wait().