Skip to content

Commit

Permalink
Write rate limits (bluesky-social#1578)
Browse files Browse the repository at this point in the history
* get rate limit ip correctly

* add write rate-limits
  • Loading branch information
dholms authored and mloar committed Sep 25, 2023
1 parent 370b123 commit fd599f5
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
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 @@ -288,6 +288,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

0 comments on commit fd599f5

Please sign in to comment.