fix(backend-java): 收口并发资源与恢复链路
This commit is contained in:
@@ -45,14 +45,13 @@ public class TaskQueueGate implements TaskExecutor {
|
||||
recordRejected("invalid-input");
|
||||
throw new IllegalArgumentException("task 不能为 null");
|
||||
}
|
||||
if (waiting.get() >= maxWaiting) {
|
||||
if (!tryReserveWaitingSlot()) {
|
||||
recordRejected("queue-full");
|
||||
log.warn("[task-queue][gate] waiting queue full, reject submit waiting={} limit={}",
|
||||
waiting.get(), maxWaiting);
|
||||
throw new TaskRejectedException("task 等待队列已满,limit=" + maxWaiting
|
||||
+ ", waiting=" + waiting.get());
|
||||
}
|
||||
waiting.incrementAndGet();
|
||||
long queuedAt = System.nanoTime();
|
||||
try {
|
||||
delegate.execute(() -> {
|
||||
@@ -83,6 +82,22 @@ public class TaskQueueGate implements TaskExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 原子预留一个等待槽位。不能使用“先 get 再 increment”,否则并发提交
|
||||
* 会同时通过检查,导致等待数量突破 maxWaiting。
|
||||
*/
|
||||
private boolean tryReserveWaitingSlot() {
|
||||
while (true) {
|
||||
int current = waiting.get();
|
||||
if (current >= maxWaiting) {
|
||||
return false;
|
||||
}
|
||||
if (waiting.compareAndSet(current, current + 1)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void recordRejected(String reason) {
|
||||
MeterRegistry registry = meterRegistry();
|
||||
if (registry != null) {
|
||||
|
||||
Reference in New Issue
Block a user