Skip to content

Commit

Permalink
feat: usage limits
Browse files Browse the repository at this point in the history
  • Loading branch information
IncognitoTGT committed Dec 2, 2024
1 parent 1646335 commit 38d1ea6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .config/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@
"default": 1440,
"description": "The amount of time to keep an inactive session alive for, in minutes.",
"type": "number"
},
"usageLimits": {
"additionalProperties": false,
"description": "Session per user usage limit configuration",
"properties": {
"instance": {
"type": "number"
},
"user": {
"type": "number"
}
},
"type": "object"
}
},
"type": "object"
Expand Down
19 changes: 19 additions & 0 deletions src/lib/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ async function createSession(image: string) {
const config = getConfig();
const userSession = await auth();
if (!userSession?.user) throw new Error("User not found");
if (config.session?.usageLimits) {
const { userIsAdmin, allSessions } = await db.transaction(async (tx) => ({
userIsAdmin: (
await tx.query.user.findFirst({
where: (user, { eq }) => eq(user.id, userSession?.user?.id as string),
columns: {
isAdmin: true,
},
})
)?.isAdmin,
allSessions: await tx.select().from(session),
}));
if (config.session?.usageLimits.instance && config.session?.usageLimits.instance <= allSessions.length)
throw new Error("Instance limit exceeded");
if (config.session.usageLimits.user && !userIsAdmin) {
const userSessions = allSessions.filter((v) => v.userId === userSession.user.id);
if (config.session?.usageLimits.user <= userSessions.length) throw new Error("User session limit exceeded");
}
}
const container = await docker.createContainer({
name: `stardust-${createId()}-${image.split("/")[2] || image.split("/")[1]}`,
Image: image,
Expand Down
7 changes: 7 additions & 0 deletions src/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ export interface SessionConfig {
* @default system default
*/
dnsServers?: string[];
/**
* Session per user usage limit configuration
*/
usageLimits?: {
instance?: number;
user?: number;
};
}

export interface TurnstileConfig {
Expand Down

0 comments on commit 38d1ea6

Please sign in to comment.