Skip to content

Commit 66a4b48

Browse files
perf: use a redis cluster (#596)
1 parent 96c4dcd commit 66a4b48

35 files changed

+6273
-198
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ coverage/
55
dist/
66
node_modules/
77
probes-stats/
8+
test/

.github/workflows/ci.yml

+4-10
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ jobs:
1515
NODE_ENV: test
1616

1717
services:
18-
redis:
19-
image: redis/redis-stack-server:latest
20-
ports:
21-
- 16379:6379
22-
options: >-
23-
--health-cmd "redis-cli ping"
24-
--health-interval 10s
25-
--health-timeout 5s
26-
--health-retries 5
27-
2818
mariadb:
2919
image: mariadb:10.11.5
3020
ports:
@@ -45,6 +35,10 @@ jobs:
4535
- uses: actions/setup-node@v4
4636
with:
4737
node-version: 20.x
38+
- name: Set up Redis
39+
run: |
40+
cp config/redis/.env.redis ./
41+
docker compose up -d
4842
- name: Build
4943
run: |
5044
npm ci

.github/workflows/e2e.yml

+4-10
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ jobs:
1515
NODE_ENV: test
1616

1717
services:
18-
redis:
19-
image: redis/redis-stack-server:latest
20-
ports:
21-
- 16379:6379
22-
options: >-
23-
--health-cmd "redis-cli ping"
24-
--health-interval 10s
25-
--health-timeout 5s
26-
--health-retries 5
27-
2818
mariadb:
2919
image: mariadb:10.11.5
3020
ports:
@@ -51,6 +41,10 @@ jobs:
5141
sudo mv daemon.json /etc/docker/
5242
sudo systemctl restart docker
5343
docker restart $(docker ps -aq)
44+
- name: Set up Redis
45+
run: |
46+
cp config/redis/.env.redis ./
47+
docker compose up -d
5448
- name: Build
5549
run: |
5650
npm ci

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ coverage/
66
dist/
77
tmp/
88
config/local*
9+
data/redis
910
data/DOMAIN_BLACKLIST.json
1011
data/IP_BLACKLIST.json
1112
data/AWS_IP_RANGES.json
@@ -18,3 +19,4 @@ probes-stats/all-result.csv
1819
probes-stats/all-result.json
1920
.eslintcache
2021
.env
22+
/.env.redis

.husky/pre-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ if ! type cygpath > /dev/null 2>&1; then
66
PATH="$PATH:$(type -ap git | grep 'cmd/git' | sed 's$cmd/git$usr/bin$')"
77
fi
88

9-
node_modules/.bin/lint-staged --no-stash --quiet
9+
node_modules/.bin/lint-staged --quiet

CONTRIBUTING.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ Hi! We're really excited that you're interested in contributing! Before submitti
1010

1111
## Project setup
1212

13-
In order to run the Globalping API locally you will need Node.js 20 and Redis with [RedisJSON](https://oss.redis.com/redisjson/) module and MariaDB. All of them are included in docker-compose.yml file. You will also need to run a development instance of the [Globalping Probe](https://github.com/jsdelivr/globalping-probe) at the same time when testing.
13+
In order to run the Globalping API locally you will need Node.js 20 and Redis with [RedisJSON](https://oss.redis.com/redisjson/) module and MariaDB. All of them are included in docker-compose.dev.yml file. You will also need to run a development instance of the [Globalping Probe](https://github.com/jsdelivr/globalping-probe) at the same time when testing.
1414

1515
The API uses 3000 port by default. This can be overridden by `PORT` environment variable.
1616

1717
You can run the project by following these steps:
1818

1919
1. Clone this repository.
20-
2. `docker-compose up -d` - Run Redis and MariaDB
21-
3. `npm install && npm run download:files`
22-
4. Run `npm run start:dev`
20+
2. [Enable host networking in Docker Desktop](https://docs.docker.com/engine/network/drivers/host/#docker-desktop) if you haven't already.
21+
3. `docker compose -f docker-compose.dev.yml up -d` - Run Redis and MariaDB
22+
4. `npm install && npm run download:files`
23+
5. Run `npm run start:dev`
2324

2425
Once the API is live, you can spin up a probe instance by running as described at https://github.com/jsdelivr/globalping-probe/blob/master/CONTRIBUTING.md.
2526

@@ -48,3 +49,4 @@ Most IDEs have plugins integrating the used linter (eslint), including support f
4849
- `SYSTEM_API_KEY={value}` used for integration with the dashboard
4950
- `SERVER_SESSION_COOKIE_SECRET={value}` used to read the shared session cookie
5051
- `DB_CONNECTION_HOST`, `DB_CONNECTION_USER`, `DB_CONNECTION_PASSWORD`, and `DB_CONNECTION_DATABASE` database connection details
52+
- `REDIS_STANDALONE_PERSISTENT_URL`, `REDIS_STANDALONE_NON_PERSISTENT_URL`, `REDIS_CLUSTER_MEASUREMENTS_NODES_0`, `REDIS_CLUSTER_MEASUREMENTS_NODES_1`, `REDIS_CLUSTER_MEASUREMENTS_NODES_2`, and `REDIS_SHARED_OPTIONS_PASSWORD` - redis connection details

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM node:20-bullseye-slim AS builder
22
RUN apt-get update -y && apt-get install util-linux curl git -y
33

4-
ENV NODE_ENV production
4+
ENV NODE_ENV=production
55

66
COPY package.json package-lock.json /app/
77
WORKDIR /app
@@ -12,7 +12,7 @@ RUN npm run build
1212
FROM node:20-bullseye-slim
1313
RUN apt-get update -y && apt-get install tini util-linux curl -y
1414

15-
ENV NODE_ENV production
15+
ENV NODE_ENV=production
1616

1717
COPY package.json package-lock.json /app/
1818
WORKDIR /app
+1-24
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,2 @@
1-
const _ = require('lodash');
21
const df = require('./default.cjs');
3-
4-
function mapEnvConfig (object, prefix = '') {
5-
return _.mapValues(object, (value, key) => {
6-
const currentKey = (prefix ? `${prefix}_` : '') + _.snakeCase(key).toUpperCase();
7-
8-
if (_.isObject(value)) {
9-
return mapEnvConfig(value, currentKey);
10-
}
11-
12-
if (typeof value === 'number' || typeof value === 'boolean') {
13-
return {
14-
__name: currentKey,
15-
__format: typeof value,
16-
};
17-
}
18-
19-
return currentKey;
20-
});
21-
}
22-
23-
const mapped = mapEnvConfig(df);
24-
25-
module.exports = mapped;
2+
module.exports = require('config-mapper-env')(df);

config/default.cjs

+20-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,26 @@ module.exports = {
1717
},
1818
},
1919
redis: {
20-
url: 'redis://localhost:6379',
21-
socket: {
22-
tls: false,
20+
standalonePersistent: {
21+
url: 'redis://localhost:7001',
22+
},
23+
standaloneNonPersistent: {
24+
url: 'redis://localhost:7002',
25+
},
26+
clusterMeasurements: {
27+
// listing three nodes here is enough, the rest will be discovered automatically
28+
nodes: {
29+
0: 'redis://localhost:7101',
30+
1: 'redis://localhost:7102',
31+
2: 'redis://localhost:7103',
32+
},
33+
options: {},
34+
},
35+
sharedOptions: {
36+
password: 'PASSWORD',
37+
socket: {
38+
tls: false,
39+
},
2340
},
2441
},
2542
db: {

config/development.cjs

-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ module.exports = {
44
cookieSecret: 'xxx',
55
},
66
},
7-
redis: {
8-
url: 'redis://localhost:16379',
9-
socket: {
10-
tls: false,
11-
},
12-
},
137
db: {
148
connection: {
159
port: 13306,

config/redis/.env.redis

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# standalone:
2+
REDIS_ARGS="--requirepass PASSWORD"
3+
4+
# cluster:
5+
REDIS_PASSWORD=PASSWORD
6+
REDIS_PUBLIC_IP=127.0.0.1

redis/setup.md config/redis/README.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ swapon /swapfile
1212
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
1313
```
1414

15-
## Config
15+
### Docker config
1616

17-
1. Download redis.conf to /etc/redis/
18-
2. Download zip file with json module to same folder
19-
3. Unzip
20-
4. Restart
17+
Assuming you start in this directory:
18+
19+
```
20+
cp .env.redis ../../
21+
```
22+
23+
Set the redis password and return to the project root. Then:
24+
25+
```
26+
docker compose up -d
27+
```

0 commit comments

Comments
 (0)