1Run(ctx, gw, txIDs, 3) tạo jobs := make(chan string) và results := make(chan Result), rồi for range n: wg.Add(1) và go worker(ctx, gw, jobs, results). Ba worker cố định cùng chờ ở for txID := range jobs, thay vì một goroutine cho mỗi giao dịch.Run(ctx, gw, txIDs, 3) makes jobs := make(chan string) and results := make(chan Result), then for range n: wg.Add(1) and go worker(ctx, gw, jobs, results). Three fixed workers wait at for txID := range jobs instead of one goroutine per transaction.
2Goroutine job feeder gửi jobs <- "TX-0001". Channel không buffer nên lệnh gửi chờ tới khi có worker rảnh nhận; nếu ctx bị huỷ, nhánh <-ctx.Done() cho feeder thoát.The job feeder goroutine sends jobs <- "TX-0001". The channel is unbuffered, so the send waits until a worker is free to receive; if ctx is cancelled, the <-ctx.Done() case lets the feeder exit.
3worker 0 nhận TX-0001, worker 1 và worker 2 nhận TX-0002, TX-0003; mỗi worker gọi gw.CheckStatus(ctx, txID). Khi cả ba đang bận, lệnh gửi TX-0004 phải chờ: mức đồng thời luôn là 3.worker 0 receives TX-0001, worker 1 and worker 2 receive TX-0002 and TX-0003; each calls gw.CheckStatus(ctx, txID). While all three are busy, the TX-0004 send must wait: concurrency stays at 3.
4TX-0002 xong trước: worker 1 gửi results <- Result{TxID, Status, Err}. Lỗi nằm trong Result.Err nên một giao dịch lỗi không làm hỏng cả lô.TX-0002 finishes first: worker 1 sends results <- Result{TxID, Status, Err}. The error travels in Result.Err, so one failed transaction does not sink the batch.
5Run đọc for r := range results và append vào out ngay khi worker còn chạy, nên lệnh gửi của worker không bị kẹt. worker 1 rảnh lại nhận TX-0004 bằng chính goroutine cũ.Run reads for r := range results and appends to out while workers are still running, so worker sends never get stuck. worker 1, free again, takes TX-0004 with the same goroutine.
6Feeder gửi hết txIDs và defer close(jobs): các vòng range của worker kết thúc, defer wg.Done() chạy. Goroutine results closer đợi wg.Wait() rồi mới close(results), nên không worker nào gửi vào channel đã đóng; Run trả out.The feeder sends every txID and runs defer close(jobs): the workers' range loops end and defer wg.Done() runs. The results closer goroutine waits on wg.Wait() and only then calls close(results), so no worker sends to a closed channel; Run returns out.
7Java: PoolConfig.reconcilePool() tạo ThreadPoolTaskExecutor với core = max = 8 thread reconcile-, queueCapacity 500 và CallerRunsPolicy (queue đầy thì thread gọi tự chạy job, tạo backpressure). ReconcileService.reconcile gọi pool.submit(() -> new Result(txId, gateway.checkStatus(txId))) cho mỗi giao dịch.Java: PoolConfig.reconcilePool() builds a ThreadPoolTaskExecutor with core = max = 8 reconcile- threads, queueCapacity 500 and CallerRunsPolicy (a full queue makes the caller run the job itself, creating backpressure). ReconcileService.reconcile calls pool.submit(() -> new Result(txId, gateway.checkStatus(txId))) for each transaction.
8Mỗi job hoàn tất Future<Result> của nó; ReconcileService lần lượt f.get(). Job ném lỗi thì f.get() ném ExecutionException và service thêm Result("?", "ERROR: ...") thay vì dừng cả lô.Each job completes its Future<Result>; ReconcileService calls f.get() on each in turn. A failed job makes f.get() throw ExecutionException, and the service adds Result("?", "ERROR: ...") instead of stopping the batch.
9Biến thể VirtualReconciler: newVirtualThreadPerTaskExecutor() tạo một virtual thread cho mỗi giao dịch (không gom pool), còn Semaphore(8) với acquireUninterruptibly() / release() giữ tối đa 8 lời gọi gateway.checkStatus cùng lúc.The VirtualReconciler variant: newVirtualThreadPerTaskExecutor() creates one virtual thread per transaction (no pooling), while Semaphore(8) with acquireUninterruptibly() / release() keeps at most 8 gateway.checkStatus calls at once.