Skip to content

Commit 4459bff

Browse files
authored
[server] Make redis client reusable, stop creating new one on each mutex WEB-230 (#17384)
* [server] Make redis client reusable * fix * fix
1 parent a314926 commit 4459bff

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

components/server/src/container-module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ import { BitbucketAppSupport } from "./bitbucket/bitbucket-app-support";
125125
import { GitHubEnterpriseApp } from "./prebuilds/github-enterprise-app";
126126
import { BitbucketServerApp } from "./prebuilds/bitbucket-server-app";
127127
import { IncrementalPrebuildsService } from "./prebuilds/incremental-prebuilds-service";
128-
import { RedisMutex } from "./mutex/redlock";
128+
import { RedisClient } from "./redis/client";
129+
import { RedisMutex } from "./redis/mutex";
129130

130131
export const productionContainerModule = new ContainerModule((bind, unbind, isBound, rebind) => {
131132
bind(Config).toConstantValue(ConfigFile.fromFile());
@@ -345,5 +346,6 @@ export const productionContainerModule = new ContainerModule((bind, unbind, isBo
345346
bind(BitbucketServerApp).toSelf().inSingletonScope();
346347
bind(IncrementalPrebuildsService).toSelf().inSingletonScope();
347348

349+
bind(RedisClient).toSelf().inSingletonScope();
348350
bind(RedisMutex).toSelf().inSingletonScope();
349351
});

components/server/src/redis/client.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { inject, injectable, postConstruct } from "inversify";
8+
import { Redis } from "ioredis";
9+
import { Config } from "../config";
10+
11+
@injectable()
12+
export class RedisClient {
13+
@inject(Config) protected config: Config;
14+
15+
private client: Redis;
16+
17+
@postConstruct()
18+
protected initialize(): void {
19+
const [host, port] = this.config.redis.address.split(":");
20+
this.client = new Redis({
21+
port: Number(port),
22+
host,
23+
enableReadyCheck: true,
24+
keepAlive: 10 * 1000,
25+
connectionName: "server",
26+
});
27+
}
28+
29+
public get(): Redis {
30+
return this.client;
31+
}
32+
}

components/server/src/mutex/redlock.ts renamed to components/server/src/redis/mutex.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,15 @@
55
*/
66

77
import { inject, injectable } from "inversify";
8-
import { Config } from "../config";
9-
import { Redis as RedisClient } from "ioredis";
108
import Redlock from "redlock";
9+
import { RedisClient } from "./client";
1110

1211
@injectable()
1312
export class RedisMutex {
14-
@inject(Config) protected config: Config;
13+
@inject(RedisClient) protected redis: RedisClient;
1514

1615
public client(): Redlock {
17-
const [host, port] = this.config.redis.address.split(":");
18-
const redis = new RedisClient({
19-
port: Number(port),
20-
host,
21-
enableReadyCheck: true,
22-
});
23-
return new Redlock([redis], {
16+
return new Redlock([this.redis.get()], {
2417
// The expected clock drift; for more details see:
2518
// http://redis.io/topics/distlock
2619
driftFactor: 0.01, // multiplied by lock ttl to determine drift time

components/server/src/workspace/garbage-collector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { TracedWorkspaceDB, DBWithTracing, WorkspaceDB } from "@gitpod/gitpod-db
1313
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
1414
import { Config } from "../config";
1515
import { repeat } from "@gitpod/gitpod-protocol/lib/util/repeat";
16-
import { RedisMutex } from "../mutex/redlock";
1716
import { ResourceLockedError } from "redlock";
17+
import { RedisMutex } from "../redis/mutex";
1818

1919
/**
2020
* The WorkspaceGarbageCollector has two tasks:

0 commit comments

Comments
 (0)