Skip to content

Commit

Permalink
check with webapps this is on right lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Emms committed Mar 8, 2022
1 parent fa9e838 commit 8fdfc65
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions components/server/ee/src/container-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { GitLabAppSupport } from "./gitlab/gitlab-app-support";
import { Config } from "../../src/config";
import { SnapshotService } from "./workspace/snapshot-service";
import { BitbucketAppSupport } from "./bitbucket/bitbucket-app-support";
import { UserCounter } from "./user/user-counter";

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

bind(UserCounter).toSelf().inSingletonScope();

bind(LicenseEvaluator).toSelf().inSingletonScope();
bind(LicenseKeySource).to(DBLicenseKeySource).inSingletonScope();

Expand Down
32 changes: 32 additions & 0 deletions components/server/ee/src/user/user-counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { injectable } from 'inversify';

@injectable()
export class UserCounter {
public expires: Date | null = null;

protected data: number | null = null;

protected readonly timeout: number = 60 * 1000; // Cache data for 1 minute

get count(): number | null {
if (this.expires !== null && Date.now() >= this.expires.getTime()) {
// The timestamp is in range - return the data
console.log('getting cached data', this.data);
return this.data;
}
// Not in range - return null
return null;
}

set count(userCount: number | null) {
this.expires = new Date(Date.now() + this.timeout);

this.data = userCount;
}
}
16 changes: 15 additions & 1 deletion components/server/ee/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { SnapshotService, WaitForSnapshotOptions } from "./snapshot-service";
import { ClientMetadata, traceClientMetadata } from "../../../src/websocket/websocket-connection-manager";
import { BitbucketAppSupport } from "../bitbucket/bitbucket-app-support";
import { URL } from 'url';
import { UserCounter } from "../user/user-counter";

@injectable()
export class GitpodServerEEImpl extends GitpodServerImpl {
Expand Down Expand Up @@ -75,6 +76,8 @@ export class GitpodServerEEImpl extends GitpodServerImpl {

@inject(SnapshotService) protected readonly snapshotService: SnapshotService;

@inject(UserCounter) protected readonly userCounter: UserCounter;

initialize(client: GitpodClient | undefined, user: User | undefined, accessGuard: ResourceAccessGuard, clientMetadata: ClientMetadata, connectionCtx: TraceContext | undefined, clientHeaderFields: ClientHeaderFields): void {
super.initialize(client, user, accessGuard, clientMetadata, connectionCtx, clientHeaderFields);

Expand Down Expand Up @@ -153,7 +156,18 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
}

protected async requireEELicense(feature: Feature) {
const userCount = await this.userDB.getUserCount(true);
const cachedUserCount = this.userCounter.count;

let userCount: number;
if (cachedUserCount == null) {
userCount = await this.userDB.getUserCount(true);
this.userCounter.count = userCount;
console.log('workspace user count')
} else {
console.log('workspace cache user count')
userCount = cachedUserCount;
}
console.log({ userCount })

if (!this.licenseEvaluator.isEnabled(feature, userCount)) {
throw new ResponseError(ErrorCodes.EE_LICENSE_REQUIRED, "enterprise license required");
Expand Down
15 changes: 14 additions & 1 deletion components/server/ee/src/workspace/workspace-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,30 @@ import { ErrorCodes } from '@gitpod/gitpod-protocol/lib/messaging/error';
import { HostContextProvider } from '../../../src/auth/host-context-provider';
import { RepoURL } from '../../../src/repohost';
import { UserDB } from '@gitpod/gitpod-db/lib';
import { UserCounter } from '../user/user-counter';

@injectable()
export class WorkspaceFactoryEE extends WorkspaceFactory {

@inject(LicenseEvaluator) protected readonly licenseEvaluator: LicenseEvaluator;
@inject(HostContextProvider) protected readonly hostContextProvider: HostContextProvider;
@inject(UserCounter) protected readonly userCounter: UserCounter;

@inject(UserDB) protected readonly userDB: UserDB;

protected async requireEELicense(feature: Feature) {
const userCount = await this.userDB.getUserCount(true);
const cachedUserCount = this.userCounter.count;

let userCount: number;
if (cachedUserCount == null) {
userCount = await this.userDB.getUserCount(true);
this.userCounter.count = userCount;
console.log('user count')
} else {
console.log('cache user count')
userCount = cachedUserCount;
}
console.log({ userCount })

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

0 comments on commit 8fdfc65

Please sign in to comment.