sourceServerMigrationCounts.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {
  2. sourceServerManagementSnapshotSchema,
  3. type SourceServerManagementSnapshot,
  4. } from "@zcode/shared";
  5. import type { ManagedSourceServerConnection } from "./sourceServerConnectionManager.js";
  6. import type { SourceServerDirectorySnapshot } from "./sourceServerDirectory.js";
  7. /** Host 管理服务持有的统计投影;远端读取不阻塞本机目录,连接换代后旧结果失效。 */
  8. export function createSourceServerMigrationCounts(options: {
  9. getOnlineConnection(sourceServerId: string): ManagedSourceServerConnection | undefined;
  10. onDidChange(): void;
  11. }) {
  12. let disposed = false;
  13. type MigrationCount = SourceServerManagementSnapshot["migrationCounts"][number];
  14. interface MigrationRead {
  15. connection: ManagedSourceServerConnection;
  16. value: MigrationCount;
  17. refreshRequested: boolean;
  18. }
  19. const migrationReads = new Map<string, MigrationRead>();
  20. function invalidateMigrationCounts(sourceServerId?: string) {
  21. for (const [id, entry] of migrationReads) {
  22. if (sourceServerId !== undefined && id !== sourceServerId) continue;
  23. // 在途只标记一次刷新,避免目录事件在网络停滞时积累远端请求。
  24. if (entry.value.status === "loading") entry.refreshRequested = true;
  25. else migrationReads.delete(id);
  26. }
  27. }
  28. function startMigrationRead(sourceServerId: string, entry: MigrationRead) {
  29. void (async () => {
  30. let value: MigrationCount;
  31. try {
  32. const service = entry.connection.services.sourceServerWorkspaceService;
  33. const [failed, abandoned] = await Promise.allSettled([
  34. Promise.resolve().then(() => service.getFailedPathCount()),
  35. Promise.resolve().then(() => service.getAbandonedPathCount()),
  36. ]);
  37. // 一项失败也要等该组另一项收口,避免反复失效时堆积仍在途的请求。
  38. if (failed.status !== "fulfilled" || abandoned.status !== "fulfilled") {
  39. throw new Error("Migration counts unavailable");
  40. }
  41. value = sourceServerManagementSnapshotSchema.shape.migrationCounts.element.parse({
  42. sourceServerId,
  43. status: "available",
  44. failedPathCount: failed.value,
  45. abandonedPathCount: abandoned.value,
  46. });
  47. } catch {
  48. value = { sourceServerId, status: "unavailable" };
  49. }
  50. // 统计属于当前物理连接;断开、换代、目录移除或释放后的迟到结果不能发布。
  51. if (
  52. disposed ||
  53. migrationReads.get(sourceServerId) !== entry ||
  54. options.getOnlineConnection(sourceServerId) !== entry.connection
  55. )
  56. return;
  57. if (entry.refreshRequested) {
  58. entry.refreshRequested = false;
  59. startMigrationRead(sourceServerId, entry);
  60. return;
  61. }
  62. entry.value = value;
  63. options.onDidChange();
  64. })();
  65. }
  66. function readMigrationCounts(directory: SourceServerDirectorySnapshot): {
  67. failedPathCount: number;
  68. abandonedPathCount: number;
  69. migrationCounts: SourceServerManagementSnapshot["migrationCounts"];
  70. } {
  71. const activeSources = new Set(directory.servers.map((server) => server.sourceServerId));
  72. for (const id of migrationReads.keys()) {
  73. if (!activeSources.has(id)) migrationReads.delete(id);
  74. }
  75. // 本机设备目录与手动断开不能依赖远端迁移统计完成;统计仅异步补全快照。
  76. const migrationCounts = directory.servers.map((server): MigrationCount => {
  77. const connection = options.getOnlineConnection(server.sourceServerId);
  78. if (!connection) {
  79. migrationReads.delete(server.sourceServerId);
  80. return {
  81. sourceServerId: server.sourceServerId,
  82. status: "requires-connection" as const,
  83. };
  84. }
  85. let entry = migrationReads.get(server.sourceServerId);
  86. if (!entry || entry.connection !== connection) {
  87. entry = {
  88. connection,
  89. value: { sourceServerId: server.sourceServerId, status: "loading" },
  90. refreshRequested: false,
  91. };
  92. migrationReads.set(server.sourceServerId, entry);
  93. startMigrationRead(server.sourceServerId, entry);
  94. }
  95. return entry.value;
  96. });
  97. const totals = migrationCounts.reduce(
  98. (total, count) => ({
  99. failedPathCount:
  100. total.failedPathCount + (count.status === "available" ? count.failedPathCount : 0),
  101. abandonedPathCount:
  102. total.abandonedPathCount + (count.status === "available" ? count.abandonedPathCount : 0),
  103. }),
  104. { failedPathCount: 0, abandonedPathCount: 0 },
  105. );
  106. return { ...totals, migrationCounts };
  107. }
  108. return {
  109. read: readMigrationCounts,
  110. invalidate: invalidateMigrationCounts,
  111. dispose() {
  112. disposed = true;
  113. migrationReads.clear();
  114. },
  115. };
  116. }