Repository navigation
Expand file tree
/
Copy pathdiff-cache.ts
More file actions
83 lines (72 loc) · 3.21 KB
/
Copy pathdiff-cache.ts
File metadata and controls
83 lines (72 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import * as q from "q";
import * as redis from "../redis-manager";
import { CachedRelease } from "../types/rest-definitions";
import { Package, PackageHashToBlobInfoMap } from "../storage/storage";
import { Microcache } from "./microcache";
import { DiffMapFetcher } from "./acquisition";
const DIFFPACKAGE_MEM_TTL_MS: number = Number(process.env.DIFFPACKAGE_MEM_TTL_MS) || 5 * 60 * 1000;
// Losing the diff map costs the client a full-bundle download instead of a patch, which
// is a bandwidth bill. Waiting on a stalled Redis costs it the whole update check, which
// is a blocked app launch. This sits on the response path, so it gets a deadline and the
// existing fallback absorbs the miss.
const DIFFMAP_TIMEOUT_MS: number = Number(process.env.DIFFMAP_TIMEOUT_MS) || 1000;
// Memoizes per-release diff map lookups so burst traffic only hits Redis once per package hash.
const diffMapCache = new Microcache<q.Promise<PackageHashToBlobInfoMap>>(DIFFPACKAGE_MEM_TTL_MS);
export function createDiffMapFetcher(deploymentKey: string, redisManager: redis.RedisManager): DiffMapFetcher {
if (!deploymentKey || !redisManager) {
return () => q<PackageHashToBlobInfoMap>(null);
}
return (packageHash: string): q.Promise<PackageHashToBlobInfoMap> => {
if (!packageHash) {
return q<PackageHashToBlobInfoMap>(null);
}
const cacheKey = `${deploymentKey}:${packageHash}`;
const cachedPromise = diffMapCache.get(cacheKey);
if (cachedPromise) {
return cachedPromise;
}
return redisManager
.getPackageDiffMap(deploymentKey, packageHash)
.timeout(DIFFMAP_TIMEOUT_MS, "diff map lookup timed out")
.then((diffMap) => {
const normalizedDiffMap = diffMap || null;
const resolvedPromise = q<PackageHashToBlobInfoMap>(normalizedDiffMap);
diffMapCache.set(cacheKey, resolvedPromise);
return normalizedDiffMap;
})
.catch((error: any): PackageHashToBlobInfoMap => {
// If Redis is unreachable, degrade gracefully: return no diff map so the
// client receives the full package instead of failing the update check.
// Don't cache the failure so it retries once Redis recovers.
console.warn("Failed to fetch diff package map from Redis", error);
return null;
});
};
}
export function primeDiffCacheForReleases(
deploymentKey: string,
releases: CachedRelease[] | undefined,
packageLookup: Map<string, Package>,
redisManager: redis.RedisManager
): q.Promise<void> {
if (!redisManager || !deploymentKey || !releases || !releases.length) {
return q<void>(null);
}
const diffCacheWrites: q.Promise<void>[] = [];
releases.forEach((release) => {
if (!release) {
return;
}
const pkg = packageLookup.get(release.packageHash);
const diffMap = pkg && pkg.diffPackageMap;
const hasDiff = !!(diffMap && Object.keys(diffMap).length);
if (hasDiff && diffMap) {
diffCacheWrites.push(
redisManager.setPackageDiffMap(deploymentKey, release.packageHash, diffMap).catch((error: any) => {
console.warn("Failed to cache diff package map for updateCheck", error);
})
);
}
});
return diffCacheWrites.length ? q.all(diffCacheWrites).then(() => {}) : q<void>(null);
}