Skip to content

Commit 8fdfc65

Browse files
author
Simon Emms
committed
check with webapps this is on right lines
1 parent fa9e838 commit 8fdfc65

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

components/server/ee/src/container-module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { GitLabAppSupport } from "./gitlab/gitlab-app-support";
4949
import { Config } from "../../src/config";
5050
import { SnapshotService } from "./workspace/snapshot-service";
5151
import { BitbucketAppSupport } from "./bitbucket/bitbucket-app-support";
52+
import { UserCounter } from "./user/user-counter";
5253

5354
export const productionEEContainerModule = new ContainerModule((bind, unbind, isBound, rebind) => {
5455
rebind(Server).to(ServerEE).inSingletonScope();
@@ -69,6 +70,8 @@ export const productionEEContainerModule = new ContainerModule((bind, unbind, is
6970
bind(BitbucketApp).toSelf().inSingletonScope();
7071
bind(BitbucketAppSupport).toSelf().inSingletonScope();
7172

73+
bind(UserCounter).toSelf().inSingletonScope();
74+
7275
bind(LicenseEvaluator).toSelf().inSingletonScope();
7376
bind(LicenseKeySource).to(DBLicenseKeySource).inSingletonScope();
7477

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) 2022 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 { injectable } from 'inversify';
8+
9+
@injectable()
10+
export class UserCounter {
11+
public expires: Date | null = null;
12+
13+
protected data: number | null = null;
14+
15+
protected readonly timeout: number = 60 * 1000; // Cache data for 1 minute
16+
17+
get count(): number | null {
18+
if (this.expires !== null && Date.now() >= this.expires.getTime()) {
19+
// The timestamp is in range - return the data
20+
console.log('getting cached data', this.data);
21+
return this.data;
22+
}
23+
// Not in range - return null
24+
return null;
25+
}
26+
27+
set count(userCount: number | null) {
28+
this.expires = new Date(Date.now() + this.timeout);
29+
30+
this.data = userCount;
31+
}
32+
}

components/server/ee/src/workspace/gitpod-server-impl.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { SnapshotService, WaitForSnapshotOptions } from "./snapshot-service";
4242
import { ClientMetadata, traceClientMetadata } from "../../../src/websocket/websocket-connection-manager";
4343
import { BitbucketAppSupport } from "../bitbucket/bitbucket-app-support";
4444
import { URL } from 'url';
45+
import { UserCounter } from "../user/user-counter";
4546

4647
@injectable()
4748
export class GitpodServerEEImpl extends GitpodServerImpl {
@@ -75,6 +76,8 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
7576

7677
@inject(SnapshotService) protected readonly snapshotService: SnapshotService;
7778

79+
@inject(UserCounter) protected readonly userCounter: UserCounter;
80+
7881
initialize(client: GitpodClient | undefined, user: User | undefined, accessGuard: ResourceAccessGuard, clientMetadata: ClientMetadata, connectionCtx: TraceContext | undefined, clientHeaderFields: ClientHeaderFields): void {
7982
super.initialize(client, user, accessGuard, clientMetadata, connectionCtx, clientHeaderFields);
8083

@@ -153,7 +156,18 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
153156
}
154157

155158
protected async requireEELicense(feature: Feature) {
156-
const userCount = await this.userDB.getUserCount(true);
159+
const cachedUserCount = this.userCounter.count;
160+
161+
let userCount: number;
162+
if (cachedUserCount == null) {
163+
userCount = await this.userDB.getUserCount(true);
164+
this.userCounter.count = userCount;
165+
console.log('workspace user count')
166+
} else {
167+
console.log('workspace cache user count')
168+
userCount = cachedUserCount;
169+
}
170+
console.log({ userCount })
157171

158172
if (!this.licenseEvaluator.isEnabled(feature, userCount)) {
159173
throw new ResponseError(ErrorCodes.EE_LICENSE_REQUIRED, "enterprise license required");

components/server/ee/src/workspace/workspace-factory.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,30 @@ import { ErrorCodes } from '@gitpod/gitpod-protocol/lib/messaging/error';
1717
import { HostContextProvider } from '../../../src/auth/host-context-provider';
1818
import { RepoURL } from '../../../src/repohost';
1919
import { UserDB } from '@gitpod/gitpod-db/lib';
20+
import { UserCounter } from '../user/user-counter';
2021

2122
@injectable()
2223
export class WorkspaceFactoryEE extends WorkspaceFactory {
2324

2425
@inject(LicenseEvaluator) protected readonly licenseEvaluator: LicenseEvaluator;
2526
@inject(HostContextProvider) protected readonly hostContextProvider: HostContextProvider;
27+
@inject(UserCounter) protected readonly userCounter: UserCounter;
2628

2729
@inject(UserDB) protected readonly userDB: UserDB;
2830

2931
protected async requireEELicense(feature: Feature) {
30-
const userCount = await this.userDB.getUserCount(true);
32+
const cachedUserCount = this.userCounter.count;
33+
34+
let userCount: number;
35+
if (cachedUserCount == null) {
36+
userCount = await this.userDB.getUserCount(true);
37+
this.userCounter.count = userCount;
38+
console.log('user count')
39+
} else {
40+
console.log('cache user count')
41+
userCount = cachedUserCount;
42+
}
43+
console.log({ userCount })
3144

3245
if (!this.licenseEvaluator.isEnabled(feature, userCount)) {
3346
throw new ResponseError(ErrorCodes.EE_LICENSE_REQUIRED, "enterprise license required");

0 commit comments

Comments
 (0)