fix: SN命令回执改同轮返回且失败不丢,全量批处理移出Timer C消除2分钟同步周期
Check Source / check (push) Successful in 3s
Auto Release / build (push) Successful in 2m10s
Simple Test / test (push) Successful in 1s
Test Env / test (push) Successful in 1s

This commit is contained in:
youpaishidifu
2026-09-29 18:03:32 +08:00
parent ccb67a0bf5
commit 841fd928a5
2 changed files with 48 additions and 20 deletions
+25 -5
View File
@@ -1565,19 +1565,39 @@ public class SnManager implements Listener {
String resp = wm.httpGet("api/sn.php", params); String resp = wm.httpGet("api/sn.php", params);
if (resp != null && resp.contains("[")) { if (resp != null && resp.contains("[")) {
List<Map<String, Object>> cmds = parseJsonArray(resp); List<Map<String, Object>> cmds = parseJsonArray(resp);
if (!cmds.isEmpty()) {
// 改背包必须回主线程。tickSync 跑在异步线程,原先 fire-and-forget
// 提交后立刻走到第4步 ack,此时 ackBuf 还是空的 → 回执要等
// 下一轮 Timer C(10~20秒,异常时1~2分钟)才发出,后台迟迟看不到结果。
// 这里用 CountDownLatch 等主线程执行完(超时3秒兜底),
// 让 pull → 执行 → ack 在同一轮内完成。
final java.util.concurrent.CountDownLatch latch =
new java.util.concurrent.CountDownLatch(cmds.size());
for (final Map<String, Object> c : cmds) { for (final Map<String, Object> c : cmds) {
// 改背包必须回主线程
if (Bukkit.isPrimaryThread()) { if (Bukkit.isPrimaryThread()) {
try {
execCommand(c); execCommand(c);
} finally {
latch.countDown();
}
} else { } else {
Bukkit.getScheduler().runTask(plugin, new Runnable() { Bukkit.getScheduler().runTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
try {
execCommand(c); execCommand(c);
} finally {
latch.countDown();
}
} }
}); });
} }
} }
if (!latch.await(3, java.util.concurrent.TimeUnit.SECONDS)) {
// 主线程被卡住:结果仍在 ackBuf 里,下一轮重发,不丢
throttleErr("tick-cmd:主线程执行超时3s,回执延后到下一轮");
}
}
} }
} catch (Throwable t) { } catch (Throwable t) {
throttleErr("tick-cmd:" + t.getMessage()); throttleErr("tick-cmd:" + t.getMessage());
@@ -1588,10 +1608,10 @@ public class SnManager implements Listener {
List<String> acks; List<String> acks;
synchronized (ackBuf) { synchronized (ackBuf) {
if (ackBuf.isEmpty()) acks = null; if (ackBuf.isEmpty()) acks = null;
else { // ★ 只做只读快照,不 clear:PHP 确认收到后才移除(见下方 ackResp 判断)。
acks = new ArrayList<String>(ackBuf); // 原实现先 clear 再 POST,POST 失败时这批回执被永久丢弃 →
ackBuf.clear(); // PHP 端命令一直停在 sent,180 秒后被判 timeout,状态永不回写。
} else acks = new ArrayList<String>(ackBuf);
} }
if (acks != null) { if (acks != null) {
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
+12 -4
View File
@@ -2024,13 +2024,21 @@ public class WebManager {
} }
// 全量批处理同步(仅在nextSyncTime到达时执行) // 全量批处理同步(仅在nextSyncTime到达时执行)
if (now >= nextSyncTime) { // ★ doActiveSyncBatch 内部有 9 处 6~14 秒的错峰 sleep(合计 54~126 秒),
try { doActiveSyncBatch(); } // 原来同步跑在本定时器里,直接把 Timer C 单轮拉长到约 2 分钟,
catch (Exception e) { // SN 命令的拉取/回执也跟着变成 1~2 分钟一轮。
// 改为丢到独立异步线程执行:Timer C 只负责到点触发后立刻排下一轮,
// 批处理照旧串行错峰(防 PHP 锁库),只是不再占用 SN 同步链路。
if (now >= nextSyncTime && !activeSyncRunning) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
doActiveSyncBatch();
} catch (Exception e) {
plugin.getLogger().warning("[合并C-全量同步] 异常: " + e.getMessage()); plugin.getLogger().warning("[合并C-全量同步] 异常: " + e.getMessage());
} finally { } finally {
scheduleNextSync(); // 设置60~90秒后的下一次同步时间 scheduleNextSync(); // 批处理结束后设置60~90秒后的下一次同步时间
} }
});
} }
// 自调度下一轮(10~20秒,错峰) // 自调度下一轮(10~20秒,错峰)