Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

improve performance of limit update methods #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
samuelcolvin merged 1 commit into main from improve-limit-updates
Oct 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions gateway/src/db.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -147,42 +147,48 @@ RETURNING entityType, scope, spend > spendingLimit as ex;`,
}

async updateProjectLimits(projectId: number, { daily, weekly, monthly }: LimitUpdate) {
const stmts = []
if (daily) {
await this.updateSpend(daily, 'project', projectId, 'daily')
stmts.push(this.updateSpend(daily, 'project', projectId, 'daily'))
}
if (weekly) {
await this.updateSpend(weekly, 'project', projectId, 'weekly')
stmts.push(this.updateSpend(weekly, 'project', projectId, 'weekly'))
}
if (monthly) {
await this.updateSpend(monthly, 'project', projectId, 'monthly')
stmts.push(this.updateSpend(monthly, 'project', projectId, 'monthly'))
}
await this.db.batch(stmts)
}

async updateUserLimits(userId: number, { daily, weekly, monthly }: LimitUpdate) {
const stmts = []
if (daily) {
await this.updateSpend(daily, 'user', userId, 'daily')
stmts.push(this.updateSpend(daily, 'user', userId, 'daily'))
}
if (weekly) {
await this.updateSpend(weekly, 'user', userId, 'weekly')
stmts.push(this.updateSpend(weekly, 'user', userId, 'weekly'))
}
if (monthly) {
await this.updateSpend(monthly, 'user', userId, 'monthly')
stmts.push(this.updateSpend(monthly, 'user', userId, 'monthly'))
}
await this.db.batch(stmts)
}

async updateKeyLimits(keyId: number, { daily, weekly, monthly, total }: KeyLimitUpdate) {
const stmts = []
if (daily) {
await this.updateSpend(daily, 'key', keyId, 'daily')
stmts.push(this.updateSpend(daily, 'key', keyId, 'daily'))
}
if (weekly) {
await this.updateSpend(weekly, 'key', keyId, 'weekly')
stmts.push(this.updateSpend(weekly, 'key', keyId, 'weekly'))
}
if (monthly) {
await this.updateSpend(monthly, 'key', keyId, 'monthly')
stmts.push(this.updateSpend(monthly, 'key', keyId, 'monthly'))
}
if (total) {
await this.updateSpend(total, 'key', keyId, 'total')
stmts.push(this.updateSpend(total, 'key', keyId, 'total'))
}
await this.db.batch(stmts)
}

async spendStatus(entityType: EntityType, entityId?: number): Promise<SpendStatus[]> {
Expand Down Expand Up @@ -218,11 +224,10 @@ WHERE entityType = ? ${entityIdClause}
}))
}

protected async updateSpend(limit: number, entityType: EntityType, entityId: number, scope: Scope) {
await this.db
protected updateSpend(limit: number, entityType: EntityType, entityId: number, scope: Scope): D1PreparedStatement {
return this.db
.prepare(`UPDATE spend SET spendingLimit = ? WHERE entityType = ? AND entityId = ? and scope = ?`)
.bind(limit, entityTypeLookup[entityType], entityId, scopeLookup[scope])
.run()
}
}

Expand Down

AltStyle によって変換されたページ (->オリジナル) /