Skip to content

Commit 5b81d89

Browse files
committed
根据AI意见修正
1 parent 39b2e4a commit 5b81d89

3 files changed

Lines changed: 30 additions & 26 deletions

File tree

src/app/service/service_worker/resource.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ export class ResourceService {
297297
async createResourceByUrlFetch(u: TUrlSRIInfo, type: ResourceType): Promise<Resource> {
298298
const url = u.url; // 无 URI Integrity Hash
299299

300+
// 随机抖动:分散对同一 server 的请求启动时间,降低被限速的概率
301+
await sleep(randNum(FETCH_JITTER_MIN_MS, FETCH_JITTER_MAX_MS));
302+
300303
// 等待并发槽位(滑动窗口入口)
301304
await concurrentFetchSlots.acquire();
302305

@@ -309,18 +312,19 @@ export class ResourceService {
309312
}
310313
};
311314

312-
// 随机抖动:分散对同一 server 的请求启动时间,降低被限速的概率
313-
await sleep(randNum(FETCH_JITTER_MIN_MS, FETCH_JITTER_MAX_MS));
314-
315315
// 滑动窗口语义:
316316
// - fetch 超时 (timeouted=true) → 提前归还槽位,下一个请求可以启动
317-
// - fetch 完成/失败 (done=true) → 归还槽位(若 timeout 已归还则为 no-op)
317+
// - fetch 完成/失败 (settled=true) → 归还槽位(若 timeout 已归还则为 no-op)
318318
// 原 fetch 在超时后仍继续运行,响应到达时照常处理(不会被取消)
319-
const { result, err } = await withTimeoutNotify(fetch(url), FETCH_SLOT_SLIDE_TIMEOUT_MS, ({ done, timeouted }) => {
320-
if (timeouted || done) {
321-
releaseSlotOnce();
319+
const { result, err } = await withTimeoutNotify(
320+
fetch(url),
321+
FETCH_SLOT_SLIDE_TIMEOUT_MS,
322+
({ settled, timeouted }) => {
323+
if (timeouted || settled) {
324+
releaseSlotOnce();
325+
}
322326
}
323-
});
327+
);
324328

325329
if (err) {
326330
throw new Error(`resource fetch failed: ${err.message || err}`);

src/pkg/utils/concurrency-control.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,35 +96,35 @@ describe("Semaphore", () => {
9696
});
9797

9898
describe("withTimeoutNotify", () => {
99-
it.concurrent("promise 在超时前完成时,回调收到 done=true", async () => {
99+
it.concurrent("promise 在超时前完成时,回调收到 settled=true", async () => {
100100
const promise = Promise.resolve("ok");
101-
const calls: Array<{ done: boolean; timeouted: boolean }> = [];
101+
const calls: Array<{ settled: boolean; timeouted: boolean }> = [];
102102

103103
const res = await withTimeoutNotify(promise, 1000, (r) => {
104-
calls.push({ done: r.done, timeouted: r.timeouted });
104+
calls.push({ settled: r.settled, timeouted: r.timeouted });
105105
});
106106

107107
expect(res.result).toBe("ok");
108-
expect(res.done).toBe(true);
108+
expect(res.settled).toBe(true);
109109
expect(res.timeouted).toBe(false);
110110
expect(res.err).toBeUndefined();
111111
// 只调用一次(done),不触发 timeout
112-
expect(calls).toEqual([{ done: true, timeouted: false }]);
112+
expect(calls).toEqual([{ settled: true, timeouted: false }]);
113113
});
114114

115115
it.concurrent("promise 在超时前失败时,回调收到 err", async () => {
116116
const error = new Error("fail");
117117
const promise = Promise.reject(error);
118-
const calls: Array<{ done: boolean; err: Error | undefined }> = [];
118+
const calls: Array<{ settled: boolean; err: Error | undefined }> = [];
119119

120120
const res = await withTimeoutNotify(promise, 1000, (r) => {
121-
calls.push({ done: r.done, err: r.err });
121+
calls.push({ settled: r.settled, err: r.err });
122122
});
123123

124124
expect(res.err).toBe(error);
125-
expect(res.done).toBe(true);
125+
expect(res.settled).toBe(true);
126126
expect(res.result).toBeUndefined();
127-
expect(calls).toEqual([{ done: true, err: error }]);
127+
expect(calls).toEqual([{ settled: true, err: error }]);
128128
});
129129

130130
it.concurrent("超时后回调被调用,promise 完成后再次调用", async () => {
@@ -133,26 +133,26 @@ describe("withTimeoutNotify", () => {
133133
const promise = new Promise<string>((r) => {
134134
resolvePromise = r;
135135
});
136-
const calls: Array<{ done: boolean; timeouted: boolean }> = [];
136+
const calls: Array<{ settled: boolean; timeouted: boolean }> = [];
137137

138138
const resultPromise = withTimeoutNotify(promise, 100, (r) => {
139-
calls.push({ done: r.done, timeouted: r.timeouted });
139+
calls.push({ settled: r.settled, timeouted: r.timeouted });
140140
});
141141

142142
// 触发超时
143143
vi.advanceTimersByTime(100);
144-
expect(calls).toEqual([{ done: false, timeouted: true }]);
144+
expect(calls).toEqual([{ settled: false, timeouted: true }]);
145145

146146
// promise 完成
147147
resolvePromise!("late");
148148
const res = await resultPromise;
149149

150150
expect(res.result).toBe("late");
151-
expect(res.done).toBe(true);
151+
expect(res.settled).toBe(true);
152152
expect(res.timeouted).toBe(true);
153-
// 回调被调用两次:timeout + done
153+
// 回调被调用两次:timeout + settled
154154
expect(calls).toHaveLength(2);
155-
expect(calls[1]).toEqual({ done: true, timeouted: true });
155+
expect(calls[1]).toEqual({ settled: true, timeouted: true });
156156

157157
vi.useRealTimers();
158158
});

src/pkg/utils/concurrency-control.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class Semaphore {
2626
type TWithTimeoutNotifyResult<T> = {
2727
timeouted: boolean;
2828
result: T | undefined;
29-
done: boolean;
29+
settled: boolean;
3030
err: undefined | Error;
3131
};
3232
export const withTimeoutNotify = <T>(
@@ -43,14 +43,14 @@ export const withTimeoutNotify = <T>(
4343
.then((result: T) => {
4444
clearTimeout(cid);
4545
res.result = result;
46-
res.done = true;
46+
res.settled = true;
4747
fn(res);
4848
return res;
4949
})
5050
.catch((e) => {
5151
clearTimeout(cid);
5252
res.err = e;
53-
res.done = true;
53+
res.settled = true;
5454
fn(res);
5555
return res;
5656
});

0 commit comments

Comments
 (0)