Skip to content

Commit

Permalink
real time limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Jan 31, 2024
1 parent 6c0571d commit c1bbd68
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/pds/src/api/com/atproto/server/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function (server: Server, ctx: AppContext) {
points: 100,
},
handler: async ({ input, req }) => {
const hasAvailability = ctx.signupLimiter.hasAvailability()
const hasAvailability = await ctx.signupLimiter.hasAvailability()

const {
did,
Expand Down
18 changes: 8 additions & 10 deletions packages/pds/src/api/com/atproto/temp/checkSignupQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ export default function (server: Server, ctx: AppContext) {
placeInQueue = res?.count
}

const limiterStatus = ctx.signupLimiter.status
const limiter = ctx.signupLimiter
let estimatedTimeMs: number | undefined
if (
placeInQueue &&
!limiterStatus.disableSignups &&
limiterStatus.accountsInPeriod > 0
) {
estimatedTimeMs = Math.ceil(
(placeInQueue * limiterStatus.periodMs) /
limiterStatus.accountsInPeriod,
)
if (placeInQueue && !limiter.flags.disableSignups) {
const accountsInPeriod = await limiter.accountsInPeriod()
if (accountsInPeriod > 0) {
estimatedTimeMs = Math.ceil(
(placeInQueue * limiter.flags.periodMs) / accountsInPeriod,
)
}
}

return {
Expand Down
19 changes: 12 additions & 7 deletions packages/pds/src/signup-queue/limiter.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { SECOND } from '@atproto/common'
import { limiterLogger as log } from '../logger'
import Database from '../db'
import { LimiterStatus, getQueueStatus } from './util'
import { LimiterFlags, getAccountsInPeriod, getQueueStatus } from './util'

export class SignupLimiter {
destroyed = false
promise: Promise<void> = Promise.resolve()
timer: NodeJS.Timer | undefined
status: LimiterStatus
flags: LimiterFlags

constructor(private db: Database) {}

hasAvailability(): boolean {
if (this.status.disableSignups) return false
return this.status.accountsInPeriod < this.status.periodAllowance
async hasAvailability(): Promise<boolean> {
if (this.flags.disableSignups) return false
const accountsInPeriod = await this.accountsInPeriod()
return accountsInPeriod < this.flags.periodAllowance
}

async start() {
Expand All @@ -38,9 +39,13 @@ export class SignupLimiter {
await this.promise
}

async accountsInPeriod(): Promise<number> {
return getAccountsInPeriod(this.db, this.flags.periodMs)
}

async refresh() {
this.status = await getQueueStatus(this.db)
this.flags = await getQueueStatus(this.db)

log.info({ ...this.status }, 'limiter refresh')
log.info({ ...this.flags }, 'limiter refresh')
}
}

0 comments on commit c1bbd68

Please sign in to comment.