Skip to content

Commit ffd4265

Browse files
committed
Merge branch 'master' into overview-title-fix
2 parents 94c9b52 + 3041102 commit ffd4265

33 files changed

+278
-261
lines changed

.nais/config.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@ spec:
1818
port: 3000
1919
liveness:
2020
path: /api/internal/isAlive
21-
initialDelay: 5
21+
initialDelay: 10
2222
timeout: 10
2323
readiness:
2424
path: /api/internal/isReady
25-
initialDelay: 20
25+
initialDelay: 10
2626
timeout: 10
2727
prometheus:
2828
enabled: true
2929
path: /internal/metrics
30-
leaderElection: true
3130
redis:
3231
- instance: {{ redis.instance }}
33-
access: admin
32+
access: readwrite
3433
ingresses:
3534
{{#each ingresses as |url|}}
3635
- {{url}}

.nais/redis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ metadata:
99
spec:
1010
plan: {{ redis.plan }}
1111
project: {{ redis.project }}
12+
maintenanceWindowDow: saturday
13+
userConfig:
14+
redis_maxmemory_policy: allkeys-lru
1215

1316
---
1417
apiVersion: aiven.io/v1alpha1

package-lock.json

+3-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"react-dom": "18.2.0",
6262
"react-movable": "3.0.4",
6363
"react-redux": "8.1.3",
64+
"redis": "4.6.13",
6465
"rss": "1.2.2",
6566
"sharp": "0.33.2",
6667
"swr": "2.2.4",

server/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
"express-prom-bundle": "7.0.0",
2323
"http-terminator": "3.2.0",
2424
"on-headers": "1.0.2",
25-
"prom-client": "15.1.0",
26-
"redis": "4.6.13"
25+
"prom-client": "15.1.0"
2726
},
2827
"devDependencies": {
2928
"@types/cookie-parser": "1.4.6",
+10-22
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import FileSystemCache from 'next/dist/server/lib/incremental-cache/file-system-cache';
22
import { LRUCache } from 'lru-cache';
33
import { CacheHandlerValue } from 'next/dist/server/lib/incremental-cache';
4-
import { RedisCache, RedisCacheDummy } from 'cache/redis';
5-
import { isLeaderPod } from 'leader-pod';
6-
7-
const CACHE_TTL_24_HOURS_IN_MS = 3600 * 24 * 1000;
4+
import { RedisCache } from 'srcCommon/redis';
5+
import { CACHE_TTL_72_HOURS_IN_MS } from 'srcCommon/constants';
6+
import { pathToCacheKey } from 'srcCommon/cache-key';
87

98
const TTL_RESOLUTION_MS = 60 * 1000;
109

11-
export const redisCache = !process.env.REDIS_URI_PAGECACHE
12-
? new RedisCacheDummy()
13-
: new RedisCache({ ttl: CACHE_TTL_24_HOURS_IN_MS });
10+
export const redisCache = new RedisCache();
1411

1512
const localCache = new LRUCache<string, CacheHandlerValue>({
1613
max: 2000,
17-
ttl: CACHE_TTL_24_HOURS_IN_MS,
14+
ttl: CACHE_TTL_72_HOURS_IN_MS,
1815
ttlResolution: TTL_RESOLUTION_MS,
1916
});
2017

@@ -27,16 +24,16 @@ export default class PageCacheHandler {
2724
return fromLocalCache;
2825
}
2926

30-
const fromRedisCache = await redisCache.get(key);
27+
const fromRedisCache = await redisCache.getRender(key);
3128
if (!fromRedisCache) {
3229
return null;
3330
}
3431

3532
const ttlRemaining = fromRedisCache.lastModified
3633
? fromRedisCache.lastModified +
37-
CACHE_TTL_24_HOURS_IN_MS -
34+
CACHE_TTL_72_HOURS_IN_MS -
3835
Date.now()
39-
: CACHE_TTL_24_HOURS_IN_MS;
36+
: CACHE_TTL_72_HOURS_IN_MS;
4037

4138
if (ttlRemaining > TTL_RESOLUTION_MS) {
4239
localCache.set(key, fromRedisCache, {
@@ -56,23 +53,14 @@ export default class PageCacheHandler {
5653
};
5754

5855
localCache.set(key, cacheItem);
59-
redisCache.set(key, cacheItem);
56+
redisCache.setRender(key, cacheItem);
6057
}
6158

6259
public async clear() {
6360
localCache.clear();
64-
65-
if (await isLeaderPod()) {
66-
return redisCache.clear();
67-
}
6861
}
6962

7063
public async delete(path: string) {
71-
const pagePath = path === '/' ? '/index' : path;
72-
localCache.delete(pagePath);
73-
74-
if (await isLeaderPod()) {
75-
return redisCache.delete(pagePath);
76-
}
64+
localCache.delete(pathToCacheKey(path));
7765
}
7866
}

server/src/cache/redis.ts

-129
This file was deleted.

server/src/cache/revalidator-proxy-heartbeat.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('Revalidator proxy heartbeat', () => {
1616

1717
fetchMock.mockResponse('Hello!');
1818

19-
initRevalidatorProxyHeartbeat();
19+
initRevalidatorProxyHeartbeat('dummy-build-id');
2020

2121
expect(fetchMock.mock.calls.length).toEqual(1);
2222
expect(fetchMock.mock.calls[0][0]).toMatch(

server/src/cache/revalidator-proxy-heartbeat.ts

+40-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
// See: https://github.com/navikt/nav-enonicxp-frontend-revalidator-proxy
44
import { networkInterfaces } from 'os';
55
import { logger } from 'srcCommon/logger';
6+
import {
7+
getRenderCacheKeyPrefix,
8+
getResponseCacheKeyPrefix,
9+
} from 'srcCommon/redis';
10+
import { objectToQueryString } from 'srcCommon/fetch-utils';
611

712
const {
813
ENV,
@@ -11,7 +16,8 @@ const {
1116
REVALIDATOR_PROXY_ORIGIN,
1217
SERVICE_SECRET,
1318
} = process.env;
14-
const heartbeatPeriodMs = 5000;
19+
20+
const HEARTBEAT_PERIOD_MS = 5000;
1521

1622
const getPodAddress = () => {
1723
if (ENV === 'localhost') {
@@ -34,33 +40,46 @@ const getPodAddress = () => {
3440
return podAddress;
3541
};
3642

37-
const getProxyLivenessUrl = () => {
43+
const getProxyLivenessUrl = (buildId: string) => {
3844
const podAddress = getPodAddress();
3945
return podAddress
40-
? `${REVALIDATOR_PROXY_ORIGIN}/liveness?address=${podAddress}`
46+
? `${REVALIDATOR_PROXY_ORIGIN}/liveness${objectToQueryString({
47+
address: podAddress,
48+
redisPrefixes: [
49+
getRenderCacheKeyPrefix(buildId),
50+
getResponseCacheKeyPrefix(),
51+
].join(','),
52+
})}`
4153
: null;
4254
};
4355

44-
export const initRevalidatorProxyHeartbeat = (() => {
56+
let didStart = false;
57+
58+
export const initRevalidatorProxyHeartbeat = (buildId: string) => {
4559
if (NODE_ENV === 'development') {
46-
return () => {};
60+
return;
4761
}
4862

49-
let heartbeatInterval: NodeJS.Timer;
50-
const url = getProxyLivenessUrl();
63+
if (didStart) {
64+
logger.warn('Proxy heartbeat loop already started!');
65+
return;
66+
}
67+
68+
const url = getProxyLivenessUrl(buildId);
69+
if (!url) {
70+
return;
71+
}
5172

52-
return () => {
53-
if (!heartbeatInterval && url) {
54-
logger.info('Starting heartbeat loop');
55-
const heartbeatFunc = () => {
56-
fetch(url, {
57-
headers: { secret: SERVICE_SECRET },
58-
}).catch((e) =>
59-
logger.error(`Failed to send heartbeat signal - ${e}`)
60-
);
61-
};
62-
heartbeatFunc();
63-
heartbeatInterval = setInterval(heartbeatFunc, heartbeatPeriodMs);
64-
}
73+
didStart = true;
74+
75+
logger.info('Starting heartbeat loop');
76+
77+
const heartbeatFunc = () => {
78+
fetch(url, {
79+
headers: { secret: SERVICE_SECRET },
80+
}).catch((e) => logger.error(`Failed to send heartbeat signal - ${e}`));
6581
};
66-
})();
82+
83+
heartbeatFunc();
84+
setInterval(heartbeatFunc, HEARTBEAT_PERIOD_MS);
85+
};

0 commit comments

Comments
 (0)