Skip to content
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

Write rate limits #1578

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
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
28 changes: 28 additions & 0 deletions packages/pds/src/api/com/atproto/repo/applyWrites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InvalidRequestError, AuthRequiredError } from '@atproto/xrpc-server'
import { prepareCreate, prepareDelete, prepareUpdate } from '../../../../repo'
import { Server } from '../../../../lexicon'
import {
HandlerInput,
isCreate,
isUpdate,
isDelete,
Expand All @@ -15,9 +16,36 @@ import {
import AppContext from '../../../../context'
import { ConcurrentWriteError } from '../../../../services/repo'

const ratelimitPoints = ({ input }: { input: HandlerInput }) => {
let points = 0
for (const op of input.body.writes) {
if (isCreate(op)) {
points += 3
} else if (isUpdate(op)) {
points += 2
} else {
points += 1
}
}
return points
}

export default function (server: Server, ctx: AppContext) {
server.com.atproto.repo.applyWrites({
auth: ctx.accessVerifierCheckTakedown,
rateLimit: [
{
name: 'repo-write-hour',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: ratelimitPoints,
},
{
name: 'repo-write-day',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: ratelimitPoints,
},
],

handler: async ({ input, auth }) => {
const tx = input.body
const { repo, validate, swapCommit } = tx
Expand Down
12 changes: 12 additions & 0 deletions packages/pds/src/api/com/atproto/repo/createRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ import { ConcurrentWriteError } from '../../../../services/repo'
export default function (server: Server, ctx: AppContext) {
server.com.atproto.repo.createRecord({
auth: ctx.accessVerifierCheckTakedown,
rateLimit: [
{
name: 'repo-write-hour',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 3,
},
{
name: 'repo-write-day',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 3,
},
],
handler: async ({ input, auth }) => {
const { repo, collection, rkey, record, swapCommit, validate } =
input.body
Expand Down
12 changes: 12 additions & 0 deletions packages/pds/src/api/com/atproto/repo/deleteRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import { ConcurrentWriteError } from '../../../../services/repo'
export default function (server: Server, ctx: AppContext) {
server.com.atproto.repo.deleteRecord({
auth: ctx.accessVerifierCheckTakedown,
rateLimit: [
{
name: 'repo-write-hour',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 1,
},
{
name: 'repo-write-day',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 1,
},
],
handler: async ({ input, auth }) => {
const { repo, collection, rkey, swapCommit, swapRecord } = input.body
const did = await ctx.services.account(ctx.db).getDidForActor(repo)
Expand Down
12 changes: 12 additions & 0 deletions packages/pds/src/api/com/atproto/repo/putRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ import { ConcurrentWriteError } from '../../../../services/repo'
export default function (server: Server, ctx: AppContext) {
server.com.atproto.repo.putRecord({
auth: ctx.accessVerifierCheckTakedown,
rateLimit: [
{
name: 'repo-write-hour',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 2,
},
{
name: 'repo-write-day',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 2,
},
],
handler: async ({ auth, input }) => {
const {
repo,
Expand Down
14 changes: 13 additions & 1 deletion packages/pds/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
RateLimiterOpts,
Options as XrpcServerOptions,
} from '@atproto/xrpc-server'
import { MINUTE } from '@atproto/common'
import { DAY, HOUR, MINUTE } from '@atproto/common'
import * as appviewConsumers from './app-view/event-stream/consumers'
import inProcessAppView from './app-view/api'
import API from './api'
Expand Down Expand Up @@ -286,6 +286,18 @@ export class PDS {
points: 3000,
},
],
shared: [
{
name: 'repo-write-hour',
durationMs: HOUR,
points: 5000, // creates=3, puts=2, deletes=1
},
{
name: 'repo-write-day',
durationMs: DAY,
points: 35000, // creates=3, puts=2, deletes=1
},
],
}
}

Expand Down
Loading