fix(onebot): retry rkey fetch eagerly while cache holds none (#156)
When the OIDB 0x9067_202 rkey fetch returns empty or fails, the cache stays
empty and resolveImageUrl hands out a bare multimedia.nt.qq.com.cn URL the CDN
rejects with {retcode:-5503010, invalid rkey}. The 30s refresh cooldown then
blacked out every image for 30s+ after each empty/failed fetch.
Use a short RKEY_EMPTY_COOLDOWN (10s) whenever no usable image rkey is cached,
keeping the 30s steady-state backoff only once we actually hold one — so a
transient empty fetch self-heals in seconds instead of 30s. (Part A of #156;
remote-server fallback follows.)
This commit is contained in:
2 files changed
+63
-5
No files matched your search
@@ -15,6 +15,11 @@ interface CachedRKey {
|
||||
|
||||
const RKEY_REFRESH_SKEW = 60;
|
||||
const RKEY_REFRESH_COOLDOWN = 30;
|
||||
// When the cache holds NO usable image rkey, the alternative to refreshing is
|
||||
// handing out a bare URL the CDN rejects with `invalid rkey` (issue #156). So
|
||||
// retry far more eagerly than the 30s steady-state backoff — but still throttled
|
||||
// so a persistently-failing fetch isn't hammered once per inbound image.
|
||||
const RKEY_EMPTY_COOLDOWN = 10;
|
||||
// Upper bound on how long URL resolution may block on a refresh round-trip.
|
||||
// A stuck packet send must never wedge message conversion — past this we
|
||||
// fall back to whatever the cache holds (possibly nothing).
|
||||
@@ -101,6 +106,13 @@ export class RKeyCache {
|
||||
}
|
||||
}
|
||||
|
||||
/** True when at least one common image rkey (group or private) is cached and
|
||||
* still valid — i.e. we can sign a typical image without a fresh fetch. */
|
||||
private hasUsableRkey(): boolean {
|
||||
return this.findInCache(GROUP_IMAGE_RKEY_TYPE) !== null
|
||||
|| this.findInCache(PRIVATE_IMAGE_RKEY_TYPE) !== null;
|
||||
}
|
||||
|
||||
/** Pull a still-valid rkey out of the cache, or null if missing/expiring. */
|
||||
private findInCache(primaryType: number): string | null {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
@@ -128,15 +140,18 @@ export class RKeyCache {
|
||||
|
||||
/**
|
||||
* Refresh the rkey cache via OIDB 0x9067_202, coalescing concurrent
|
||||
* callers onto one in-flight request and backing off for
|
||||
* RKEY_REFRESH_COOLDOWN after a failed attempt so a persistently
|
||||
* unreachable server isn't hammered once per inbound image. The await is
|
||||
* bounded by RKEY_REFRESH_TIMEOUT_MS.
|
||||
* callers onto one in-flight request and backing off (RKEY_REFRESH_COOLDOWN
|
||||
* when a usable rkey is held, RKEY_EMPTY_COOLDOWN while none is) so a
|
||||
* persistently-failing fetch isn't hammered once per inbound image. The await
|
||||
* is bounded by RKEY_REFRESH_TIMEOUT_MS.
|
||||
*/
|
||||
private async ensureFresh(bridge: BridgeInterface): Promise<void> {
|
||||
if (!this.refreshInflight) {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
if (now - this.lastRefreshAttempt < RKEY_REFRESH_COOLDOWN) return;
|
||||
// Back off hard once we hold a usable rkey; retry quickly while we don't,
|
||||
// so a transient empty/failed fetch self-heals in seconds, not 30s (#156).
|
||||
const cooldown = this.hasUsableRkey() ? RKEY_REFRESH_COOLDOWN : RKEY_EMPTY_COOLDOWN;
|
||||
if (now - this.lastRefreshAttempt < cooldown) return;
|
||||
this.lastRefreshAttempt = now;
|
||||
this.refreshInflight = bridge.apis.contacts.fetchDownloadRKeys()
|
||||
.then((rkeys) => { this.updateCache(rkeys); })
|
||||
|
||||
@@ -124,6 +124,49 @@ describe('RKeyCache.resolveImageUrl', () => {
|
||||
expect(url).toBe(`${noAppidUrl}&rkey=GROUPKEY`);
|
||||
});
|
||||
|
||||
// #156: a transient empty/failed fetch must self-heal in seconds, not 30s.
|
||||
// An empty cache uses the short RKEY_EMPTY_COOLDOWN (10s), so a retry ~11s
|
||||
// later refetches — under the 30s steady-state backoff it would not.
|
||||
it('retries before the 30s cooldown while the cache holds no usable rkey', async () => {
|
||||
vi.useFakeTimers({ toFake: ['Date'] });
|
||||
try {
|
||||
vi.setSystemTime(new Date(1_700_000_000_000));
|
||||
let calls = 0;
|
||||
const { bridge, fetchDownloadRKeys } = makeBridge(
|
||||
async () => (++calls === 1 ? [] : freshGroupRkey()),
|
||||
);
|
||||
const cache = new RKeyCache();
|
||||
|
||||
const url1 = await cache.resolveImageUrl(bridge as never, imageEl(), true);
|
||||
expect(url1).toBe(NT_IMAGE_URL); // empty fetch → bare URL
|
||||
|
||||
vi.setSystemTime(new Date(1_700_000_000_000 + 11_000)); // +11s: past 10s empty-cooldown, inside 30s
|
||||
const url2 = await cache.resolveImageUrl(bridge as never, imageEl(), true);
|
||||
|
||||
expect(url2).toBe(`${NT_IMAGE_URL}&rkey=GROUPKEY`);
|
||||
expect(fetchDownloadRKeys).toHaveBeenCalledTimes(2);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('does NOT refetch within 30s once a usable rkey is cached', async () => {
|
||||
vi.useFakeTimers({ toFake: ['Date'] });
|
||||
try {
|
||||
vi.setSystemTime(new Date(1_700_000_000_000));
|
||||
const { bridge, fetchDownloadRKeys } = makeBridge(async () => freshGroupRkey());
|
||||
const cache = new RKeyCache();
|
||||
|
||||
await cache.resolveImageUrl(bridge as never, imageEl(), true);
|
||||
vi.setSystemTime(new Date(1_700_000_000_000 + 15_000)); // +15s, inside 30s backoff
|
||||
await cache.resolveImageUrl(bridge as never, imageEl(), true);
|
||||
|
||||
expect(fetchDownloadRKeys).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('leaves non-NT URLs untouched and never fetches', async () => {
|
||||
const { bridge, fetchDownloadRKeys } = makeBridge(async () => freshGroupRkey());
|
||||
const cache = new RKeyCache();
|
||||
|
||||
Reference in new issue
Block a user